//------------------------------------------------------------ // Game Framework // Copyright © 2013-2021 loyalsoft. All rights reserved. // Homepage: http://www.game7000.com/ // Feedback: http://www.game7000.com/ //------------------------------------------------------------ using GameFramework.ObjectPool; using System.Collections.Generic; namespace GameFramework.Entity { internal sealed partial class EntityManager : GameFrameworkModule, IEntityManager { /// /// 实体组。 /// private sealed class EntityGroup : IEntityGroup { private readonly string m_Name; private readonly IEntityGroupHelper m_EntityGroupHelper; private readonly IObjectPool m_InstancePool; private readonly GameFrameworkLinkedList m_Entities; private LinkedListNode m_CachedNode; /// /// 初始化实体组的新实例。 /// /// 实体组名称。 /// 实体实例对象池自动释放可释放对象的间隔秒数。 /// 实体实例对象池容量。 /// 实体实例对象池对象过期秒数。 /// 实体实例对象池的优先级。 /// 实体组辅助器。 /// 对象池管理器。 public EntityGroup(string name, float instanceAutoReleaseInterval, int instanceCapacity, float instanceExpireTime, int instancePriority, IEntityGroupHelper entityGroupHelper, IObjectPoolManager objectPoolManager) { if (string.IsNullOrEmpty(name)) { throw new GameFrameworkException("Entity group name is invalid."); } if (entityGroupHelper == null) { throw new GameFrameworkException("Entity group helper is invalid."); } m_Name = name; m_EntityGroupHelper = entityGroupHelper; m_InstancePool = objectPoolManager.CreateSingleSpawnObjectPool(Utility.Text.Format("Entity Instance Pool ({0})", name), instanceCapacity, instanceExpireTime, instancePriority); m_InstancePool.AutoReleaseInterval = instanceAutoReleaseInterval; m_Entities = new GameFrameworkLinkedList(); m_CachedNode = null; } /// /// 获取实体组名称。 /// public string Name { get { return m_Name; } } /// /// 获取实体组中实体数量。 /// public int EntityCount { get { return m_Entities.Count; } } /// /// 获取或设置实体组实例对象池自动释放可释放对象的间隔秒数。 /// public float InstanceAutoReleaseInterval { get { return m_InstancePool.AutoReleaseInterval; } set { m_InstancePool.AutoReleaseInterval = value; } } /// /// 获取或设置实体组实例对象池的容量。 /// public int InstanceCapacity { get { return m_InstancePool.Capacity; } set { m_InstancePool.Capacity = value; } } /// /// 获取或设置实体组实例对象池对象过期秒数。 /// public float InstanceExpireTime { get { return m_InstancePool.ExpireTime; } set { m_InstancePool.ExpireTime = value; } } /// /// 获取或设置实体组实例对象池的优先级。 /// public int InstancePriority { get { return m_InstancePool.Priority; } set { m_InstancePool.Priority = value; } } /// /// 获取实体组辅助器。 /// public IEntityGroupHelper Helper { get { return m_EntityGroupHelper; } } /// /// 实体组轮询。 /// /// 逻辑流逝时间,以秒为单位。 /// 真实流逝时间,以秒为单位。 public void Update(float elapseSeconds, float realElapseSeconds) { LinkedListNode current = m_Entities.First; while (current != null) { m_CachedNode = current.Next; current.Value.OnUpdate(elapseSeconds, realElapseSeconds); current = m_CachedNode; m_CachedNode = null; } } /// /// 实体组中是否存在实体。 /// /// 实体序列编号。 /// 实体组中是否存在实体。 public bool HasEntity(int entityId) { foreach (IEntity entity in m_Entities) { if (entity.Id == entityId) { return true; } } return false; } /// /// 实体组中是否存在实体。 /// /// 实体资源名称。 /// 实体组中是否存在实体。 public bool HasEntity(string entityAssetName) { if (string.IsNullOrEmpty(entityAssetName)) { throw new GameFrameworkException("Entity asset name is invalid."); } foreach (IEntity entity in m_Entities) { if (entity.EntityAssetName == entityAssetName) { return true; } } return false; } /// /// 从实体组中获取实体。 /// /// 实体序列编号。 /// 要获取的实体。 public IEntity GetEntity(int entityId) { foreach (IEntity entity in m_Entities) { if (entity.Id == entityId) { return entity; } } return null; } /// /// 从实体组中获取实体。 /// /// 实体资源名称。 /// 要获取的实体。 public IEntity GetEntity(string entityAssetName) { if (string.IsNullOrEmpty(entityAssetName)) { throw new GameFrameworkException("Entity asset name is invalid."); } foreach (IEntity entity in m_Entities) { if (entity.EntityAssetName == entityAssetName) { return entity; } } return null; } /// /// 从实体组中获取实体。 /// /// 实体资源名称。 /// 要获取的实体。 public IEntity[] GetEntities(string entityAssetName) { if (string.IsNullOrEmpty(entityAssetName)) { throw new GameFrameworkException("Entity asset name is invalid."); } List results = new List(); foreach (IEntity entity in m_Entities) { if (entity.EntityAssetName == entityAssetName) { results.Add(entity); } } return results.ToArray(); } /// /// 从实体组中获取实体。 /// /// 实体资源名称。 /// 要获取的实体。 public void GetEntities(string entityAssetName, List results) { if (string.IsNullOrEmpty(entityAssetName)) { throw new GameFrameworkException("Entity asset name is invalid."); } if (results == null) { throw new GameFrameworkException("Results is invalid."); } results.Clear(); foreach (IEntity entity in m_Entities) { if (entity.EntityAssetName == entityAssetName) { results.Add(entity); } } } /// /// 从实体组中获取所有实体。 /// /// 实体组中的所有实体。 public IEntity[] GetAllEntities() { List results = new List(); foreach (IEntity entity in m_Entities) { results.Add(entity); } return results.ToArray(); } /// /// 从实体组中获取所有实体。 /// /// 实体组中的所有实体。 public void GetAllEntities(List results) { if (results == null) { throw new GameFrameworkException("Results is invalid."); } results.Clear(); foreach (IEntity entity in m_Entities) { results.Add(entity); } } /// /// 往实体组增加实体。 /// /// 要增加的实体。 public void AddEntity(IEntity entity) { m_Entities.AddLast(entity); } /// /// 从实体组移除实体。 /// /// 要移除的实体。 public void RemoveEntity(IEntity entity) { if (m_CachedNode != null && m_CachedNode.Value == entity) { m_CachedNode = m_CachedNode.Next; } if (!m_Entities.Remove(entity)) { throw new GameFrameworkException(Utility.Text.Format("Entity group '{0}' not exists specified entity '[{1}]{2}'.", m_Name, entity.Id, entity.EntityAssetName)); } } public void RegisterEntityInstanceObject(EntityInstanceObject obj, bool spawned) { m_InstancePool.Register(obj, spawned); } public EntityInstanceObject SpawnEntityInstanceObject(string name) { return m_InstancePool.Spawn(name); } public void UnspawnEntity(IEntity entity) { m_InstancePool.Unspawn(entity.Handle); } public void SetEntityInstanceLocked(object entityInstance, bool locked) { if (entityInstance == null) { throw new GameFrameworkException("Entity instance is invalid."); } m_InstancePool.SetLocked(entityInstance, locked); } public void SetEntityInstancePriority(object entityInstance, int priority) { if (entityInstance == null) { throw new GameFrameworkException("Entity instance is invalid."); } m_InstancePool.SetPriority(entityInstance, priority); } } } }