removeEntity method

bool removeEntity(
  1. I entity
)

Unregisters entity and runs the full remove lifecycle.

Emits EventEntityRemoving first. Either hook returning false cancels the removal and emits EventEntityRemoveCancelled. On success, all components are removed before the entity is detached, and EventEntityRemoved is emitted. Unregistered entities are ignored.

Implementation

bool removeEntity(I entity) {
  if (!entity.isAdded) return false;

  emit(EventEntityRemoving(app, this, entity));

  if (!_doOnBeforeEntityRemove(entity)) {
    emit(EventEntityRemoveCancelled(app, this, entity));
    return false;
  }

  if (!entity._doOnBeforeRemove()) {
    emit(EventEntityRemoveCancelled(app, this, entity));
    return false;
  }

  _doOnEntityRemove(entity);

  entity._doRemove();

  // remove entity components
  // NOTE: toList() is important
  entity._components.toList().forEach(
    (c) => entity._removeComponentInstance(c)
  );

  _entities.remove(entity);

  entity._doOnAfterRemove();
  _doOnAfterEntityRemove(entity);
  emit(EventEntityRemoved(app, this, entity));
  return true;
}