Options
All
  • Public
  • Public/Protected
  • All
Menu

Class World

World is the core of the ECS. It stores all entities and their components, and enables efficiently querying them.

Visit https://jprochazk.github.io/uecs/ for a comprehensive tutorial.

Hierarchy

  • World

Index

Constructors

constructor

Properties

Private components

components: TypeStorage<ComponentStorage<Component>> = ...

Private entities

entities: Set<number> = ...

Private entitySequence

entitySequence: number = 0

Private views

views: {} = ...

Type declaration

  • [id: string]: View<any>

Methods

all

  • all(): IterableIterator<number>
  • Returns an iterator over all the entities in the world.

    Returns IterableIterator<number>

clear

  • clear(): void
  • Removes every entity, and destroys all components.

    Returns void

create

  • create<T>(...components: T): number
  • Creates an entity, and optionally assigns all components to it.

    Type parameters

    Parameters

    • Rest ...components: T

    Returns number

destroy

  • destroy(entity: number): void
  • Destroys an entity and all its components

    Calls .free() (if available) on each destroyed component

    Example:

     class A { free() { console.log("A freed"); } }
     const world = new World();
     const entity = world.create(new A);
     world.destroy(entity); // logs "A freed"
    

    Parameters

    • entity: number

    Returns void

emplace

  • emplace<T>(entity: number, component: T): void
  • Sets entity's instance of component type to component.

    throws

    If entity does not exist

    Warning: Overwrites any existing instance of the component. This is to avoid an unnecessary check in 99% of cases where the entity does not have the component yet. Use world.has to check for the existence of the component first, if this is undesirable.

    Example:

     class A { constructor(value) { this.value = value } }
     const entity = world.create();
     world.emplace(entity, new A(0));
     world.emplace(entity, new A(5));
     world.get(entity, A); // A { value: 5 } -> overwritten
    

    Note: This is the only place in the API where an error will be thrown in case you try to use a non-existent entity.

    Here's an example of why it'd be awful if World.emplace didn't throw:

     class A { constructor(value = 0) { this.value = value } }
     const world = new World;
     world.exists(0); // false
     world.emplace(0, new A);
     // entity '0' doesn't exist, but it now has a component.
     // let's try creating a brand new entity:
     const entity = world.create();
     // *BOOM!*
     world.get(0, A); // A { value: 0 }
     // it'd be extremely difficult to track down this bug.
    

    Type parameters

    Parameters

    • entity: number
    • component: T

    Returns void

exists

  • exists(entity: number): boolean
  • Returns true if entity exists in this World

    Parameters

    • entity: number

    Returns boolean

get

  • get<T>(entity: number, component: Constructor<T, any>): undefined | T
  • Retrieves component belonging to entity. Returns undefined if it the entity doesn't have component, or the entity doesn't exist.

    Example:

     class A { value = 50 }
     class B {}
     const world = new World();
     const entity = world.create();
     world.emplace(entity, new A);
     world.get(entity, A).value; // 50
     world.get(entity, A).value = 10;
     world.get(entity, A).value; // 10
     world.get(entity, B); // undefined
     world.get(100, A); // undefined
    

    Type parameters

    Parameters

    • entity: number
    • component: Constructor<T, any>

    Returns undefined | T

has

  • has<T>(entity: number, component: Constructor<T, any>): boolean
  • Returns true if entity exists AND has component, false otherwise.

    Example:

     class A {}
     const world = new World();
     const entity = world.create();
     world.has(entity, A); // false
     world.emplace(entity, new A);
     world.has(entity, A); // true
     world.has(100, A); // false
    

    Type parameters

    Parameters

    • entity: number
    • component: Constructor<T, any>

    Returns boolean

insert

  • insert<T>(entity: number, ...components: T): number
  • Inserts the entity, and optionally assigns all components to it.

    If the entity already exists, all components will be assigned to it. If it already has some other components, they won't be destroyed:

     class A { constructor(value = 0) { this.value = value } }
     class B { constructor(value = 0) { this.value = value } }
     const world = new World;
     const entity = world.create(new A, new B);
     world.get(entity, A); // A { value: 0 }
     world.insert(entity, new A(5));
     world.get(entity, A); // A { value: 5 }
     world.get(entity, B); // B { value: 0 }
    

    You can first check if the entity exists, destroy it if so, and then insert it.

     if (world.exists(entity)) {
         world.destroy(entity);
     }
     world.insert(entity, new A, new B, ...);
    

    Type parameters

    Parameters

    • entity: number
    • Rest ...components: T

    Returns number

remove

  • remove<T>(entity: number, component: Constructor<T, any>): undefined | T
  • Removes instance of component from entity, and returns the removed component. Returns undefined if nothing was removed, or if entity does not exist.

    Example:

     class A { value = 10 }
     const world = new World();
     const entity = world.create();
     world.emplace(entity, new A);
     world.get(entity, A).value = 50
     world.remove(entity, A); // A { value: 50 }
     world.remove(entity, A); // undefined
    

    This does not call .free() on the component. The reason for this is that you don't always want to free the removed component. Don't fret, you can still free component, because the World.remove call returns it! Example:

     class F { free() { console.log("freed") } }
     const world = new World;
     const entity = world.create(new F);
     world.remove(entity, F).free();
     // you can use optional chaining to easily guard against the 'undefined' case:
     world.remove(entity, F)?.free();
    

    Type parameters

    Parameters

    • entity: number
    • component: Constructor<T, any>

    Returns undefined | T

size

  • size(): number
  • Returns the size of the world (how many entities are stored)

    Returns number

view

  • view<T>(...types: T): View<T>
  • Used to query for entities with specific component combinations and efficiently iterate over the result.

    Example:

     class Fizz { }
     class Buzz { }
     const world = new World();
     for (let i = 0; i < 100; ++i) {
         const entity = world.create();
         if (i % 3 === 0) world.emplace(entity, new Fizz);
         if (i % 5 === 0) world.emplace(entity, new Buzz);
     }
    
     world.view(Fizz, Buzz).each((n) => {
         console.log(`FizzBuzz! (${n})`);
     });
    

    Type parameters

    Parameters

    • Rest ...types: T

    Returns View<T>

Generated using TypeDoc