Home Reference Source
import {Entity} from 'picoes/src/entity.js'
public class | source

Entity

Entity class used for storing components.

Constructor Summary

Private Constructor
private

constructor(world: World, id: number)

Do not construct an Entity yourself - use the entity() method in World instead.

Member Summary

Public Members
public get

id: number: *

Return the entity ID.

Private Members
private set

id

ID is read-only, attempting to set it will throw an error.

Method Summary

Public Methods
public

access(component: string, args: ...Object): Object

Returns a component by name (automatically created if it doesn't exist) For unregistered components, passing no arguments to this will create a {} component, if the component does not exist. The argument behavior is exactly the same as set().

public

attach(world: World)

Attaches a currently detached entity back to a world.

public

clone(): *

Creates a copy of this entity with all of the components cloned and returns it.

public

Remove this entity and all of its components from the world.

public

detach()

Removes this entity from the current world, without removing any components or data.

public

Deserializes data from JSON, creating new components and overwriting existing components.

public

get(component: string): Object

Returns a component by name, or undefined if it doesn't exist

public

has(name: string): boolean

Returns true if the entity has the specified component name.

public

remove(components: ...string): Object

Removes a component from the entity - has no effect when it doesn't exist. Can specify an onRemove() method in your component which gets called before it is removed. If nothing is specified, then nothing will be removed. Attempting to remove components that aren't set will be safely ignored.

public

replace(compName: string, value: Object, overwriteRegistered: boolean): Object

Low level method to set components directly.

public

set(compName: string, args: ...Object): Object

Adds a new component, or re-creates and overwrites an existing component

public

Serializes entire entity and components to JSON.

public

Returns true if this is a valid, existing, and usable entity, which is attached to a world.

Private Constructors

private constructor(world: World, id: number) source

Do not construct an Entity yourself - use the entity() method in World instead. Also, do not shallow/deep copy entity objects, only pass around references.

Params:

NameTypeAttributeDescription
world World

The world

id number

The identifier

Public Members

public get id: number: * source

Return the entity ID.

Return:

number

Integer entity ID

Private Members

private set id source

ID is read-only, attempting to set it will throw an error.

Throw:

Error

Cannot set entity id

Public Methods

public access(component: string, args: ...Object): Object source

Returns a component by name (automatically created if it doesn't exist) For unregistered components, passing no arguments to this will create a {} component, if the component does not exist. The argument behavior is exactly the same as set().

Params:

NameTypeAttributeDescription
component string

The component name to create/get

args ...Object
  • optional

The arguments to forward to create the new component, only if it doesn't exist.

Return:

Object

Always returns either the existing component, or the newly created one.

Example:

const position = entity.access('position', 3, 4)
entity.access('anything').foo = 'bar'
// 'anything' now has { foo: 'bar' }
entity.access('anything', { foo: 'bar' }).foo2 = 'bar2'
// 'anything' now has { foo: 'bar', foo2: 'bar2' }

public attach(world: World) source

Attaches a currently detached entity back to a world. Do not use detached entities, get() may be safe, but avoid calling other methods The ID will be reassigned, so do not rely on this

Params:

NameTypeAttributeDescription
world World

The world to attach this entity to

Example:

entity.attach(world)

public clone(): * source

Creates a copy of this entity with all of the components cloned and returns it. Individual components are either shallow or deep copied, depending on component registration status and if a clone() method is defined.

Return:

*

Example:

const clonedEntity = entity.clone()
// How to define custom clone methods
world.component('foo', class {
  onCreate(bar, baz) {
    this.bar = bar
    this.baz = baz
    this.qux = false
  }
  setQux(qux = true) {
    this.qux = qux
  }
  cloneArgs() {
    return [this.bar, this.baz]
  }
  clone(target) {
    target.qux = this.qux
  }
})

public destroy() source

Remove this entity and all of its components from the world. After an entity is destroyed, the object should be discarded, and it is recommended to avoid re-using it.

Example:

entity.destroy()

public detach() source

Removes this entity from the current world, without removing any components or data. It can be re-attached to another world (or the same world), using the attach() method. Do not use detached entities, get() may be safe, but avoid calling other methods The ID will be reassigned, so do not rely on this

Example:

entity.detach()

public fromJSON(data: string): Object source

Deserializes data from JSON, creating new components and overwriting existing components. Defining fromJSON methods in your components will override the built-in behavior.

Params:

NameTypeAttributeDescription
data string

A JSON string containing component data to parse, and store in this entity.

Return:

Object

The original entity that fromJSON() was called on, so that operations can be chained.

Example:

entity.fromJSON(serializedEntity)

public get(component: string): Object source

Returns a component by name, or undefined if it doesn't exist

Params:

NameTypeAttributeDescription
component string

The component name to get

Return:

Object

The component if defined, otherwise undefined

Example:

const position = entity.get('position')

public has(name: string): boolean source

Returns true if the entity has the specified component name.

Params:

NameTypeAttributeDescription
name string

The component name to check for

Return:

boolean

true or false

Example:

entity.has('position')

public remove(components: ...string): Object source

Removes a component from the entity - has no effect when it doesn't exist. Can specify an onRemove() method in your component which gets called before it is removed. If nothing is specified, then nothing will be removed. Attempting to remove components that aren't set will be safely ignored.

Params:

NameTypeAttributeDescription
components ...string
  • optional

The component names to remove from the entity.

Return:

Object

The original entity that remove() was called on, so that operations can be chained.

Example:

entity.remove('position')

public replace(compName: string, value: Object, overwriteRegistered: boolean): Object source

Low level method to set components directly. It is recommended to use set() instead, unless you know what you are doing. The onCreate method is not called, and it is expected that you pass an already initialized component. By default, an error will be thrown if you try calling this on a registered component, as that could have unintended consequences in your systems. This method can be useful for saving and restoring components without serialization.

Params:

NameTypeAttributeDescription
compName string

The component name to set.

value Object

Should be a previous component instance, or whatever is expected for the component name.

overwriteRegistered boolean

Whether or not to proceed with overwriting components in this entity that are registered components. By default, this is false.

Return:

Object

The original entity that replace() was called on, so that operations can be chained.

Example:

entity.replace('position', position)

public set(compName: string, args: ...Object): Object source

Adds a new component, or re-creates and overwrites an existing component

Params:

NameTypeAttributeDescription
compName string

The component name to create. If there is a registered component for this name, then its constructor will be called with (...args) and an object of that type will be created. The parent entity reference gets injected into registered components after they are constructed. The onCreate method gets called after the component is added to the entity, and after the entity is injected. The onCreate method also gets passed the same (...args) as the component's constructor.

args ...Object
  • optional

The arguments to forward to the registered component type. If the component type is not registered, then only the first additional argument will be used as the value of the entire component.

Return:

Object

The original entity that set() was called on, so that operations can be chained.

Example:

entity.set('position', 1, 2)
// If position was registered: { x: 1, y: 2 }
// If position was not registered: 1
// This is because only the first arg is used for unregistered components
entity.set('anonymousComponent', { keys: 'values' })
entity.set('someString', 'Any type of any value')
entity.set('thisCompIsUndefined', undefined)
entity.set('thisCompIsEmptyObject')

public toJSON(): string source

Serializes entire entity and components to JSON. Defining toJSON methods in your components will override the built-in behavior.

Return:

string

JSON encoded string

Example:

const serializedEntity = entity.toJSON()

public valid(): boolean source

Returns true if this is a valid, existing, and usable entity, which is attached to a world.

Return:

boolean

true or false

Example:

if (entity.valid()) {...}