Skip to content

World and Entities

Giacomo Cavalieri edited this page Sep 30, 2021 · 10 revisions

A World is an entities container.

Creating a world

To create a World you can simply write:

object Example with ECScalaDSL {
  val world = World()
}

Adding entities to a world

Once you have a World you can add an Entity to it with the following syntax:

object Example with ECScalaDSL {
  val world = World()
  val addedEntity = world hasAn entity
  // or val addedEntity = world.createEntity()
}

Removing entities from a world

Entities can also be removed from the World they belong to:

object Example with ECScalaDSL {
  val world = World()
  val addedEntity = world hasAn entity
  remove (addedEntity) from world
  // or world removeEntity addedEntity
  // or world -= addedEntity
}

Getting a view on a world

Views are basically a way to iterate over the entities contained in a world. A view has to specify the CList of components required by an entity that will be included in the view, e.g.

object Example with ECScalaDSL {
  val world = World()
  val entity1 = world hasAn entity withComponent Position(1, 1)
  val entity2 = world hasAn entity withComponent Color(255, 0, 0)
  val view = world.getView[Position &: CNil]
  // view contains entity1 since it has a Position component
  // it does not contain entity2 since it does not have a Position component
  val emptyView = world.getView[Position &: Color &: CNil]
  // emptyView is an empty iterator since no component has both a Position
  // and Color component
}
Clone this wiki locally