noa.entities - manages entities and components.

This class extends ent-comp, a general-purpose ECS. It's also decorated with noa-specific helpers and accessor functions for querying entity positions, etc.

Expects entity definitions in a specific format - see source components folder for examples.

This module uses the following default options (from the options object passed to the Engine):

var defaults = {
shadowDistance: 10,
}

Hierarchy

Properties

addComponent: ((entID, compName, state) => ECS)

Type declaration

    • (entID, compName, state): ECS
    • Adds a component to an entity, optionally initializing the state object.

      ecs.createComponent({
      name: 'foo',
      state: { val: 1 }
      })
      ecs.addComponent(id1, 'foo') // use default state
      ecs.addComponent(id2, 'foo', { val:2 }) // pass in state data

      Parameters

      • entID: any
      • compName: any
      • state: any

      Returns ECS

components: {}

Hash of component definitions. Also aliased to comps.

var comp = { name: 'foo' }
ecs.createComponent(comp)
ecs.components['foo'] === comp // true
ecs.comps['foo'] // same

Type declaration

    comps: {}

    Type declaration

      createComponent: ((compDefn) => string)

      Type declaration

        • (compDefn): string
        • Creates a new component from a definition object. The definition must have a name; all other properties are optional.

          Returns the component name, to make it easy to grab when the component is being required from a module.

          var comp = {
          name: 'some-unique-string',
          state: {},
          order: 99,
          multi: false,
          onAdd: (id, state) => { },
          onRemove: (id, state) => { },
          system: (dt, states) => { },
          renderSystem: (dt, states) => { },
          }

          var name = ecs.createComponent( comp )
          // name == 'some-unique-string'

          Note the multi flag - for components where this is true, a given entity can have multiple state objects for that component. For multi-components, APIs that would normally return a state object (like getState) will instead return an array of them.

          Parameters

          • compDefn: any

          Returns string

      createEntity: ((compList) => number)

      Type declaration

        • (compList): number
        • Creates a new entity id (currently just an incrementing integer).

          Optionally takes a list of component names to add to the entity (with default state data).

          var id1 = ecs.createEntity()
          var id2 = ecs.createEntity([ 'some-component', 'other-component' ])

          Parameters

          • compList: any

          Returns number

      deleteComponent: ((compName) => ECS)

      Type declaration

        • (compName): ECS
        • Deletes the component definition with the given name. First removes the component from all entities that have it.

          Note: This API shouldn't be necessary in most real-world usage - you should set up all your components during init and then leave them be. But it's useful if, say, you receive an ECS from another library and you need to replace its components.

          ecs.deleteComponent( 'some-component' )
          

          Parameters

          • compName: any

          Returns ECS

      deleteEntity: ((entID) => ECS)

      Type declaration

        • (entID): ECS
        • Deletes an entity, which in practice means removing all its components.

          ecs.deleteEntity(id)
          

          Parameters

          • entID: any

          Returns ECS

      getCollideEntities: ((id) => {
          callback: Function;
          collideBits: number;
          collideMask: number;
          cylinder: boolean;
      })

      Type declaration

        • (id): {
              callback: Function;
              collideBits: number;
              collideMask: number;
              cylinder: boolean;
          }
        • Returns the entity's collideEntities component state

          Parameters

          • id: number

          Returns {
              callback: Function;
              collideBits: number;
              collideMask: number;
              cylinder: boolean;
          }

          • callback: Function
          • collideBits: number
          • collideMask: number
          • cylinder: boolean
      getCollideTerrain: ((id) => {
          callback: Function;
      })

      Type declaration

        • (id): {
              callback: Function;
          }
        • Returns the entity's collideTerrain component state

          Parameters

          • id: number

          Returns {
              callback: Function;
          }

          • callback: Function
      getComponentAccessor: ((compName) => ((id) => boolean))

      Type declaration

        • (compName): ((id) => boolean)
        • Makes a hasComponent-like accessor function bound to a given component. The accessor is much faster than hasComponent.

          ecs.createComponent({
          name: 'foo',
          })
          var hasFoo = ecs.getComponentAccessor('foo')
          // ...
          ecs.addComponent(id, 'foo')
          hasFoo(id) // true

          Parameters

          • compName: any

          Returns ((id) => boolean)

            • (id): boolean
            • Parameters

              • id: any

              Returns boolean

      getMeshData: ((id) => {
          mesh: any;
          offset: number[];
      })

      Type declaration

        • (id): {
              mesh: any;
              offset: number[];
          }
        • Returns the entity's mesh component state

          Parameters

          • id: number

          Returns {
              mesh: any;
              offset: number[];
          }

          • mesh: any
          • offset: number[]
      getMovement: ((id) => MovementState)

      Type declaration

      getPhysics: ((id) => PhysicsState)

      Type declaration

      getPhysicsBody: ((id) => RigidBody)

      Type declaration

        • (id): RigidBody
        • Returns the entity's physics body Note, will throw if the entity doesn't have the position component!

          Parameters

          • id: number

          Returns RigidBody

      getPosition: ((id) => number[])

      Type declaration

        • (id): number[]
        • Returns the entity's position vector.

          Parameters

          • id: number

          Returns number[]

      getPositionData: ((id) => PositionState)

      Type declaration

      getState: ((entID, compName) => any)

      Type declaration

        • (entID, compName): any
        • Get the component state for a given entity. It will automatically have an __id property for the entity id.

          ecs.createComponent({
          name: 'foo',
          state: { val: 0 }
          })
          ecs.addComponent(id, 'foo')
          ecs.getState(id, 'foo').val // 0
          ecs.getState(id, 'foo').__id // equals id

          Parameters

          • entID: any
          • compName: any

          Returns any

      getStateAccessor: ((compName) => ((id) => any))

      Type declaration

        • (compName): ((id) => any)
        • Makes a getState-like accessor bound to a given component. The accessor is faster than getState, so you may want to create an accessor for any component you'll be accessing a lot.

          ecs.createComponent({
          name: 'size',
          state: { val: 0 }
          })
          var getEntitySize = ecs.getStateAccessor('size')
          // ...
          ecs.addComponent(id, 'size', { val:123 })
          getEntitySize(id).val // 123

          Parameters

          • compName: any

          Returns ((id) => any)

            • (id): any
            • Parameters

              • id: any

              Returns any

      getStatesList: ((compName) => any)

      Type declaration

        • (compName): any
        • Get an array of state objects for every entity with the given component. Each one will have an __id property for the entity id it refers to. Don't add or remove elements from the returned list!

          var arr = ecs.getStatesList('foo')
          // returns something shaped like:
          // [
          // {__id:0, x:1},
          // {__id:7, x:2},
          // ]

          Parameters

          • compName: any

          Returns any

      hasComponent: ((entID, compName) => boolean)

      Type declaration

        • (entID, compName): boolean
        • Checks if an entity has a component.

          ecs.addComponent(id, 'foo')
          ecs.hasComponent(id, 'foo') // true

          Parameters

          • entID: any
          • compName: any

          Returns boolean

      hasMesh: ((id) => boolean)

      Type declaration

        • (id): boolean
        • Returns whether the entity has a mesh

          Parameters

          • id: number

          Returns boolean

      hasPhysics: ((id) => boolean)

      Type declaration

        • (id): boolean
        • Returns whether the entity has a physics body

          Parameters

          • id: number

          Returns boolean

      hasPosition: ((id) => boolean)

      Type declaration

        • (id): boolean
        • Returns whether the entity has a position

          Parameters

          • id: number

          Returns boolean

      names: {
          [key: string]: string;
      }

      Hash containing the component names of built-in components.

      Type declaration

      • [key: string]: string
      onPairwiseEntityCollision: ((id1, id2) => void)

      Type declaration

        • (id1, id2): void
        • Pairwise collideEntities event - assign your own function to this property if you want to handle entity-entity overlap events.

          Parameters

          • id1: number
          • id2: number

          Returns void

      removeComponent: ((entID, compName) => ECS)

      Type declaration

        • (entID, compName): ECS
        • Removes a component from an entity, triggering the component's onRemove handler, and then deleting any state data.

          ecs.removeComponent(id, 'foo')
          ecs.hasComponent(id, 'foo') // false

          Parameters

          • entID: any
          • compName: any

          Returns ECS

      removeMultiComponent: ((entID, compName, index) => ECS)

      Type declaration

        • (entID, compName, index): ECS
        • Removes one particular instance of a multi-component. To avoid breaking loops, the relevant state object will get nulled immediately, and spliced from the states array later when safe (after the current tick/render/animationFrame).

          // where component 'foo' is a multi-component
          ecs.getState(id, 'foo') // [ state1, state2, state3 ]
          ecs.removeMultiComponent(id, 'foo', 1)
          ecs.getState(id, 'foo') // [ state1, null, state3 ]
          // one JS event loop later...
          ecs.getState(id, 'foo') // [ state1, state3 ]

          Parameters

          • entID: any
          • compName: any
          • index: any

          Returns ECS

      render: ((dt) => ECS)

      Type declaration

        • (dt): ECS
        • Functions exactly like tick, but calls renderSystem functions. this effectively gives you a second set of systems that are called with separate timing, in case you want to tick and render in separate loops (which you should!).

          ecs.createComponent({
          name: foo,
          order: 5,
          renderSystem: function(dt, states) {
          // states is the same array you'd get from #getStatesList()
          }
          })
          ecs.render(1000/60)

          Parameters

          • dt: any

          Returns ECS

      tick: ((dt) => ECS)

      Type declaration

        • (dt): ECS
        • Tells the ECS that a game tick has occurred, causing component system functions to get called.

          The optional parameter simply gets passed to the system functions. It's meant to be a timestep, but can be used (or not used) as you like.

          If components have an order property, they'll get called in that order (lowest to highest). Component order defaults to 99.

          ecs.createComponent({
          name: foo,
          order: 1,
          system: function(dt, states) {
          // states is the same array you'd get from #getStatesList()
          states.forEach(state => {
          console.log('Entity ID: ', state.__id)
          })
          }
          })
          ecs.tick(30) // triggers log statements

          Parameters

          • dt: any

          Returns ECS

      Methods

      • Helper to set up a general entity, and populate with some common components depending on arguments.

        Parameters

        • position: any = null
        • width: number = 1
        • height: number = 1
        • mesh: any = null
        • meshOffset: any = null
        • doPhysics: boolean = false
        • shadow: boolean = false

        Returns number

      • Safely add a component - if the entity already had the component, this will remove and re-add it.

        Parameters

        • id: any
        • name: any
        • state: any

        Returns void

      • Gets an array of all entities overlapping the given AABB

        Parameters

        • box: any
        • withComponent: any

        Returns any[]

      • Checks whether a voxel is obstructed by any entity (with the collidesTerrain component)

        Parameters

        • x: any
        • y: any
        • z: any

        Returns boolean

      • Set an entity's size

        Parameters

        • id: any
        • xs: number
        • ys: number
        • zs: number

        Returns void

      • Set an entity's position, and update all derived state.

        In general, always use this to set an entity's position unless you're familiar with engine internals.

        noa.ents.setPosition(playerEntity, [5, 6, 7])
        noa.ents.setPosition(playerEntity, 5, 6, 7) // also works

        Parameters

        • id: number
        • pos: any
        • y: number = 0
        • z: number = 0

        Returns void

      Generated using TypeDoc