//------------------------------------------------------------
// Game Framework
// Copyright © 2013-2021 loyalsoft. All rights reserved.
// Homepage: http://www.game7000.com/
// Feedback: http://www.game7000.com/
//------------------------------------------------------------
using GameFramework.ObjectPool;
using GameFramework.Resource;
using System;
using System.Collections.Generic;
namespace GameFramework.Entity
{
///
/// 实体管理器。
///
internal sealed partial class EntityManager : GameFrameworkModule, IEntityManager
{
private readonly Dictionary m_EntityInfos;
private readonly Dictionary m_EntityGroups;
private readonly Dictionary m_EntitiesBeingLoaded;
private readonly HashSet m_EntitiesToReleaseOnLoad;
private readonly Queue m_RecycleQueue;
private readonly LoadAssetCallbacks m_LoadAssetCallbacks;
private IObjectPoolManager m_ObjectPoolManager;
private IResourceManager m_ResourceManager;
private IEntityHelper m_EntityHelper;
private int m_Serial;
private bool m_IsShutdown;
private EventHandler m_ShowEntitySuccessEventHandler;
private EventHandler m_ShowEntityFailureEventHandler;
private EventHandler m_ShowEntityUpdateEventHandler;
private EventHandler m_ShowEntityDependencyAssetEventHandler;
private EventHandler m_HideEntityCompleteEventHandler;
///
/// 初始化实体管理器的新实例。
///
public EntityManager()
{
m_EntityInfos = new Dictionary();
m_EntityGroups = new Dictionary(StringComparer.Ordinal);
m_EntitiesBeingLoaded = new Dictionary();
m_EntitiesToReleaseOnLoad = new HashSet();
m_RecycleQueue = new Queue();
m_LoadAssetCallbacks = new LoadAssetCallbacks(LoadAssetSuccessCallback, LoadAssetFailureCallback, LoadAssetUpdateCallback, LoadAssetDependencyAssetCallback);
m_ObjectPoolManager = null;
m_ResourceManager = null;
m_EntityHelper = null;
m_Serial = 0;
m_IsShutdown = false;
m_ShowEntitySuccessEventHandler = null;
m_ShowEntityFailureEventHandler = null;
m_ShowEntityUpdateEventHandler = null;
m_ShowEntityDependencyAssetEventHandler = null;
m_HideEntityCompleteEventHandler = null;
}
///
/// 获取实体数量。
///
public int EntityCount
{
get
{
return m_EntityInfos.Count;
}
}
///
/// 获取实体组数量。
///
public int EntityGroupCount
{
get
{
return m_EntityGroups.Count;
}
}
///
/// 显示实体成功事件。
///
public event EventHandler ShowEntitySuccess
{
add
{
m_ShowEntitySuccessEventHandler += value;
}
remove
{
m_ShowEntitySuccessEventHandler -= value;
}
}
///
/// 显示实体失败事件。
///
public event EventHandler ShowEntityFailure
{
add
{
m_ShowEntityFailureEventHandler += value;
}
remove
{
m_ShowEntityFailureEventHandler -= value;
}
}
///
/// 显示实体更新事件。
///
public event EventHandler ShowEntityUpdate
{
add
{
m_ShowEntityUpdateEventHandler += value;
}
remove
{
m_ShowEntityUpdateEventHandler -= value;
}
}
///
/// 显示实体时加载依赖资源事件。
///
public event EventHandler ShowEntityDependencyAsset
{
add
{
m_ShowEntityDependencyAssetEventHandler += value;
}
remove
{
m_ShowEntityDependencyAssetEventHandler -= value;
}
}
///
/// 隐藏实体完成事件。
///
public event EventHandler HideEntityComplete
{
add
{
m_HideEntityCompleteEventHandler += value;
}
remove
{
m_HideEntityCompleteEventHandler -= value;
}
}
///
/// 实体管理器轮询。
///
/// 逻辑流逝时间,以秒为单位。
/// 真实流逝时间,以秒为单位。
internal override void Update(float elapseSeconds, float realElapseSeconds)
{
while (m_RecycleQueue.Count > 0)
{
EntityInfo entityInfo = m_RecycleQueue.Dequeue();
IEntity entity = entityInfo.Entity;
EntityGroup entityGroup = (EntityGroup)entity.EntityGroup;
if (entityGroup == null)
{
throw new GameFrameworkException("Entity group is invalid.");
}
entityInfo.Status = EntityStatus.WillRecycle;
entity.OnRecycle();
entityInfo.Status = EntityStatus.Recycled;
entityGroup.UnspawnEntity(entity);
ReferencePool.Release(entityInfo);
}
foreach (KeyValuePair entityGroup in m_EntityGroups)
{
entityGroup.Value.Update(elapseSeconds, realElapseSeconds);
}
}
///
/// 关闭并清理实体管理器。
///
internal override void Shutdown()
{
m_IsShutdown = true;
HideAllLoadedEntities();
m_EntityGroups.Clear();
m_EntitiesBeingLoaded.Clear();
m_EntitiesToReleaseOnLoad.Clear();
m_RecycleQueue.Clear();
}
///
/// 设置对象池管理器。
///
/// 对象池管理器。
public void SetObjectPoolManager(IObjectPoolManager objectPoolManager)
{
if (objectPoolManager == null)
{
throw new GameFrameworkException("Object pool manager is invalid.");
}
m_ObjectPoolManager = objectPoolManager;
}
///
/// 设置资源管理器。
///
/// 资源管理器。
public void SetResourceManager(IResourceManager resourceManager)
{
if (resourceManager == null)
{
throw new GameFrameworkException("Resource manager is invalid.");
}
m_ResourceManager = resourceManager;
}
///
/// 设置实体辅助器。
///
/// 实体辅助器。
public void SetEntityHelper(IEntityHelper entityHelper)
{
if (entityHelper == null)
{
throw new GameFrameworkException("Entity helper is invalid.");
}
m_EntityHelper = entityHelper;
}
///
/// 是否存在实体组。
///
/// 实体组名称。
/// 是否存在实体组。
public bool HasEntityGroup(string entityGroupName)
{
if (string.IsNullOrEmpty(entityGroupName))
{
throw new GameFrameworkException("Entity group name is invalid.");
}
return m_EntityGroups.ContainsKey(entityGroupName);
}
///
/// 获取实体组。
///
/// 实体组名称。
/// 要获取的实体组。
public IEntityGroup GetEntityGroup(string entityGroupName)
{
if (string.IsNullOrEmpty(entityGroupName))
{
throw new GameFrameworkException("Entity group name is invalid.");
}
EntityGroup entityGroup = null;
if (m_EntityGroups.TryGetValue(entityGroupName, out entityGroup))
{
return entityGroup;
}
return null;
}
///
/// 获取所有实体组。
///
/// 所有实体组。
public IEntityGroup[] GetAllEntityGroups()
{
int index = 0;
IEntityGroup[] results = new IEntityGroup[m_EntityGroups.Count];
foreach (KeyValuePair entityGroup in m_EntityGroups)
{
results[index++] = entityGroup.Value;
}
return results;
}
///
/// 获取所有实体组。
///
/// 所有实体组。
public void GetAllEntityGroups(List results)
{
if (results == null)
{
throw new GameFrameworkException("Results is invalid.");
}
results.Clear();
foreach (KeyValuePair entityGroup in m_EntityGroups)
{
results.Add(entityGroup.Value);
}
}
///
/// 增加实体组。
///
/// 实体组名称。
/// 实体实例对象池自动释放可释放对象的间隔秒数。
/// 实体实例对象池容量。
/// 实体实例对象池对象过期秒数。
/// 实体实例对象池的优先级。
/// 实体组辅助器。
/// 是否增加实体组成功。
public bool AddEntityGroup(string entityGroupName, float instanceAutoReleaseInterval, int instanceCapacity, float instanceExpireTime, int instancePriority, IEntityGroupHelper entityGroupHelper)
{
if (string.IsNullOrEmpty(entityGroupName))
{
throw new GameFrameworkException("Entity group name is invalid.");
}
if (entityGroupHelper == null)
{
throw new GameFrameworkException("Entity group helper is invalid.");
}
if (m_ObjectPoolManager == null)
{
throw new GameFrameworkException("You must set object pool manager first.");
}
if (HasEntityGroup(entityGroupName))
{
return false;
}
m_EntityGroups.Add(entityGroupName, new EntityGroup(entityGroupName, instanceAutoReleaseInterval, instanceCapacity, instanceExpireTime, instancePriority, entityGroupHelper, m_ObjectPoolManager));
return true;
}
///
/// 是否存在实体。
///
/// 实体编号。
/// 是否存在实体。
public bool HasEntity(int entityId)
{
return m_EntityInfos.ContainsKey(entityId);
}
///
/// 是否存在实体。
///
/// 实体资源名称。
/// 是否存在实体。
public bool HasEntity(string entityAssetName)
{
if (string.IsNullOrEmpty(entityAssetName))
{
throw new GameFrameworkException("Entity asset name is invalid.");
}
foreach (KeyValuePair entityInfo in m_EntityInfos)
{
if (entityInfo.Value.Entity.EntityAssetName == entityAssetName)
{
return true;
}
}
return false;
}
///
/// 获取实体。
///
/// 实体编号。
/// 要获取的实体。
public IEntity GetEntity(int entityId)
{
EntityInfo entityInfo = GetEntityInfo(entityId);
if (entityInfo == null)
{
return null;
}
return entityInfo.Entity;
}
///
/// 获取实体。
///
/// 实体资源名称。
/// 要获取的实体。
public IEntity GetEntity(string entityAssetName)
{
if (string.IsNullOrEmpty(entityAssetName))
{
throw new GameFrameworkException("Entity asset name is invalid.");
}
foreach (KeyValuePair entityInfo in m_EntityInfos)
{
if (entityInfo.Value.Entity.EntityAssetName == entityAssetName)
{
return entityInfo.Value.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 (KeyValuePair entityInfo in m_EntityInfos)
{
if (entityInfo.Value.Entity.EntityAssetName == entityAssetName)
{
results.Add(entityInfo.Value.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 (KeyValuePair entityInfo in m_EntityInfos)
{
if (entityInfo.Value.Entity.EntityAssetName == entityAssetName)
{
results.Add(entityInfo.Value.Entity);
}
}
}
///
/// 获取所有已加载的实体。
///
/// 所有已加载的实体。
public IEntity[] GetAllLoadedEntities()
{
int index = 0;
IEntity[] results = new IEntity[m_EntityInfos.Count];
foreach (KeyValuePair entityInfo in m_EntityInfos)
{
results[index++] = entityInfo.Value.Entity;
}
return results;
}
///
/// 获取所有已加载的实体。
///
/// 所有已加载的实体。
public void GetAllLoadedEntities(List results)
{
if (results == null)
{
throw new GameFrameworkException("Results is invalid.");
}
results.Clear();
foreach (KeyValuePair entityInfo in m_EntityInfos)
{
results.Add(entityInfo.Value.Entity);
}
}
///
/// 获取所有正在加载实体的编号。
///
/// 所有正在加载实体的编号。
public int[] GetAllLoadingEntityIds()
{
int index = 0;
int[] results = new int[m_EntitiesBeingLoaded.Count];
foreach (KeyValuePair entityBeingLoaded in m_EntitiesBeingLoaded)
{
results[index++] = entityBeingLoaded.Key;
}
return results;
}
///
/// 获取所有正在加载实体的编号。
///
/// 所有正在加载实体的编号。
public void GetAllLoadingEntityIds(List results)
{
if (results == null)
{
throw new GameFrameworkException("Results is invalid.");
}
results.Clear();
foreach (KeyValuePair entityBeingLoaded in m_EntitiesBeingLoaded)
{
results.Add(entityBeingLoaded.Key);
}
}
///
/// 是否正在加载实体。
///
/// 实体编号。
/// 是否正在加载实体。
public bool IsLoadingEntity(int entityId)
{
return m_EntitiesBeingLoaded.ContainsKey(entityId);
}
///
/// 是否是合法的实体。
///
/// 实体。
/// 实体是否合法。
public bool IsValidEntity(IEntity entity)
{
if (entity == null)
{
return false;
}
return HasEntity(entity.Id);
}
///
/// 显示实体。
///
/// 实体编号。
/// 实体资源名称。
/// 实体组名称。
public void ShowEntity(int entityId, string entityAssetName, string entityGroupName)
{
ShowEntity(entityId, entityAssetName, entityGroupName, Constant.DefaultPriority, null);
}
///
/// 显示实体。
///
/// 实体编号。
/// 实体资源名称。
/// 实体组名称。
/// 加载实体资源的优先级。
public void ShowEntity(int entityId, string entityAssetName, string entityGroupName, int priority)
{
ShowEntity(entityId, entityAssetName, entityGroupName, priority, null);
}
///
/// 显示实体。
///
/// 实体编号。
/// 实体资源名称。
/// 实体组名称。
/// 用户自定义数据。
public void ShowEntity(int entityId, string entityAssetName, string entityGroupName, object userData)
{
ShowEntity(entityId, entityAssetName, entityGroupName, Constant.DefaultPriority, userData);
}
///
/// 显示实体。
///
/// 实体编号。
/// 实体资源名称。
/// 实体组名称。
/// 加载实体资源的优先级。
/// 用户自定义数据。
public void ShowEntity(int entityId, string entityAssetName, string entityGroupName, int priority, object userData)
{
if (m_ResourceManager == null)
{
throw new GameFrameworkException("You must set resource manager first.");
}
if (m_EntityHelper == null)
{
throw new GameFrameworkException("You must set entity helper first.");
}
if (string.IsNullOrEmpty(entityAssetName))
{
throw new GameFrameworkException("Entity asset name is invalid.");
}
if (string.IsNullOrEmpty(entityGroupName))
{
throw new GameFrameworkException("Entity group name is invalid.");
}
if (HasEntity(entityId))
{
throw new GameFrameworkException(Utility.Text.Format("Entity id '{0}' is already exist.", entityId));
}
if (IsLoadingEntity(entityId))
{
throw new GameFrameworkException(Utility.Text.Format("Entity '{0}' is already being loaded.", entityId));
}
EntityGroup entityGroup = (EntityGroup)GetEntityGroup(entityGroupName);
if (entityGroup == null)
{
throw new GameFrameworkException(Utility.Text.Format("Entity group '{0}' is not exist.", entityGroupName));
}
EntityInstanceObject entityInstanceObject = entityGroup.SpawnEntityInstanceObject(entityAssetName);
if (entityInstanceObject == null)
{
int serialId = ++m_Serial;
m_EntitiesBeingLoaded.Add(entityId, serialId);
m_ResourceManager.LoadAsset(entityAssetName, priority, m_LoadAssetCallbacks, ShowEntityInfo.Create(serialId, entityId, entityGroup, userData));
return;
}
InternalShowEntity(entityId, entityAssetName, entityGroup, entityInstanceObject.Target, false, 0f, userData);
}
///
/// 隐藏实体。
///
/// 实体编号。
public void HideEntity(int entityId)
{
HideEntity(entityId, null);
}
///
/// 隐藏实体。
///
/// 实体编号。
/// 用户自定义数据。
public void HideEntity(int entityId, object userData)
{
if (IsLoadingEntity(entityId))
{
m_EntitiesToReleaseOnLoad.Add(m_EntitiesBeingLoaded[entityId]);
m_EntitiesBeingLoaded.Remove(entityId);
return;
}
EntityInfo entityInfo = GetEntityInfo(entityId);
if (entityInfo == null)
{
throw new GameFrameworkException(Utility.Text.Format("Can not find entity '{0}'.", entityId));
}
InternalHideEntity(entityInfo, userData);
}
///
/// 隐藏实体。
///
/// 实体。
public void HideEntity(IEntity entity)
{
HideEntity(entity, null);
}
///
/// 隐藏实体。
///
/// 实体。
/// 用户自定义数据。
public void HideEntity(IEntity entity, object userData)
{
if (entity == null)
{
throw new GameFrameworkException("Entity is invalid.");
}
HideEntity(entity.Id, userData);
}
///
/// 隐藏所有已加载的实体。
///
public void HideAllLoadedEntities()
{
HideAllLoadedEntities(null);
}
///
/// 隐藏所有已加载的实体。
///
/// 用户自定义数据。
public void HideAllLoadedEntities(object userData)
{
while (m_EntityInfos.Count > 0)
{
foreach (KeyValuePair entityInfo in m_EntityInfos)
{
InternalHideEntity(entityInfo.Value, userData);
break;
}
}
}
///
/// 隐藏所有正在加载的实体。
///
public void HideAllLoadingEntities()
{
foreach (KeyValuePair entityBeingLoaded in m_EntitiesBeingLoaded)
{
m_EntitiesToReleaseOnLoad.Add(entityBeingLoaded.Value);
}
m_EntitiesBeingLoaded.Clear();
}
///
/// 获取父实体。
///
/// 要获取父实体的子实体的实体编号。
/// 子实体的父实体。
public IEntity GetParentEntity(int childEntityId)
{
EntityInfo childEntityInfo = GetEntityInfo(childEntityId);
if (childEntityInfo == null)
{
throw new GameFrameworkException(Utility.Text.Format("Can not find child entity '{0}'.", childEntityId));
}
return childEntityInfo.ParentEntity;
}
///
/// 获取父实体。
///
/// 要获取父实体的子实体。
/// 子实体的父实体。
public IEntity GetParentEntity(IEntity childEntity)
{
if (childEntity == null)
{
throw new GameFrameworkException("Child entity is invalid.");
}
return GetParentEntity(childEntity.Id);
}
///
/// 获取子实体数量。
///
/// 要获取子实体数量的父实体的实体编号。
/// 子实体数量。
public int GetChildEntityCount(int parentEntityId)
{
EntityInfo parentEntityInfo = GetEntityInfo(parentEntityId);
if (parentEntityInfo == null)
{
throw new GameFrameworkException(Utility.Text.Format("Can not find parent entity '{0}'.", parentEntityId));
}
return parentEntityInfo.ChildEntityCount;
}
///
/// 获取子实体。
///
/// 要获取子实体的父实体的实体编号。
/// 子实体。
public IEntity GetChildEntity(int parentEntityId)
{
EntityInfo parentEntityInfo = GetEntityInfo(parentEntityId);
if (parentEntityInfo == null)
{
throw new GameFrameworkException(Utility.Text.Format("Can not find parent entity '{0}'.", parentEntityId));
}
return parentEntityInfo.GetChildEntity();
}
///
/// 获取子实体。
///
/// 要获取子实体的父实体。
/// 子实体。
public IEntity GetChildEntity(IEntity parentEntity)
{
if (parentEntity == null)
{
throw new GameFrameworkException("Parent entity is invalid.");
}
return GetChildEntity(parentEntity.Id);
}
///
/// 获取所有子实体。
///
/// 要获取所有子实体的父实体的实体编号。
/// 所有子实体。
public IEntity[] GetChildEntities(int parentEntityId)
{
EntityInfo parentEntityInfo = GetEntityInfo(parentEntityId);
if (parentEntityInfo == null)
{
throw new GameFrameworkException(Utility.Text.Format("Can not find parent entity '{0}'.", parentEntityId));
}
return parentEntityInfo.GetChildEntities();
}
///
/// 获取所有子实体。
///
/// 要获取所有子实体的父实体的实体编号。
/// 所有子实体。
public void GetChildEntities(int parentEntityId, List results)
{
EntityInfo parentEntityInfo = GetEntityInfo(parentEntityId);
if (parentEntityInfo == null)
{
throw new GameFrameworkException(Utility.Text.Format("Can not find parent entity '{0}'.", parentEntityId));
}
parentEntityInfo.GetChildEntities(results);
}
///
/// 获取所有子实体。
///
/// 要获取所有子实体的父实体。
/// 所有子实体。
public IEntity[] GetChildEntities(IEntity parentEntity)
{
if (parentEntity == null)
{
throw new GameFrameworkException("Parent entity is invalid.");
}
return GetChildEntities(parentEntity.Id);
}
///
/// 获取所有子实体。
///
/// 要获取所有子实体的父实体。
/// 所有子实体。
public void GetChildEntities(IEntity parentEntity, List results)
{
if (parentEntity == null)
{
throw new GameFrameworkException("Parent entity is invalid.");
}
GetChildEntities(parentEntity.Id, results);
}
///
/// 附加子实体。
///
/// 要附加的子实体的实体编号。
/// 被附加的父实体的实体编号。
public void AttachEntity(int childEntityId, int parentEntityId)
{
AttachEntity(childEntityId, parentEntityId, null);
}
///
/// 附加子实体。
///
/// 要附加的子实体的实体编号。
/// 被附加的父实体的实体编号。
/// 用户自定义数据。
public void AttachEntity(int childEntityId, int parentEntityId, object userData)
{
if (childEntityId == parentEntityId)
{
throw new GameFrameworkException(Utility.Text.Format("Can not attach entity when child entity id equals to parent entity id '{0}'.", parentEntityId));
}
EntityInfo childEntityInfo = GetEntityInfo(childEntityId);
if (childEntityInfo == null)
{
throw new GameFrameworkException(Utility.Text.Format("Can not find child entity '{0}'.", childEntityId));
}
if (childEntityInfo.Status >= EntityStatus.WillHide)
{
throw new GameFrameworkException(Utility.Text.Format("Can not attach entity when child entity status is '{0}'.", childEntityInfo.Status));
}
EntityInfo parentEntityInfo = GetEntityInfo(parentEntityId);
if (parentEntityInfo == null)
{
throw new GameFrameworkException(Utility.Text.Format("Can not find parent entity '{0}'.", parentEntityId));
}
if (parentEntityInfo.Status >= EntityStatus.WillHide)
{
throw new GameFrameworkException(Utility.Text.Format("Can not attach entity when parent entity status is '{0}'.", parentEntityInfo.Status));
}
IEntity childEntity = childEntityInfo.Entity;
IEntity parentEntity = parentEntityInfo.Entity;
DetachEntity(childEntity.Id, userData);
childEntityInfo.ParentEntity = parentEntity;
parentEntityInfo.AddChildEntity(childEntity);
parentEntity.OnAttached(childEntity, userData);
childEntity.OnAttachTo(parentEntity, userData);
}
///
/// 附加子实体。
///
/// 要附加的子实体的实体编号。
/// 被附加的父实体。
public void AttachEntity(int childEntityId, IEntity parentEntity)
{
AttachEntity(childEntityId, parentEntity, null);
}
///
/// 附加子实体。
///
/// 要附加的子实体的实体编号。
/// 被附加的父实体。
/// 用户自定义数据。
public void AttachEntity(int childEntityId, IEntity parentEntity, object userData)
{
if (parentEntity == null)
{
throw new GameFrameworkException("Parent entity is invalid.");
}
AttachEntity(childEntityId, parentEntity.Id, userData);
}
///
/// 附加子实体。
///
/// 要附加的子实体。
/// 被附加的父实体的实体编号。
public void AttachEntity(IEntity childEntity, int parentEntityId)
{
AttachEntity(childEntity, parentEntityId, null);
}
///
/// 附加子实体。
///
/// 要附加的子实体。
/// 被附加的父实体的实体编号。
/// 用户自定义数据。
public void AttachEntity(IEntity childEntity, int parentEntityId, object userData)
{
if (childEntity == null)
{
throw new GameFrameworkException("Child entity is invalid.");
}
AttachEntity(childEntity.Id, parentEntityId, userData);
}
///
/// 附加子实体。
///
/// 要附加的子实体。
/// 被附加的父实体。
public void AttachEntity(IEntity childEntity, IEntity parentEntity)
{
AttachEntity(childEntity, parentEntity, null);
}
///
/// 附加子实体。
///
/// 要附加的子实体。
/// 被附加的父实体。
/// 用户自定义数据。
public void AttachEntity(IEntity childEntity, IEntity parentEntity, object userData)
{
if (childEntity == null)
{
throw new GameFrameworkException("Child entity is invalid.");
}
if (parentEntity == null)
{
throw new GameFrameworkException("Parent entity is invalid.");
}
AttachEntity(childEntity.Id, parentEntity.Id, userData);
}
///
/// 解除子实体。
///
/// 要解除的子实体的实体编号。
public void DetachEntity(int childEntityId)
{
DetachEntity(childEntityId, null);
}
///
/// 解除子实体。
///
/// 要解除的子实体的实体编号。
/// 用户自定义数据。
public void DetachEntity(int childEntityId, object userData)
{
EntityInfo childEntityInfo = GetEntityInfo(childEntityId);
if (childEntityInfo == null)
{
throw new GameFrameworkException(Utility.Text.Format("Can not find child entity '{0}'.", childEntityId));
}
IEntity parentEntity = childEntityInfo.ParentEntity;
if (parentEntity == null)
{
return;
}
EntityInfo parentEntityInfo = GetEntityInfo(parentEntity.Id);
if (parentEntityInfo == null)
{
throw new GameFrameworkException(Utility.Text.Format("Can not find parent entity '{0}'.", parentEntity.Id));
}
IEntity childEntity = childEntityInfo.Entity;
childEntityInfo.ParentEntity = null;
parentEntityInfo.RemoveChildEntity(childEntity);
parentEntity.OnDetached(childEntity, userData);
childEntity.OnDetachFrom(parentEntity, userData);
}
///
/// 解除子实体。
///
/// 要解除的子实体。
public void DetachEntity(IEntity childEntity)
{
DetachEntity(childEntity, null);
}
///
/// 解除子实体。
///
/// 要解除的子实体。
/// 用户自定义数据。
public void DetachEntity(IEntity childEntity, object userData)
{
if (childEntity == null)
{
throw new GameFrameworkException("Child entity is invalid.");
}
DetachEntity(childEntity.Id, userData);
}
///
/// 解除所有子实体。
///
/// 被解除的父实体的实体编号。
public void DetachChildEntities(int parentEntityId)
{
DetachChildEntities(parentEntityId, null);
}
///
/// 解除所有子实体。
///
/// 被解除的父实体的实体编号。
/// 用户自定义数据。
public void DetachChildEntities(int parentEntityId, object userData)
{
EntityInfo parentEntityInfo = GetEntityInfo(parentEntityId);
if (parentEntityInfo == null)
{
throw new GameFrameworkException(Utility.Text.Format("Can not find parent entity '{0}'.", parentEntityId));
}
while (parentEntityInfo.ChildEntityCount > 0)
{
IEntity childEntity = parentEntityInfo.GetChildEntity();
DetachEntity(childEntity.Id, userData);
}
}
///
/// 解除所有子实体。
///
/// 被解除的父实体。
public void DetachChildEntities(IEntity parentEntity)
{
DetachChildEntities(parentEntity, null);
}
///
/// 解除所有子实体。
///
/// 被解除的父实体。
/// 用户自定义数据。
public void DetachChildEntities(IEntity parentEntity, object userData)
{
if (parentEntity == null)
{
throw new GameFrameworkException("Parent entity is invalid.");
}
DetachChildEntities(parentEntity.Id, userData);
}
///
/// 获取实体信息。
///
/// 实体编号。
/// 实体信息。
private EntityInfo GetEntityInfo(int entityId)
{
EntityInfo entityInfo = null;
if (m_EntityInfos.TryGetValue(entityId, out entityInfo))
{
return entityInfo;
}
return null;
}
private void InternalShowEntity(int entityId, string entityAssetName, EntityGroup entityGroup, object entityInstance, bool isNewInstance, float duration, object userData)
{
try
{
IEntity entity = m_EntityHelper.CreateEntity(entityInstance, entityGroup, userData);
if (entity == null)
{
throw new GameFrameworkException("Can not create entity in entity helper.");
}
EntityInfo entityInfo = EntityInfo.Create(entity);
m_EntityInfos.Add(entityId, entityInfo);
entityInfo.Status = EntityStatus.WillInit;
entity.OnInit(entityId, entityAssetName, entityGroup, isNewInstance, userData);
entityInfo.Status = EntityStatus.Inited;
entityGroup.AddEntity(entity);
entityInfo.Status = EntityStatus.WillShow;
entity.OnShow(userData);
entityInfo.Status = EntityStatus.Showed;
if (m_ShowEntitySuccessEventHandler != null)
{
ShowEntitySuccessEventArgs showEntitySuccessEventArgs = ShowEntitySuccessEventArgs.Create(entity, duration, userData);
m_ShowEntitySuccessEventHandler(this, showEntitySuccessEventArgs);
ReferencePool.Release(showEntitySuccessEventArgs);
}
}
catch (Exception exception)
{
if (m_ShowEntityFailureEventHandler != null)
{
ShowEntityFailureEventArgs showEntityFailureEventArgs = ShowEntityFailureEventArgs.Create(entityId, entityAssetName, entityGroup.Name, exception.ToString(), userData);
m_ShowEntityFailureEventHandler(this, showEntityFailureEventArgs);
ReferencePool.Release(showEntityFailureEventArgs);
return;
}
throw;
}
}
private void InternalHideEntity(EntityInfo entityInfo, object userData)
{
while (entityInfo.ChildEntityCount > 0)
{
IEntity childEntity = entityInfo.GetChildEntity();
HideEntity(childEntity.Id, userData);
}
if (entityInfo.Status == EntityStatus.Hidden)
{
return;
}
IEntity entity = entityInfo.Entity;
DetachEntity(entity.Id, userData);
entityInfo.Status = EntityStatus.WillHide;
entity.OnHide(m_IsShutdown, userData);
entityInfo.Status = EntityStatus.Hidden;
EntityGroup entityGroup = (EntityGroup)entity.EntityGroup;
if (entityGroup == null)
{
throw new GameFrameworkException("Entity group is invalid.");
}
entityGroup.RemoveEntity(entity);
if (!m_EntityInfos.Remove(entity.Id))
{
throw new GameFrameworkException("Entity info is unmanaged.");
}
if (m_HideEntityCompleteEventHandler != null)
{
HideEntityCompleteEventArgs hideEntityCompleteEventArgs = HideEntityCompleteEventArgs.Create(entity.Id, entity.EntityAssetName, entityGroup, userData);
m_HideEntityCompleteEventHandler(this, hideEntityCompleteEventArgs);
ReferencePool.Release(hideEntityCompleteEventArgs);
}
m_RecycleQueue.Enqueue(entityInfo);
}
private void LoadAssetSuccessCallback(string entityAssetName, object entityAsset, float duration, object userData)
{
ShowEntityInfo showEntityInfo = (ShowEntityInfo)userData;
if (showEntityInfo == null)
{
throw new GameFrameworkException("Show entity info is invalid.");
}
if (m_EntitiesToReleaseOnLoad.Contains(showEntityInfo.SerialId))
{
m_EntitiesToReleaseOnLoad.Remove(showEntityInfo.SerialId);
ReferencePool.Release(showEntityInfo);
m_EntityHelper.ReleaseEntity(entityAsset, null);
return;
}
m_EntitiesBeingLoaded.Remove(showEntityInfo.EntityId);
EntityInstanceObject entityInstanceObject = EntityInstanceObject.Create(entityAssetName, entityAsset, m_EntityHelper.InstantiateEntity(entityAsset), m_EntityHelper);
showEntityInfo.EntityGroup.RegisterEntityInstanceObject(entityInstanceObject, true);
InternalShowEntity(showEntityInfo.EntityId, entityAssetName, showEntityInfo.EntityGroup, entityInstanceObject.Target, true, duration, showEntityInfo.UserData);
ReferencePool.Release(showEntityInfo);
}
private void LoadAssetFailureCallback(string entityAssetName, LoadResourceStatus status, string errorMessage, object userData)
{
ShowEntityInfo showEntityInfo = (ShowEntityInfo)userData;
if (showEntityInfo == null)
{
throw new GameFrameworkException("Show entity info is invalid.");
}
if (m_EntitiesToReleaseOnLoad.Contains(showEntityInfo.SerialId))
{
m_EntitiesToReleaseOnLoad.Remove(showEntityInfo.SerialId);
return;
}
m_EntitiesBeingLoaded.Remove(showEntityInfo.EntityId);
string appendErrorMessage = Utility.Text.Format("Load entity failure, asset name '{0}', status '{1}', error message '{2}'.", entityAssetName, status, errorMessage);
if (m_ShowEntityFailureEventHandler != null)
{
ShowEntityFailureEventArgs showEntityFailureEventArgs = ShowEntityFailureEventArgs.Create(showEntityInfo.EntityId, entityAssetName, showEntityInfo.EntityGroup.Name, appendErrorMessage, showEntityInfo.UserData);
m_ShowEntityFailureEventHandler(this, showEntityFailureEventArgs);
ReferencePool.Release(showEntityFailureEventArgs);
return;
}
throw new GameFrameworkException(appendErrorMessage);
}
private void LoadAssetUpdateCallback(string entityAssetName, float progress, object userData)
{
ShowEntityInfo showEntityInfo = (ShowEntityInfo)userData;
if (showEntityInfo == null)
{
throw new GameFrameworkException("Show entity info is invalid.");
}
if (m_ShowEntityUpdateEventHandler != null)
{
ShowEntityUpdateEventArgs showEntityUpdateEventArgs = ShowEntityUpdateEventArgs.Create(showEntityInfo.EntityId, entityAssetName, showEntityInfo.EntityGroup.Name, progress, showEntityInfo.UserData);
m_ShowEntityUpdateEventHandler(this, showEntityUpdateEventArgs);
ReferencePool.Release(showEntityUpdateEventArgs);
}
}
private void LoadAssetDependencyAssetCallback(string entityAssetName, string dependencyAssetName, int loadedCount, int totalCount, object userData)
{
ShowEntityInfo showEntityInfo = (ShowEntityInfo)userData;
if (showEntityInfo == null)
{
throw new GameFrameworkException("Show entity info is invalid.");
}
if (m_ShowEntityDependencyAssetEventHandler != null)
{
ShowEntityDependencyAssetEventArgs showEntityDependencyAssetEventArgs = ShowEntityDependencyAssetEventArgs.Create(showEntityInfo.EntityId, entityAssetName, showEntityInfo.EntityGroup.Name, dependencyAssetName, loadedCount, totalCount, showEntityInfo.UserData);
m_ShowEntityDependencyAssetEventHandler(this, showEntityDependencyAssetEventArgs);
ReferencePool.Release(showEntityDependencyAssetEventArgs);
}
}
}
}