retro-engine / Exports / GameObjectBase
The base class inherited by all game objects.
Note that when creating a game object, a proxy is returned which allows you to directly access entity components without having to use the
get
method. In the example below, theVelocity
component is accessed directly.
Example
An example user-defined game object:
class Player extends engine.GameObjectBase {
static arg_names = ["Jump Speed"];
static arg_types = [engine.Types.Number];
times_jumped;
onCreate(jump_speed) {
this.jump_speed = jump_speed;
this.times_jumped = 0;
}
update() {
if (engine.KeyStates.isPressed("Space")) {
this.Velocity.y = this.jump_speed;
this.times_jumped++;
}
}
}
• new GameObjectBase(name
)
Constructor which wraps the object in a proxy. This allows the user to access the components directly.
Name | Type | Description |
---|---|---|
name |
string |
The name of the entity, used for retrieving an entity from a scene by name. |
• name: string
• scene: Scene
= null
▸ add(component
): GameObjectBase
Add a new component to the entity. Returns itself allowing calls to be chained.
Example
entity.add(new Position(0, 0)).add(new Velocity(0, 0));
Name | Type | Description |
---|---|---|
component |
Component |
The component to add |
▸ get<T
>(c
): T
Get the component instance of the given type.
Name | Type |
---|---|
T |
extends Component <T > |
Name | Type | Description |
---|---|---|
c |
ComponentType <T > |
The type of the component to get. |
T
The component instance.
▸ getAllComponents(): string
[]
Get the names of all components linked to this entity
string
[]
All components linked to this entity
▸ getByName(name
): Component
Get the component instance of the given component name, returns null if the component doesn't exist.
Name | Type | Description |
---|---|---|
name |
string |
The name of the component to get. |
The component instance.
▸ has<T
>(c
): boolean
Returns true if the entity has the given component.
Name | Type |
---|---|
T |
extends Component <T > |
Name | Type | Description |
---|---|---|
c |
ComponentType <T > |
The type of the component to check. |
boolean
True if the entity has the component.
▸ hasAll(components
): boolean
Name | Type |
---|---|
components |
any [] |
boolean
▸ Abstract
onCreate(...args
): void
User-defined function that is called when the entity is added to a scene, after all components have been created (if using a editor created entity, or prefab entity).
Name | Type |
---|---|
...args |
any [] |
void
▸ onDelete(): void
User-defined function that is called when the entity is deleted.
void
Remove a component from the entity.
▸ remove<T
>(c
): void
Name | Type |
---|---|
T |
extends Component <T > |
Name | Type |
---|---|
c |
ComponentType <T > |
void
Remove a component from the entity by its name.
▸ removeByName(name
): void
Name | Type |
---|---|
name |
string |
void
▸ update(): void
User-defined function that is called every frame.
void