Beispiel #1
0
        private void InternalHideEntity(EntityInfo entityInfo, object userData)
        {
            Entity entity = entityInfo.Entity;

            Entity[] childEntities = entityInfo.GetChildEntities();
            foreach (Entity childEntity in childEntities)
            {
                HideEntity(childEntity.Id, userData);
            }

            if (entityInfo.Status == EntityStatus.Hidden)
            {
                return;
            }

            entityInfo.Status = EntityStatus.WillHide;
            entity.OnHide(m_IsShutdown, userData);
            entityInfo.Status = EntityStatus.Hidden;

            EntityGroup entityGroup = (EntityGroup)entity.EntityGroup;

            if (entityGroup == null)
            {
                throw new LufyException("Entity group is invalid.");
            }

            entityGroup.RemoveEntity(entity);
            if (!m_EntityInfos.Remove(entity.Id))
            {
                throw new LufyException("Entity info is unmanaged.");
            }

            m_RecycleQueue.Enqueue(entityInfo);
        }
Beispiel #2
0
 public void Clear()
 {
     m_SerialId        = 0;
     m_EntityId        = 0;
     m_EntityGroup     = null;
     m_UserData        = null;
     m_EntityLogicType = null;
 }
Beispiel #3
0
 public ShowEntityInfo()
 {
     m_SerialId        = 0;
     m_EntityId        = 0;
     m_EntityGroup     = null;
     m_UserData        = null;
     m_EntityLogicType = null;
 }
Beispiel #4
0
        public static ShowEntityInfo Create(int serialId, int entityId, EntityGroup entityGroup, Type logicType, object userData)
        {
            ShowEntityInfo showEntityInfo = ReferencePool.Acquire <ShowEntityInfo>();

            showEntityInfo.m_SerialId        = serialId;
            showEntityInfo.m_EntityId        = entityId;
            showEntityInfo.m_EntityGroup     = entityGroup;
            showEntityInfo.m_UserData        = userData;
            showEntityInfo.m_EntityLogicType = logicType;
            return(showEntityInfo);
        }
Beispiel #5
0
        /// <summary>
        /// 获取实体组。
        /// </summary>
        /// <param name="entityGroupName">实体组名称。</param>
        /// <returns>要获取的实体组。</returns>
        private EntityGroup GetEntityGroup(string entityGroupName)
        {
            if (string.IsNullOrEmpty(entityGroupName))
            {
                throw new LufyException("Entity group name is invalid.");
            }

            EntityGroup entityGroup = null;

            if (m_EntityGroups.TryGetValue(entityGroupName, out entityGroup))
            {
                return(entityGroup);
            }

            return(null);
        }
Beispiel #6
0
        /// <summary>
        /// 创建实体。
        /// </summary>
        /// <param name="entityInstance">实体实例。</param>
        /// <param name="entityGroup">实体所属的实体组。</param>
        /// <param name="userData">用户自定义数据。</param>
        /// <returns>实体。</returns>
        private Entity CreateEntity(object entityInstance, EntityGroup entityGroup, object userData)
        {
            GameObject gameObject = entityInstance as GameObject;

            if (gameObject == null)
            {
                Log.Error("Entity instance is invalid.");
                return(null);
            }

            Transform transform = gameObject.transform;

            transform.SetParent(entityGroup.RootInstance.transform);

            return(gameObject.GetOrAddComponent <Entity>());
        }
Beispiel #7
0
        /// <summary>
        /// 显示实体。
        /// </summary>
        /// <param name="entityId">实体编号。</param>
        /// <param name="entityAssetName">实体资源名称。</param>
        /// <param name="entityGroupName">实体组名称。</param>
        /// <param name="userData">用户自定义数据。</param>
        public void ShowEntity(int entityId, string entityAssetName, string entityGroupName, Type logicType, object userData)
        {
            if (m_ResourceManager == null)
            {
                throw new LufyException("You must set resource manager first.");
            }

            if (string.IsNullOrEmpty(entityAssetName))
            {
                throw new LufyException("Entity asset name is invalid.");
            }

            if (string.IsNullOrEmpty(entityGroupName))
            {
                throw new LufyException("Entity group name is invalid.");
            }

            if (HasEntity(entityId))
            {
                throw new LufyException(Utility.Text.Format("Entity id '{0}' is already exist.", entityId.ToString()));
            }

            if (IsLoadingEntity(entityId))
            {
                throw new LufyException(Utility.Text.Format("Entity '{0}' is already being loaded.", entityId.ToString()));
            }

            EntityGroup entityGroup = GetEntityGroup(entityGroupName);

            if (entityGroup == null)
            {
                throw new LufyException(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, m_LoadAssetCallbacks, ShowEntityInfo.Create(serialId, entityId, entityGroup, logicType, userData));
                return;
            }

            InternalShowEntity(entityId, entityAssetName, entityGroup, entityInstanceObject.Target, false, 0f, logicType, userData);
        }
Beispiel #8
0
        private void InternalShowEntity(int entityId, string entityAssetName, EntityGroup entityGroup, object entityInstance, bool isNewInstance, float duration, Type logicType, object userData)
        {
            Entity entity = CreateEntity(entityInstance, entityGroup, userData);

            if (entity == null)
            {
                throw new LufyException("Can not create entity in helper.");
            }

            EntityInfo entityInfo = EntityInfo.Create(entity);

            m_EntityInfos.Add(entityId, entityInfo);
            entityInfo.Status = EntityStatus.WillInit;
            entity.OnInit(entityId, entityAssetName, entityGroup, isNewInstance, logicType, userData);
            entityInfo.Status = EntityStatus.Inited;
            entityGroup.AddEntity(entity);
            entityInfo.Status = EntityStatus.WillShow;
            entity.OnShow(userData);
            entityInfo.Status = EntityStatus.Showed;
        }
Beispiel #9
0
        /// <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, EntityGroup entityGroup, bool isNewInstance, Type logicType, 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;
            }

            if (logicType == null)
            {
                Log.Error("Entity logic type is invalid.");
                return;
            }

            if (m_EntityLogic != null)
            {
                if (m_EntityLogic.GetType() == logicType)
                {
                    m_EntityLogic.enabled = true;
                    return;
                }

                Destroy(m_EntityLogic);
                m_EntityLogic = null;
            }

            m_EntityLogic = gameObject.AddComponent(logicType) as EntityLogic;
            if (m_EntityLogic == null)
            {
                Log.Error("Entity '{0}' can not add entity logic.", entityAssetName);
                return;
            }

            m_EntityLogic.OnInit(userData);
        }
Beispiel #10
0
        internal override void OnUpdate(float elapseSeconds, float realElapseSeconds)
        {
            while (m_RecycleQueue.Count > 0)
            {
                EntityInfo  entityInfo  = m_RecycleQueue.Dequeue();
                Entity      entity      = entityInfo.Entity;
                EntityGroup entityGroup = (EntityGroup)entity.EntityGroup;
                if (entityGroup == null)
                {
                    throw new LufyException("Entity group is invalid.");
                }

                entityInfo.Status = EntityStatus.WillRecycle;
                entity.OnRecycle();
                entityInfo.Status = EntityStatus.Recycled;
                entityGroup.UnspawnEntity(entity);
                ReferencePool.Release(entityInfo);
            }

            foreach (KeyValuePair <string, EntityGroup> entityGroup in m_EntityGroups)
            {
                entityGroup.Value.Update(elapseSeconds, realElapseSeconds);
            }
        }