Ejemplo n.º 1
0
 private void configureEntity(ECSEntity entity, ECSEntityPool pool, bool isNew)
 {
     if (entity == null)
     {
         throw new ArgumentNullException("entity");
     }
     if (isNew)
     {
         var components = entity.GetComponents();
         for (int i = 0; i < components.Length; ++i)
         {
             components[i].Entity = entity;
         }
         entity.Id   = _entities.Count;
         entity.Pool = pool;
         _entities.Add(entity);
     }
     else
     {
         entity.Id = reverseEntityId(entity.Id);
     }
     this.debugVerifyEntityId(entity);
     if (_isIterating)
     {
         _entitiesCreatedDuringIteration.Add(entity);
     }
     else
     {
         this.insertComponentsOfEntity(entity);
     }
     entity.gameObject.SendMessage("Acquire", null, SendMessageOptions.DontRequireReceiver);
 }
Ejemplo n.º 2
0
        public ECSEntity Acquire(GameObject prefab, Transform parent)
        {
            if (prefab == null)
            {
                throw new ArgumentNullException();
            }
            var prefabEntity = (ECSEntity)prefab.GetComponent(typeof(ECSEntity));

            if (prefabEntity == null)
            {
                prefabEntity    = (ECSEntity)prefab.AddComponent(typeof(ECSEntity));
                prefabEntity.Id = int.MinValue;
            }
            if (prefabEntity.Pool == null)
            {
                prefabEntity.Pool = ECSEntityPool.Create(prefab);
            }
            bool       isNew;
            GameObject prefabCopy = prefabEntity.Pool.Acquire(parent, out isNew);
            ECSEntity  entity     = (ECSEntity)prefabCopy.GetComponent(typeof(ECSEntity));

            if (entity == null)
            {
                throw new InvalidOperationException("Internal Error: Clone of entity prefab doesn't have an ECSEntity component");
            }
            if (entity.IdIsValid)
            {
                throw new InvalidOperationException("Internal Error: Entity acquired from pool with valid ID. Possibly acquired an existing object.");
            }
            this.configureEntity(entity, prefabEntity.Pool, isNew);
            return(entity);
        }