Skip to content
This repository has been archived by the owner on Jun 1, 2022. It is now read-only.

Implement getComponentOrThrow methods on Entity #257

Open
wants to merge 5 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 26 additions & 16 deletions src/Entity.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,21 @@ export class Entity {
includeRemoved?: boolean
): Readonly<C> | undefined;

/**
* Get an immutable reference to a component on this entity. Throws if the component is not on the entity.
* @param Component Type of component to get
* @param includeRemoved Whether a component that is staled to be removed should be also considered
*/
getComponentOrThrow<C extends Component<any>>(
Component: ComponentConstructor<C>,
includeRemoved?: boolean
): Readonly<C>;

/**
* Get a component that is slated to be removed from this entity.
*/
getRemovedComponent<C extends Component<any>>(
Component: ComponentConstructor<C>
Component: ComponentConstructor<C>
): Readonly<C> | undefined;

/**
Expand All @@ -54,6 +64,14 @@ export class Entity {
Component: ComponentConstructor<C>
): C | undefined;

/**
* Get a mutable reference to a component on this entity. Throws if the component is not on the entity.
* @param Component Type of component to get
*/
getMutableComponentOrThrow<C extends Component<any>>(
Component: ComponentConstructor<C>
): C;

/**
* Add a component to the entity.
* @param Component Type of component to add to this entity
Expand Down Expand Up @@ -96,37 +114,29 @@ export class Entity {
* Check if the entity has all components in a list.
* @param Components Component types to check
*/
hasAllComponents(
Components: Array<ComponentConstructor<any>>
): boolean
hasAllComponents(Components: Array<ComponentConstructor<any>>): boolean;

/**
* Check if the entity has any of the components in a list.
* @param Components Component types to check
*/
hasAnyComponents(
Components: Array<ComponentConstructor<any>>
): boolean
hasAnyComponents(Components: Array<ComponentConstructor<any>>): boolean;

/**
* Remove all components on this entity.
* @param forceImmediate Whether all components should be removed immediately
*/
removeAllComponents(
forceImmediate?: boolean
): void
removeAllComponents(forceImmediate?: boolean): void;

copy(source: this): this
copy(source: this): this;

clone(): this
clone(): this;

reset(): void
reset(): void;

/**
* Remove this entity from the world.
* @param forceImmediate Whether this entity should be removed immediately
*/
remove(
forceImmediate?: boolean
): void;
remove(forceImmediate?: boolean): void;
}
20 changes: 20 additions & 0 deletions src/Entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ export class Entity {
: component;
}

getComponentOrThrow(Component, includeRemoved) {
const component = this.getComponent(Component, includeRemoved);
if (!component) {
throw new Error(
`Entity ${this.id} does not have component ${Component.getName()}.`
);
}
return component;
}

getRemovedComponent(Component) {
const component = this._componentsToRemove[Component._typeId];

Expand Down Expand Up @@ -84,6 +94,16 @@ export class Entity {
return component;
}

getMutableComponentOrThrow(Component) {
const component = this.getMutableComponent(Component);
if (!component) {
throw new Error(
`Entity ${this.id} does not have component ${Component.getName()}.`
);
}
return component;
}

addComponent(Component, values) {
this._entityManager.entityAddComponent(this, Component, values);
return this;
Expand Down