using System; using System.Collections.Generic; using UnityEngine; namespace TEngine { /// /// 实体逻辑基类。 /// public abstract class EntityLogic : MonoBehaviour { private bool _available = false; private bool _visible = false; private Entity _entity = null; private Transform _cachedTransform = null; private int _originalLayer = 0; private Transform _originalTransform = null; /// /// 获取实体。 /// public Entity Entity => _entity; /// /// 获取或设置实体名称。 /// public string Name { get => gameObject.name; set => gameObject.name = value; } /// /// 获取实体是否可用。 /// public bool Available => _available; /// /// 获取或设置实体是否可见。 /// public bool Visible { get => _available && _visible; set { if (!_available) { Log.Warning("Entity '{0}' is not available.", Name); return; } if (_visible == value) { return; } _visible = value; InternalSetVisible(value); } } /// /// 获取已缓存的 Transform。 /// public Transform CachedTransform => _cachedTransform; private ActorEventDispatcher _event; /// /// 事件分发器。 /// public ActorEventDispatcher Event => _event ??= MemoryPool.Acquire(); /// /// 实体初始化。 /// /// 用户自定义数据。 protected internal virtual void OnInit(object userData) { if (_cachedTransform == null) { _cachedTransform = transform; } _entity = GetComponent(); _originalLayer = gameObject.layer; _originalTransform = CachedTransform.parent; } /// /// 实体回收。 /// protected internal virtual void OnRecycle() { } /// /// 实体显示。 /// /// 用户自定义数据。 protected internal virtual void OnShow(object userData) { _available = true; Visible = true; } /// /// 实体隐藏。 /// /// 是否是关闭实体管理器时触发。 /// 用户自定义数据。 protected internal virtual void OnHide(bool isShutdown, object userData) { if (_event != null) { MemoryPool.Release(_event); _event = null; } if (gameObject != null) { // gameObject.SetLayerRecursively(_originalLayer); } Visible = false; _available = false; var iter = _componentMap.GetEnumerator(); while (iter.MoveNext()) { EntityComponent component = iter.Current.Value; if (component == null) { continue; } if (gameObject != null) { MemoryPool.Release(component); } } iter.Dispose(); _componentMap.Clear(); _updateComponents.Clear(); } /// /// 实体附加子实体。 /// /// 附加的子实体。 /// 被附加父实体的位置。 /// 用户自定义数据。 protected internal virtual void OnAttached(EntityLogic childEntity, Transform parentTransform, object userData) { } /// /// 实体解除子实体。 /// /// 解除的子实体。 /// 用户自定义数据。 protected internal virtual void OnDetached(EntityLogic childEntity, object userData) { } /// /// 实体附加子实体。 /// /// 被附加的父实体。 /// 被附加父实体的位置。 /// 用户自定义数据。 protected internal virtual void OnAttachTo(EntityLogic parentEntity, Transform parentTransform, object userData) { CachedTransform.SetParent(parentTransform); } /// /// 实体解除子实体。 /// /// 被解除的父实体。 /// 用户自定义数据。 protected internal virtual void OnDetachFrom(EntityLogic parentEntity, object userData) { CachedTransform.SetParent(_originalTransform); } /// /// 实体轮询。 /// /// 逻辑流逝时间,以秒为单位。 /// 真实流逝时间,以秒为单位。 protected virtual void OnUpdate(float elapseSeconds, float realElapseSeconds) { } /// /// 实体轮询。 /// /// 逻辑流逝时间,以秒为单位。 /// 真实流逝时间,以秒为单位。 internal void OnExecuteUpdate(float elapseSeconds, float realElapseSeconds) { OnUpdate(elapseSeconds, realElapseSeconds); var iter = _updateComponents.GetEnumerator(); while (iter.MoveNext()) { var current = iter.Current; if (current != null) { current.OnUpdate(elapseSeconds, realElapseSeconds); } } iter.Dispose(); } /// /// 设置实体的可见性。 /// /// 实体的可见性。 protected virtual void InternalSetVisible(bool visible) { gameObject.SetActive(visible); } private readonly Dictionary _componentMap = new Dictionary(); private static readonly GameFrameworkLinkedList _updateComponents = new GameFrameworkLinkedList(); /// /// 添加实体组件。 /// /// 实体组件类型。 /// 实体组件。 public T AddEntityComponent() where T : EntityComponent { T ret = MemoryPool.Acquire(typeof(T)) as T; if (ret == null) { Log.Fatal($"add entityComponent failed"); return null; } _componentMap.Add(typeof(T),ret); if (ret.NeedUpdate) { LinkedListNode current = _updateComponents.First; while (current != null) { if (ret.Priority > current.Value.Priority) { break; } current = current.Next; } if (current != null) { _updateComponents.AddBefore(current, ret); } else { _updateComponents.AddLast(ret); } } ret?.OnAwake(this); return ret; } /// /// 获取实体组件。 /// /// 实体组件类型。 /// 实体组件。 public T GetEntityComponent() where T : EntityComponent { _componentMap.TryGetValue(typeof(T), out EntityComponent component); return component as T; } /// /// 获取或者添加实体组件。 /// /// 实体组件类型。 /// 实体组件。 public T GetOrAddEntityComponent() where T : EntityComponent { T ret = GetEntityComponent() ?? AddEntityComponent(); return ret; } /// /// 移除实体组件。 /// /// 实体组件类型。 /// 实体组件。 public void RemoveEntityComponent() where T : EntityComponent { Type key = typeof(T); RemoveEntityComponentImp(key); } /// /// 移除实体组件。 /// /// 实体组件。 /// 实体组件。 public void RemoveEntityComponent(EntityComponent component) { Type key = component.GetType(); RemoveEntityComponentImp(key); } private void RemoveEntityComponentImp(Type type) { if (_componentMap.ContainsKey(type)) { EntityComponent component = _componentMap[type]; if (component.NeedUpdate) { _updateComponents.Remove(component); } MemoryPool.Release(component); _componentMap.Remove(type); } } } }