Hierarchy

Constructors

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

      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

      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

      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

      Generated using TypeDoc