JaimeGensler / thyseus

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

[FEAT] Run conditions #57

Open JaimeGensler opened 11 months ago

JaimeGensler commented 11 months ago

Describe the problem this feature solves

A mechanism to specify whether a system or group of systems should run at all would be nice. You can kind of do this with an early return, but you may have to access data you otherwise don't care about to do this, which isn't great. Also maybe better optimization potential if they exist external to systems.

Describe the solution you'd like to see

For the API, a run condition is a function that returns a boolean - true if the system(s) should run, false if they should not. It will be supplied as the third argument for World.p.addSystems (Schedule addSystem unknown). The function can either be a system, or a function that accepts the world instance. If the getSystemParameters property exists on the function, it will be treated as a system and its arguments will be parsed. If not, it will be supplied the world.

Example usage:

type RunCondition = (...args: any[]) => boolean;
class World {
  addSystems(
    scheduleType: ScheduleType,
    systems: System | System[],
    condition: RunCondition
  ): this
}

// In use
const world = await new World()
  .addSystems(Schedule, mySystem, world => world.getResource(SomeResource).someProperty)
  .prepare()

Open Questions

  1. When should this be evaluated? a. Every runSchedule() call b. When prepare() is called c. A third API, included in prepare() but called separately?