From da187afb5f6e18f179d8260530ce95a2659aea18 Mon Sep 17 00:00:00 2001 From: Liau Jian Jie Date: Tue, 1 Sep 2020 02:59:15 +0800 Subject: [PATCH 1/5] Implement `getMutableComponentOrThrow` --- src/Entity.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/Entity.js b/src/Entity.js index 04aea57d..be6e0b91 100644 --- a/src/Entity.js +++ b/src/Entity.js @@ -84,6 +84,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; From 7be277205797b1ebd0dd8371eb6bbbdfd326ed6a Mon Sep 17 00:00:00 2001 From: Liau Jian Jie Date: Tue, 1 Sep 2020 03:04:37 +0800 Subject: [PATCH 2/5] Implement `getComponentOrThrow` --- src/Entity.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/Entity.js b/src/Entity.js index be6e0b91..b671f04f 100644 --- a/src/Entity.js +++ b/src/Entity.js @@ -42,6 +42,16 @@ export class Entity { : component; } + getComponentOrThrow(Component) { + const component = this.getComponent(Component); + if (!component) { + throw new Error( + `Entity ${this.id} does not have component ${Component.getName()}.` + ); + } + return component; + } + getRemovedComponent(Component) { const component = this._componentsToRemove[Component._typeId]; From 5e7512ab99cdd841181e9b7059133a99e803b175 Mon Sep 17 00:00:00 2001 From: Liau Jian Jie Date: Tue, 1 Sep 2020 03:07:17 +0800 Subject: [PATCH 3/5] Add `includeRemoved` param to `getComponentOrThrow` --- src/Entity.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Entity.js b/src/Entity.js index b671f04f..59eb2e92 100644 --- a/src/Entity.js +++ b/src/Entity.js @@ -42,8 +42,8 @@ export class Entity { : component; } - getComponentOrThrow(Component) { - const component = this.getComponent(Component); + getComponentOrThrow(Component, includeRemoved) { + const component = this.getComponent(Component, includeRemoved); if (!component) { throw new Error( `Entity ${this.id} does not have component ${Component.getName()}.` From 05213a2fec50bc00a8a7979beaf5757893d7a56d Mon Sep 17 00:00:00 2001 From: Liau Jian Jie Date: Tue, 1 Sep 2020 03:09:45 +0800 Subject: [PATCH 4/5] Add ts typedefs --- src/Entity.d.ts | 42 ++++++++++++++++++++++++++---------------- 1 file changed, 26 insertions(+), 16 deletions(-) diff --git a/src/Entity.d.ts b/src/Entity.d.ts index 1271b063..a3b3d7f4 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 + */ + getMutableComponent>( + 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; } From 98638b70e59ab4d1a684392d17e419bd87dcb7af Mon Sep 17 00:00:00 2001 From: Liau Jian Jie Date: Tue, 1 Sep 2020 14:49:49 +0800 Subject: [PATCH 5/5] Fix typo in typedefs --- src/Entity.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Entity.d.ts b/src/Entity.d.ts index a3b3d7f4..e27a9a71 100644 --- a/src/Entity.d.ts +++ b/src/Entity.d.ts @@ -68,7 +68,7 @@ export class Entity { * 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 */ - getMutableComponent>( + getMutableComponentOrThrow>( Component: ComponentConstructor ): C;