using System;
using UnityEngine;
namespace TEngine
{
///
/// 实体。
///
public sealed class Entity : MonoBehaviour, IEntity
{
[SerializeField]
private int _id;
private string _entityAssetName;
private IEntityGroup _entityGroup;
private EntityLogic _entityLogic;
///
/// 获取实体编号。
///
public int Id => _id;
///
/// 获取实体资源名称。
///
public string EntityAssetName => _entityAssetName;
///
/// 获取实体实例。
///
public object Handle => gameObject;
///
/// 获取实体所属的实体组。
///
public IEntityGroup EntityGroup => _entityGroup;
///
/// 获取实体逻辑。
///
public EntityLogic Logic => _entityLogic;
///
/// 实体初始化。
///
/// 实体编号。
/// 实体资源名称。
/// 实体所属的实体组。
/// 是否是新实例。
/// 用户自定义数据。
public void OnInit(int entityId, string entityAssetName, IEntityGroup entityGroup, bool isNewInstance, object userData)
{
_id = entityId;
_entityAssetName = entityAssetName;
if (isNewInstance)
{
_entityGroup = entityGroup;
}
else if (_entityGroup != entityGroup)
{
Log.Error("Entity group is inconsistent for non-new-instance entity.");
return;
}
ShowEntityInfo showEntityInfo = (ShowEntityInfo)userData;
Type entityLogicType = showEntityInfo.EntityLogicType;
if (entityLogicType == null)
{
Log.Error("Entity logic type is invalid.");
return;
}
if (_entityLogic != null)
{
if (_entityLogic.GetType() == entityLogicType)
{
_entityLogic.enabled = true;
return;
}
Destroy(_entityLogic);
_entityLogic = null;
}
_entityLogic = gameObject.AddComponent(entityLogicType) as EntityLogic;
if (_entityLogic == null)
{
Log.Error("Entity '{0}' can not add entity logic.", entityAssetName);
return;
}
try
{
_entityLogic.OnInit(showEntityInfo.UserData);
}
catch (Exception exception)
{
Log.Error("Entity '[{0}]{1}' OnInit with exception '{2}'.", _id, _entityAssetName, exception);
}
}
///
/// 实体回收。
///
public void OnRecycle()
{
try
{
_entityLogic.OnRecycle();
_entityLogic.enabled = false;
}
catch (Exception exception)
{
Log.Error("Entity '[{0}]{1}' OnRecycle with exception '{2}'.", _id, _entityAssetName, exception);
}
_id = 0;
}
///
/// 实体显示。
///
/// 用户自定义数据。
public void OnShow(object userData)
{
ShowEntityInfo showEntityInfo = (ShowEntityInfo)userData;
try
{
_entityLogic.OnShow(showEntityInfo.UserData);
}
catch (Exception exception)
{
Log.Error("Entity '[{0}]{1}' OnShow with exception '{2}'.", _id, _entityAssetName, exception);
}
}
///
/// 实体隐藏。
///
/// 是否是关闭实体管理器时触发。
/// 用户自定义数据。
public void OnHide(bool isShutdown, object userData)
{
try
{
if (_entityLogic)
{
_entityLogic.OnHide(isShutdown, userData);
}
}
catch (Exception exception)
{
Log.Error("Entity '[{0}]{1}' OnHide with exception '{2}'.", _id, _entityAssetName, exception);
}
}
///
/// 实体附加子实体。
///
/// 附加的子实体。
/// 用户自定义数据。
public void OnAttached(IEntity childEntity, object userData)
{
AttachEntityInfo attachEntityInfo = (AttachEntityInfo)userData;
try
{
_entityLogic.OnAttached(((Entity)childEntity).Logic, attachEntityInfo.ParentTransform, attachEntityInfo.UserData);
}
catch (Exception exception)
{
Log.Error("Entity '[{0}]{1}' OnAttached with exception '{2}'.", _id, _entityAssetName, exception);
}
}
///
/// 实体解除子实体。
///
/// 解除的子实体。
/// 用户自定义数据。
public void OnDetached(IEntity childEntity, object userData)
{
try
{
_entityLogic.OnDetached(((Entity)childEntity).Logic, userData);
}
catch (Exception exception)
{
Log.Error("Entity '[{0}]{1}' OnDetached with exception '{2}'.", _id, _entityAssetName, exception);
}
}
///
/// 实体附加子实体。
///
/// 被附加的父实体。
/// 用户自定义数据。
public void OnAttachTo(IEntity parentEntity, object userData)
{
AttachEntityInfo attachEntityInfo = (AttachEntityInfo)userData;
try
{
_entityLogic.OnAttachTo(((Entity)parentEntity).Logic, attachEntityInfo.ParentTransform, attachEntityInfo.UserData);
}
catch (Exception exception)
{
Log.Error("Entity '[{0}]{1}' OnAttachTo with exception '{2}'.", _id, _entityAssetName, exception);
}
MemoryPool.Release(attachEntityInfo);
}
///
/// 实体解除子实体。
///
/// 被解除的父实体。
/// 用户自定义数据。
public void OnDetachFrom(IEntity parentEntity, object userData)
{
try
{
_entityLogic.OnDetachFrom(((Entity)parentEntity).Logic, userData);
}
catch (Exception exception)
{
Log.Error("Entity '[{0}]{1}' OnDetachFrom with exception '{2}'.", _id, _entityAssetName, exception);
}
}
///
/// 实体轮询。
///
/// 逻辑流逝时间,以秒为单位。
/// 真实流逝时间,以秒为单位。
public void OnUpdate(float elapseSeconds, float realElapseSeconds)
{
try
{
_entityLogic.OnExecuteUpdate(elapseSeconds, realElapseSeconds);
}
catch (Exception exception)
{
Log.Error("Entity '[{0}]{1}' OnUpdate with exception '{2}'.", _id, _entityAssetName, exception);
}
}
}
}