Skip to content

Latest commit

 

History

History
346 lines (203 loc) · 6.5 KB

GameObjectBase.md

File metadata and controls

346 lines (203 loc) · 6.5 KB

retro-engine / Exports / GameObjectBase

Class: 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, the Velocity 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++;
    }
  }
}

Table of contents

Constructors

Properties

Methods

Constructors

constructor

new GameObjectBase(name)

Constructor which wraps the object in a proxy. This allows the user to access the components directly.

Parameters

Name Type Description
name string The name of the entity, used for retrieving an entity from a scene by name.

Defined in

src/ecs.ts:61

Properties


name

name: string

Defined in

src/ecs.ts:54


scene

scene: Scene = null

Defined in

src/ecs.ts:55

Methods

add

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));

Parameters

Name Type Description
component Component The component to add

Returns

GameObjectBase

Defined in

src/ecs.ts:116


get

get<T>(c): T

Get the component instance of the given type.

Type parameters

Name Type
T extends Component<T>

Parameters

Name Type Description
c ComponentType<T> The type of the component to get.

Returns

T

The component instance.

Defined in

src/ecs.ts:140


getAllComponents

getAllComponents(): string[]

Get the names of all components linked to this entity

Returns

string[]

All components linked to this entity

Defined in

src/ecs.ts:158


getByName

getByName(name): Component

Get the component instance of the given component name, returns null if the component doesn't exist.

Parameters

Name Type Description
name string The name of the component to get.

Returns

Component

The component instance.

Defined in

src/ecs.ts:149


has

has<T>(c): boolean

Returns true if the entity has the given component.

Type parameters

Name Type
T extends Component<T>

Parameters

Name Type Description
c ComponentType<T> The type of the component to check.

Returns

boolean

True if the entity has the component.

Defined in

src/ecs.ts:168


hasAll

hasAll(components): boolean

Parameters

Name Type
components any[]

Returns

boolean

Defined in

src/ecs.ts:173


onCreate

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).

Parameters

Name Type
...args any[]

Returns

void

Defined in

src/ecs.ts:95


onDelete

onDelete(): void

User-defined function that is called when the entity is deleted.

Returns

void

Defined in

src/ecs.ts:100


remove

Remove a component from the entity.

remove<T>(c): void

Type parameters

Name Type
T extends Component<T>

Parameters

Name Type
c ComponentType<T>

Returns

void

Defined in

src/ecs.ts:179


removeByName

Remove a component from the entity by its name.

removeByName(name): void

Parameters

Name Type
name string

Returns

void

Defined in

src/ecs.ts:183


update

update(): void

User-defined function that is called every frame.

Returns

void

Defined in

src/ecs.ts:105