World

class World(initialCapacity: Int = 1024)(source)

Holds entities and their components. Entities are cheap handles; components are plain objects stored per ComponentType in dense arrays, so iterating one component type touches contiguous memory and a two-type query costs one array lookup per entity.

The world starts with room for initialCapacity entities, which must be positive, and grows as needed.

Not thread-safe.

Throws

Constructors

Link copied to clipboard
constructor(initialCapacity: Int = 1024)

Properties

Link copied to clipboard

Number of live entities.

Functions

Link copied to clipboard
fun <T : Any> add(entity: Entity, type: ComponentType<T>, component: T)

Attaches component to entity under type, replacing any component of that type.

Link copied to clipboard
fun childrenOf(parent: Entity): List<Entity>

Returns the children of parent in the order they were attached, or an empty list if it has none. The list is a copy, so it does not change when the hierarchy does and it is safe to destroy or reparent its entities while iterating it.

Link copied to clipboard
fun count(type: ComponentType<*>): Int

Returns the number of entities that have a type component.

Link copied to clipboard
fun create(): Entity

Creates an entity with no components.

Link copied to clipboard
fun destroy(entity: Entity)

Removes entity, all its descendants and all their components. Does nothing if it is already gone.

Link copied to clipboard
fun World.drawnPositionOf(entity: Entity, alpha: Float): Vec2

Returns the position, in world units, at which the renderer draws entity this frame, so that a camera or an effect can follow what the player sees.

Link copied to clipboard
inline fun <A : Any> forEach(a: ComponentType<A>, action: (Entity, A) -> Unit)

Calls action for every entity that has an a component. Iterates the a components in storage order; do not add or remove components of this type inside action.

inline fun <A : Any, B : Any> forEach(a: ComponentType<A>, b: ComponentType<B>, action: (Entity, A, B) -> Unit)

Calls action for every entity that has both an a and a b component. Iterates the a components in storage order; do not add or remove components of these types inside action.

inline fun <A : Any, B : Any, C : Any> forEach(a: ComponentType<A>, b: ComponentType<B>, c: ComponentType<C>, action: (Entity, A, B, C) -> Unit)

Calls action for every entity that has a, b and c components, in a storage order.

Link copied to clipboard
fun <T : Any> get(entity: Entity, type: ComponentType<T>): T

Returns the type component of entity.

Link copied to clipboard
fun <T : Any> getOrNull(entity: Entity, type: ComponentType<T>): T?

Returns the type component of entity, or null if it has none.

Link copied to clipboard
fun has(entity: Entity, type: ComponentType<*>): Boolean

Returns true if entity has a type component.

Link copied to clipboard
fun isAlive(entity: Entity): Boolean

Returns true while entity exists in this world: from create until destroy, which also makes every older handle to its slot dead.

Link copied to clipboard
fun parentOrNull(child: Entity): Entity?

Returns the parent of child, or null if it has none or is not alive.

Link copied to clipboard

Computes the WorldTransform of every entity that has a parent and a Transform.

Link copied to clipboard
fun <T : Any> remove(entity: Entity, type: ComponentType<T>): Boolean

Detaches the type component from entity. Returns true if there was one.

Link copied to clipboard
fun removeParent(child: Entity)

Detaches child from its parent, making it a root whose Transform is in world space. Removes its WorldTransform right away, since only entities with a parent have one; its own children keep theirs. Does nothing if child has no parent.

Link copied to clipboard
fun setParent(child: Entity, parent: Entity)

Makes parent the parent of child, so child's Transform becomes relative to parent and destroy on parent also destroys child. If child already has a different parent it moves: it leaves the old parent's childrenOf and is appended to the end of parent's. Does nothing if parent already is the parent of child.

Link copied to clipboard

Makes the next frame draw entity exactly at its current Transform, with no blend from where it was a step ago.

Link copied to clipboard
open override fun toString(): String