Exemple #1
0
        /*
         * @brief 創建實體
         */
        public T CreateEntity()
        {
            T entity;

            if (EntitiesPoolCount > 0)
            {
                // 復用
                entity = entitiesPool.Pop();
                entity.Init(entitySerialId++);
            }
            else
            {
                // 新創
                entity = entityFactory();
                entity.Init(entitySerialId++, entityMaxComponents, componentsPools, debugInfo);
            }

            //
            entities.Add(entity);
            entity.AddRef(this);
            entitiesCache = null;

            //
            entity.OnComponentAdd    += OnEntityComponentAddOrRemoveHandler;
            entity.OnComponentRemove += OnEntityComponentAddOrRemoveHandler;
            entity.OnComponentUpdate += OnEntityComponentUpdateHandler;
            entity.OnEntityRelease   += OnEntityReleaseHandler;
            entity.OnEntityDestroy   += OnEntityDestroyHandler;

            //
            OnEntityCreate?.Invoke(this, entity);

            return(entity);
        }
Exemple #2
0
        public Entity Create(Entity prototype = null, Entity previous = null)
        {
            var newEntity = new Entity
            {
                Ecs       = Ecs,
                Prototype = prototype,
                Name      = prototype?.Name,
            };

            if (prototype != null)
            {
                foreach (var c in prototype.Components)
                {
                    var previousComponent = previous?.Components.FirstOrDefault(pc => c.GetType() == pc.GetType());
                    newEntity.AddComponent((previousComponent ?? c).Clone());
                }
            }

            Destroy(previous);

            Entities.Add(newEntity);
            OnEntityCreate?.Invoke(newEntity);

            return(newEntity);
        }