addComponentToEntity<T extends Component<V>, V> method

void addComponentToEntity<T extends Component<V>, V>(
  1. Entity entity,
  2. V? data
)

Add given component to an entity.

If the entity already has that component it will just return.

The data argument has to be of the type V.

Implementation

void addComponentToEntity<T extends Component<V>, V>(Entity entity, V? data) {
  assert(T != Component, 'An implemented Component was expected');
  assert(
    world.componentManager.components.contains(T),
    'Component $T has not been registered to the World',
  );

  if (entity._componentTypes.contains(T)) {
    return; // Entity already has an instance of the component.
  }

  final componentPool = world.componentManager.getComponentPool<T, V>();
  final component = componentPool.acquire(data);

  entity._componentTypes.add(T);
  entity._components[T] = component;
  _queryManager._onComponentAddedToEntity(entity, T);
}