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 |
Return the entity ID. |
Private Members | ||
private set |
ID is read-only, attempting to set it will throw an error. |
Method Summary
Public Methods | ||
public |
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 |
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 |
destroy() 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 |
Returns a component by name, or undefined if it doesn't exist |
|
public |
Returns true if the entity has the specified component name. |
|
public |
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 |
Low level method to set components directly. |
|
public |
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
Public Members
Private Members
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().
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:
Name | Type | Attribute | Description |
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:
Name | Type | Attribute | Description |
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:
Name | Type | Attribute | Description |
component | string | The component name to get |
Example:
const position = entity.get('position')
public has(name: string): boolean source
Returns true if the entity has the specified component name.
Params:
Name | Type | Attribute | Description |
name | string | The component name to check for |
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:
Name | Type | Attribute | Description |
components | ...string |
|
The component names to remove from the entity. |
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:
Name | Type | Attribute | Description |
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. |
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:
Name | Type | Attribute | Description |
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 |
|
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. |
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')