Exemple #1
0
        /// Destroys the entity, removes all its components and pushs it back
        /// to the internal ObjectPool for entities.
        public virtual void DestroyEntity(Entity entity)
        {
            var removed = _entities.Remove(entity);

            if (!removed)
            {
                throw new PoolDoesNotContainEntityException(
                          "'" + this + "' cannot destroy " + entity + "!",
                          "Did you call pool.DestroyEntity() on a wrong pool?"
                          );
            }
            _entitiesCache = null;

            if (OnEntityWillBeDestroyed != null)
            {
                OnEntityWillBeDestroyed(this, entity);
            }

            entity.destroy();

            if (OnEntityDestroyed != null)
            {
                OnEntityDestroyed(this, entity);
            }

            if (entity.retainCount == 1)
            {
                // Can be released immediately without
                // adding to _retainedEntities
                entity.OnEntityReleased -= _cachedEntityReleased;
                _reusableEntities.Push(entity);
                entity.Release(this);
                entity.removeAllOnEntityReleasedHandlers();
            }
            else
            {
                _retainedEntities.Add(entity);
                entity.Release(this);
            }
        }