123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280 |
- //------------------------------------------------------------
- // 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
- {
- /// <summary>
- /// 实体。
- /// </summary>
- public sealed class Entity : MonoBehaviour, IEntity
- {
- private int m_Id;
- private string m_EntityAssetName;
- private IEntityGroup m_EntityGroup;
- private EntityLogic m_EntityLogic;
- /// <summary>
- /// 获取实体编号。
- /// </summary>
- public int Id
- {
- get
- {
- return m_Id;
- }
- }
- /// <summary>
- /// 获取实体资源名称。
- /// </summary>
- public string EntityAssetName
- {
- get
- {
- return m_EntityAssetName;
- }
- }
- /// <summary>
- /// 获取实体实例。
- /// </summary>
- public object Handle
- {
- get
- {
- return gameObject;
- }
- }
- /// <summary>
- /// 获取实体所属的实体组。
- /// </summary>
- public IEntityGroup EntityGroup
- {
- get
- {
- return m_EntityGroup;
- }
- }
- /// <summary>
- /// 获取实体逻辑。
- /// </summary>
- public EntityLogic Logic
- {
- get
- {
- return m_EntityLogic;
- }
- }
- /// <summary>
- /// 实体初始化。
- /// </summary>
- /// <param name="entityId">实体编号。</param>
- /// <param name="entityAssetName">实体资源名称。</param>
- /// <param name="entityGroup">实体所属的实体组。</param>
- /// <param name="isNewInstance">是否是新实例。</param>
- /// <param name="userData">用户自定义数据。</param>
- 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());
- }
- }
- /// <summary>
- /// 实体回收。
- /// </summary>
- 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;
- }
- /// <summary>
- /// 实体显示。
- /// </summary>
- /// <param name="userData">用户自定义数据。</param>
- 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());
- }
- }
- /// <summary>
- /// 实体隐藏。
- /// </summary>
- /// <param name="isShutdown">是否是关闭实体管理器时触发。</param>
- /// <param name="userData">用户自定义数据。</param>
- 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());
- }
- }
- /// <summary>
- /// 实体附加子实体。
- /// </summary>
- /// <param name="childEntity">附加的子实体。</param>
- /// <param name="userData">用户自定义数据。</param>
- 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());
- }
- }
- /// <summary>
- /// 实体解除子实体。
- /// </summary>
- /// <param name="childEntity">解除的子实体。</param>
- /// <param name="userData">用户自定义数据。</param>
- 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());
- }
- }
- /// <summary>
- /// 实体附加子实体。
- /// </summary>
- /// <param name="parentEntity">被附加的父实体。</param>
- /// <param name="userData">用户自定义数据。</param>
- 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);
- }
- /// <summary>
- /// 实体解除子实体。
- /// </summary>
- /// <param name="parentEntity">被解除的父实体。</param>
- /// <param name="userData">用户自定义数据。</param>
- 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());
- }
- }
- /// <summary>
- /// 实体轮询。
- /// </summary>
- /// <param name="elapseSeconds">逻辑流逝时间,以秒为单位。</param>
- /// <param name="realElapseSeconds">真实流逝时间,以秒为单位。</param>
- 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());
- }
- }
- }
- }
|