//------------------------------------------------------------ // Game Framework // Copyright © 2013-2021 loyalsoft. All rights reserved. // Homepage: http://www.game7000.com/ // Feedback: http://www.game7000.com/ //------------------------------------------------------------ using GameFramework; using GameFramework.Entity; using System; using UnityEngine; namespace UnityGameFramework.Runtime { /// /// 实体。 /// public sealed class Entity : MonoBehaviour, IEntity { private int m_Id; private string m_EntityAssetName; private IEntityGroup m_EntityGroup; private EntityLogic m_EntityLogic; /// /// 获取实体编号。 /// public int Id { get { return m_Id; } } /// /// 获取实体资源名称。 /// public string EntityAssetName { get { return m_EntityAssetName; } } /// /// 获取实体实例。 /// public object Handle { get { return gameObject; } } /// /// 获取实体所属的实体组。 /// public IEntityGroup EntityGroup { get { return m_EntityGroup; } } /// /// 获取实体逻辑。 /// public EntityLogic Logic { get { return m_EntityLogic; } } /// /// 实体初始化。 /// /// 实体编号。 /// 实体资源名称。 /// 实体所属的实体组。 /// 是否是新实例。 /// 用户自定义数据。 public void OnInit(int entityId, string entityAssetName, IEntityGroup entityGroup, bool isNewInstance, object userData) { m_Id = entityId; m_EntityAssetName = entityAssetName; if (isNewInstance) { m_EntityGroup = entityGroup; } else if (m_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 (m_EntityLogic != null) { if (m_EntityLogic.GetType() == entityLogicType) { m_EntityLogic.enabled = true; return; } Destroy(m_EntityLogic); m_EntityLogic = null; } m_EntityLogic = gameObject.AddComponent(entityLogicType) as EntityLogic; if (m_EntityLogic == null) { Log.Error("Entity '{0}' can not add entity logic.", entityAssetName); return; } try { m_EntityLogic.OnInit(showEntityInfo.UserData); } catch (Exception exception) { Log.Error("Entity '[{0}]{1}' OnInit with exception '{2}'.", m_Id.ToString(), m_EntityAssetName, exception.ToString()); } } /// /// 实体回收。 /// public void OnRecycle() { try { m_EntityLogic.OnRecycle(); m_EntityLogic.enabled = false; } catch (Exception exception) { Log.Error("Entity '[{0}]{1}' OnRecycle with exception '{2}'.", m_Id.ToString(), m_EntityAssetName, exception.ToString()); } m_Id = 0; } /// /// 实体显示。 /// /// 用户自定义数据。 public void OnShow(object userData) { ShowEntityInfo showEntityInfo = (ShowEntityInfo)userData; try { m_EntityLogic.OnShow(showEntityInfo.UserData); } catch (Exception exception) { Log.Error("Entity '[{0}]{1}' OnShow with exception '{2}'.", m_Id.ToString(), m_EntityAssetName, exception.ToString()); } } /// /// 实体隐藏。 /// /// 是否是关闭实体管理器时触发。 /// 用户自定义数据。 public void OnHide(bool isShutdown, object userData) { try { m_EntityLogic.OnHide(isShutdown, userData); } catch (Exception exception) { Log.Error("Entity '[{0}]{1}' OnHide with exception '{2}'.", m_Id.ToString(), m_EntityAssetName, exception.ToString()); } } /// /// 实体附加子实体。 /// /// 附加的子实体。 /// 用户自定义数据。 public void OnAttached(IEntity childEntity, object userData) { AttachEntityInfo attachEntityInfo = (AttachEntityInfo)userData; try { m_EntityLogic.OnAttached(((Entity)childEntity).Logic, attachEntityInfo.ParentTransform, attachEntityInfo.UserData); } catch (Exception exception) { Log.Error("Entity '[{0}]{1}' OnAttached with exception '{2}'.", m_Id.ToString(), m_EntityAssetName, exception.ToString()); } } /// /// 实体解除子实体。 /// /// 解除的子实体。 /// 用户自定义数据。 public void OnDetached(IEntity childEntity, object userData) { try { m_EntityLogic.OnDetached(((Entity)childEntity).Logic, userData); } catch (Exception exception) { Log.Error("Entity '[{0}]{1}' OnDetached with exception '{2}'.", m_Id.ToString(), m_EntityAssetName, exception.ToString()); } } /// /// 实体附加子实体。 /// /// 被附加的父实体。 /// 用户自定义数据。 public void OnAttachTo(IEntity parentEntity, object userData) { AttachEntityInfo attachEntityInfo = (AttachEntityInfo)userData; try { m_EntityLogic.OnAttachTo(((Entity)parentEntity).Logic, attachEntityInfo.ParentTransform, attachEntityInfo.UserData); } catch (Exception exception) { Log.Error("Entity '[{0}]{1}' OnAttachTo with exception '{2}'.", m_Id.ToString(), m_EntityAssetName, exception.ToString()); } ReferencePool.Release(attachEntityInfo); } /// /// 实体解除子实体。 /// /// 被解除的父实体。 /// 用户自定义数据。 public void OnDetachFrom(IEntity parentEntity, object userData) { try { m_EntityLogic.OnDetachFrom(((Entity)parentEntity).Logic, userData); } catch (Exception exception) { Log.Error("Entity '[{0}]{1}' OnDetachFrom with exception '{2}'.", m_Id.ToString(), m_EntityAssetName, exception.ToString()); } } /// /// 实体轮询。 /// /// 逻辑流逝时间,以秒为单位。 /// 真实流逝时间,以秒为单位。 public void OnUpdate(float elapseSeconds, float realElapseSeconds) { try { m_EntityLogic.OnUpdate(elapseSeconds, realElapseSeconds); } catch (Exception exception) { Log.Error("Entity '[{0}]{1}' OnUpdate with exception '{2}'.", m_Id.ToString(), m_EntityAssetName, exception.ToString()); } } } }