diff --git a/src/Entity.d.ts b/src/Entity.d.ts index 1271b063..e27a9a71 100644 --- a/src/Entity.d.ts +++ b/src/Entity.d.ts @@ -24,11 +24,21 @@ export class Entity { includeRemoved?: boolean ): Readonly | 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>( + Component: ComponentConstructor, + includeRemoved?: boolean + ): Readonly; + /** * Get a component that is slated to be removed from this entity. */ getRemovedComponent>( - Component: ComponentConstructor + Component: ComponentConstructor ): Readonly | undefined; /** @@ -54,6 +64,14 @@ export class Entity { Component: ComponentConstructor ): 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>( + Component: ComponentConstructor + ): C; + /** * Add a component to the entity. * @param Component Type of component to add to this entity @@ -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> - ): boolean + hasAllComponents(Components: Array>): boolean; /** * Check if the entity has any of the components in a list. * @param Components Component types to check */ - hasAnyComponents( - Components: Array> - ): boolean + hasAnyComponents(Components: Array>): 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; } diff --git a/src/Entity.js b/src/Entity.js index 04aea57d..59eb2e92 100644 --- a/src/Entity.js +++ b/src/Entity.js @@ -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]; @@ -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;