/// Destroys the entity, removes all its components and pushs it back /// to the internal ObjectPool for entities. private void DestroyEntity(Entity entity) { var removed = _entities.Remove(entity); if (!removed) { throw new ContextDoesNotContainEntityException( "'" + this + "' cannot destroy " + entity + "!", "This cannot happen!?!" ); } _entitiesCache = null; if (OnEntityWillBeDestroyed != null) { OnEntityWillBeDestroyed(this, entity); } entity.InternalDestroy(); 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); } }