Skip to content

World and Entities

Giacomo Cavalieri edited this page Oct 14, 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.

import dev.atedeg.ecscala.given

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
}

To get a view you can write:

import dev.atedeg.ecscala.given

object Example with ECScalaDSL {
  val world = World()
  val view = getView[Position &: Color &: CNil] from world
  // or world.getView[Position &: Color &: CNil]
}

Views can also specify components that must not belong to entities they iterate over (in that case they are called ExcludingViews):

import dev.atedeg.ecscala.given

object Example with ECScalaDSL {
  val world = World()
  val view = getView[Position &: CNil].excluding[Color &: CNil] from world
  // or world.getView[Position &: CNil, Color &: CNil]
  // the view will iterate over all entities that have the Position component
  // and DO NOT have the Color component
}
Clone this wiki locally