JaimeGensler / thyseus

An archetypal Entity Component System, built entirely in Typescript
MIT License
74 stars 3 forks source link

[FEAT] `Query.p.get()` #45

Closed JaimeGensler closed 9 months ago

JaimeGensler commented 1 year ago

Describe the problem this feature solves

Getting components from a query for a specific entity should be possible. This is especially useful when dealing with multiple queries:

export function moveCar(
    cars: Query<Car>,
    carPhysics: Query<[Entity, Transform], With<CarPhysics>>,
    carVisual: Query<[Entity, Transform], With<CarVisuals>>
) {
    for (const car of cars) {
        for (const [physicsEntity, physicsTransform] of carPhysics) {
            if (car.physicsId === entity.id) {
                for (const [visualEntity, visualTransform] of carVisual) {
                    if (car.visualId == visualEntity.id) {
                      // holy nesting!
                    }
                }
            }
        }
    }
}

could instead be

export function moveCar(
    cars: Query<Car>,
    carPhysics: Query<[Entity, Transform], With<CarPhysics>>,
    carVisual: Query<[Entity, Transform], With<CarVisuals>>
) {
    for (const car of cars) {
      const [physicsEntity, physicsTransform] = carPhysics.get(car.physicsId);
      const [visualEntity, visualTransform] = carVisual.get(car.visualId);
      // Much nicer!
    }
}

The question of what happens when you call .get() with an entity that isn't in the query needs answering.

Queries will also need to know which group in the vector corresponds to which table.