A world is managed and instantiated by the WorldManager.

Each world is a running, breathing entity-component group that is created out of an EDF file. The EDF defines all instances that can be created within this world and also set's the list of static entities.

Static entities are instantiated on startup are meant to populate the rest of the world.

Using dependency information it is possible to ensure a static entity creation order. That way those entities that others rely on can be created first.

It is possible to access entities or other worlds by query for the ID of other worlds. Messages can be sent and information can find it's way.


Life-Cycle

A world has multiple states which can be manipulared by the following functions:

  • start() to begin the loading process and automatically run static Entities when everything is running
  • stop() to drop all entities and clear up RAM
  • restart() to clear and automatically activate it afterwards.
  • pause() to disable it from being processed. This is without loosing the active/deactive states of the objects.
  • resume() to continue after pause.

Basically the idea is to call activate() when you want to run the/a level or game part. If a world is finished and we don't need the world for now we call clear(). To restart a level and set everything to start we use reset(). Pause and Resume change the enabled flag and help to pause things that we currently don't need to run but like to continue later.


Loading & Reloading

Every call of stop() will not just stop the world but also release all resources. This is good if the world represents something that isn't needed anymore. However, imagine the world reperesents a running level of a Jump&Run and you like to stop/start it whenever the player dies. In such a case you want to keep all resources and reuse them. In such a case it's better to use restart().

Using restart() will only unload resources that are not needed after the start anymore and load the ones that are missing. This is only true if the same resource isn't referenced in another active world.

If this automatic approach does not work for you it's also possible to create a world only for the purpose of loading certain resources. This means a world can also be understand as resource managing object.


Rendering

Normally entities only get the onUpdate call so they can do the mainloop things. If one likes to write an entity that shall receive the render event one must call receiveFrameUpdates().

Hierarchy

  • World

Constructors

Properties

_actives: Entity[] = []
_alwaysActives: Entity[] = []
_deactives: Entity[] = []
_entityActivator: BaseEntityActivator
_entityDefinitions: Record<string, EntityDefinition>
_factory: EntityFactory
_newEntities: Entity[] = []
_onRenderEntities: Entity[] = []
_pendingInjections: number = 0
_resetting: boolean = false
_state: WorldState = WorldState.Empty
enabled: boolean = true
manager: WorldManager
name: string
staticInstanceOrder: string[] = []
statics: any = {}
time: number = 0
worldDesc: WorldDescription

Accessors

Methods

  • returns null or first entity matching name

    Parameters

    • name: any

      complete name of the entity. must match exactly

    • activeOnly: boolean = true

      if false one can also search for deactive entities

    • includeNew: boolean = false

      if true also entities in preparation are searched (useful during initialization)

    Returns Entity

  • Creates new entity(ies) and inject it as a new instance into the management process.

    This function will return when

    • a) all component scripts are loaded
    • b) all required resources are loaded
    • c) everything is glued together

    If the data is already loaded than it'll return instantly.

    If the entity cannot be created a error is sent to the logger.error function. OnReady will still be called.

    Returns

    Parameters

    • instance: any
    • onReady: any
      1. parameter is the array with the created entity instances

    Returns void

  • Main function to inject new entities into the running world.

    export class MyPlayer extends Components {

    @child("Bomb") // this makes sure all resources are loaded for the bomb
    private bomb:string;

    onUpdate(t:number):void {

    if (playerWantsToThrowBomb) {
    // new bomb is created!
    this.world.spawn(this.bomb);
    }

    if (playerWantsToThrowBigBomb) {
    // overwrite power property for this instance!
    this.world.spawn(this.bomb, {power: 200});
    }
    }
    }

    NOTE: For spawning objects to load a level use beforeWorldStart(loader:WorldStartContext) instead.

    Parameters

    • definitionName: string

      entity definition name in the current world

    • Optional props: Record<string, any>

      set/overwrite property

    • Optional instanceName: string

    Returns Entity

  • This will start the world.

    That means:

    • Load and parse the world description file(s) (*.edf)
    • Load all resources requested by the static entities of the world description (*.edf)
    • Call all beforeWorldStart(...) of static entities' components
    • Load all resources requested by beforeWorldStart(...)
    • Run all entities created in this world

    As those steps can take more than one frame to perform it won't just start. Instead it first goes into loading-state (see getState() == WorldState.Loading)

    Returns void

Generated using TypeDoc