hmans / miniplex

A šŸ‘©ā€šŸ’» developer-friendly entity management system for šŸ•¹ games and similarly demanding applications, based on šŸ›  ECS architecture.
MIT License
854 stars 39 forks source link

Best way to represent multiple entities? #302

Closed LukeWood closed 1 year ago

LukeWood commented 1 year ago

First of all, great work on miniplex. It is awesome.

Second, how do we represent multiple types of entities? Do we just do:

type PlayerEntity {
    position: Vector3;
}

type Terrain {
    position: Vector3;
    vertices: Vector3[];
    terrain: boolean;
}
ecs = new World<PlayerEntity | Terrain>()

Is that how we're meant to do these sorts of things? Then...

const terrains = ecs.with("vertices", "position", "terrain")
verybomb commented 1 year ago

I might be misinterpreting your example but iirc you're supposed to define the ecs state like this:

interface State {
  position?: Vector3 // shared component between player & terrain etc
  velocity?: Vector3
  player?: boolean
  terrain?: {
    // terrain properties (idk if vertices is supposed to be unique to the terrain component)
    vertices: Vector3
  }
}

const ecs = new World<State>()

const players = ecs.with('position', 'player')
const terrains = ecs.with('position', 'terrain')

You can also chain archetypes:

const movableObjects = ecs.with('position', 'velocity')
const players = movableObjects.with('player')
LukeWood commented 1 year ago

Ok Iā€™m understanding much better now. I think I was missing a core concept of ECS