createEntityWith method

int createEntityWith(
  1. List<Component> components
)

Implementation

int createEntityWith(List<Component> components) {
  final entityID = createEntity();
  final componentIDs = <ComponentID>[];

  for (final component in components) {
    component.entityID = entityID;
    final id = _getOrCreateComponentID(component.runtimeType);
    componentIDs.add(id);
    _componentIndex[id] ??= {};
  }

  final hash = SetHash(componentIDs);
  final archetype = _getOrCreateArchetype(hash);

  for (int i = 0; i < components.length; i++) {
    final componentID = componentIDs[i];
    final component = components[i];

    final indexMap = _componentIndex[componentID]!;
    final col = indexMap[hash] ??= archetype.components.length;
    if (col == archetype.components.length) {
      archetype.components.add([]);
    }
    archetype.components[col].add(component);
  }

  _entityIndex[entityID] = Record(
    archetype: archetype,
    entityRow: archetype.components[0].length - 1,
  );

  return entityID;
}