Exemplo n.º 1
0
        private void deleteComponent(ECSBaseComponent component)
        {
            if (component == null)
            {
                throw new ArgumentNullException("component");
            }
            ECSEntity entity               = component.Entity;
            int       entityId             = entity.Id;
            var       componentsOfSameType = this.getComponentsOfType(component.GetType());
            int       index = componentsOfSameType.LinearSearchNullSafe
                              (
                delegate(object element)
            {
                return(((ECSBaseComponent)element).Entity.Id - entityId);
            }
                              );

            // Debug.LogFormat ("DELETE {0}: {1}", component.GetType().Name, debugComponentArrayToString (componentsOfSameType));

            if (index < 0)
            {
                Debug.LogErrorFormat("Entity component of type {0} wasn't found for entity {1}", component.GetType().Name, component.Entity.Id);
                return;
            }
            if (!object.ReferenceEquals(entity, ((ECSBaseComponent)componentsOfSameType[index]).Entity))
            {
                throw new InvalidOperationException("Internal Error: component-entity pairing is messed up");
            }
            if (!object.ReferenceEquals(component, componentsOfSameType[index]))
            {
                throw new InvalidOperationException("Component found was not the component in the record; are you adding dupliates?");
            }
            componentsOfSameType[index] = null;
            _anyComponentsWereRemoved   = true;
        }
Exemplo n.º 2
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);
 }
Exemplo n.º 3
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);
        }
Exemplo n.º 4
0
        private void debugVerifyEntityId(ECSEntity entity)
        {
            int id = entity.Id;

            if (id < 0 ||
                id >= _entities.Count ||
                !object.ReferenceEquals(entity, _entities[id]))
            {
                throw new InvalidOperationException("Internal Error: Recycled entity doesn't have its old ID");
            }
        }
Exemplo n.º 5
0
        private void insertComponentsOfEntity(ECSEntity entity)
        {
            var components = entity.GetComponents();

            for (int i = 0; i < components.Length; ++i)
            {
                this.insertComponent(components[i]);
            }
#if UNITY_EDITOR
            this.debugAssertInvariants();
#endif
        }
Exemplo n.º 6
0
        public ECSEntity GetEntity(int id)
        {
            if (id < 0 || id >= _entities.Count)
            {
                throw new ArgumentException("Invalid Entity ID");
            }
            ECSEntity retval = (ECSEntity)_entities[id];

            if (retval.Id != id)
            {
                throw new ArgumentException("Entity is released");
            }
            return(retval);
        }
Exemplo n.º 7
0
        public ECSBaseComponent AcquireComponent(ECSEntity entity, Type componentType)
        {
            if (entity == null || componentType == null)
            {
                throw new ArgumentNullException();
            }
            if (!typeof(ECSBaseComponent).IsAssignableFrom(componentType))
            {
                throw new ArgumentException("Component type " + componentType.Name + " must derive from ECSBaseComponent");
            }
            this.debugVerifyEntityId(entity);
            if (null != entity.GetComponent(componentType))
            {
                throw new ArgumentException("Component type " + componentType.Name + " already exists on entity " + entity.Id);
            }
            var component = (ECSBaseComponent)entity.gameObject.AddComponent(componentType);

            component.Entity = entity;
            if (_isIterating)
            {
                if (!_entitiesCreatedDuringIteration.Contains(component.Entity))
                {
                    _componentsAcquiredDuringIteration.Add(component);
                }
            }
            else
            {
                this.insertComponent(component);
            }
            this.debugAssertInvariants();
            var acquireMethod = componentType.GetMethod("Acquire", Type.EmptyTypes);

            if (acquireMethod != null)
            {
                acquireMethod.Invoke(component, null);
            }
            return(component);
        }
Exemplo n.º 8
0
 public void Release(ECSEntity entity)
 {
     if (entity == null)
     {
         throw new ArgumentNullException("entity");
     }
     this.debugVerifyEntityId(entity);
     entity.gameObject.SendMessage("Release", null, SendMessageOptions.DontRequireReceiver);
     ECSBaseComponent[] components = entity.GetComponents();
     for (int i = 0; i < components.Length; ++i)
     {
         this.deleteComponent(components[i]);
     }
     if (entity.Pool == null)
     {
         GameObject.Destroy(entity.gameObject);
         _entities[entity.Id] = null;
     }
     else
     {
         entity.Pool.Release(entity.gameObject);
         entity.Id = reverseEntityId(entity.Id);
     }
 }
Exemplo n.º 9
0
 public ECSBaseComponent AcquireComponent(ECSEntity entity, Type componentType)
 {
     return(this.Controller.AcquireComponent(entity, componentType));
 }
Exemplo n.º 10
0
 public void Release(ECSEntity instance)
 {
     this.Controller.Release(instance);
 }