From 4ec8f1c6ba270fc0f9913bbe56386c55cf9d93e9 Mon Sep 17 00:00:00 2001 From: Erik Onarheim Date: Wed, 24 Jan 2024 17:12:05 -0600 Subject: [PATCH] feat: Add ex.GraphicsComponent.remove() --- CHANGELOG.md | 1 + sandbox/tests/camera-animation/index.html | 11 +++++++++ src/engine/EntityComponentSystem/Component.ts | 2 +- src/engine/Graphics/GraphicsComponent.ts | 17 ++++++++++++- src/spec/GraphicsComponentSpec.ts | 24 +++++++++++++++++++ 5 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 sandbox/tests/camera-animation/index.html diff --git a/CHANGELOG.md b/CHANGELOG.md index fc95887ce..1069243f2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,6 +26,7 @@ This project adheres to [Semantic Versioning](http://semver.org/). ### Added +- Added missing dual of `ex.GraphicsComponent.add()`, you can now `ex.GraphicsComponent.remove(name)`; - Added additional options to `ex.Animation.fromSpriteSheetCoordinates()` you can now pass any valid `ex.GraphicOptions` to influence the sprite per frame ```typescript const anim = ex.Animation.fromSpriteSheetCoordinates({ diff --git a/sandbox/tests/camera-animation/index.html b/sandbox/tests/camera-animation/index.html new file mode 100644 index 000000000..15f2626a1 --- /dev/null +++ b/sandbox/tests/camera-animation/index.html @@ -0,0 +1,11 @@ + + + + + + Camera Animation + + + + + \ No newline at end of file diff --git a/src/engine/EntityComponentSystem/Component.ts b/src/engine/EntityComponentSystem/Component.ts index cbabd6c43..5b0b1df04 100644 --- a/src/engine/EntityComponentSystem/Component.ts +++ b/src/engine/EntityComponentSystem/Component.ts @@ -72,7 +72,7 @@ export abstract class Component { onAdd?(owner: Entity): void; /** - * Optional callback called when a component is added to an entity + * Optional callback called when a component is removed from an entity */ onRemove?(previousOwner: Entity): void; } \ No newline at end of file diff --git a/src/engine/Graphics/GraphicsComponent.ts b/src/engine/Graphics/GraphicsComponent.ts index d79773d29..a49c2974e 100644 --- a/src/engine/Graphics/GraphicsComponent.ts +++ b/src/engine/Graphics/GraphicsComponent.ts @@ -254,6 +254,19 @@ export class GraphicsComponent extends Component { return graphicToSet; } + /** + * Removes a registered graphic, if the removed graphic is the current it will switch to the default + * @param name + */ + public remove(name: string) { + delete this._graphics[name]; + delete this._options[name]; + if (this._current === name) { + this._current = 'default'; + this.recalculateBounds(); + } + } + /** * Shows a graphic, will be removed * @param nameOrGraphic @@ -265,7 +278,9 @@ export class GraphicsComponent extends Component { } /** - * Use a graphic only, swap out any graphics on the **default** layer, returns the new [[Graphic]] + * Use a graphic only, will set the default graphic. Returns the new [[Graphic]] + * + * Optionally override the stored options * @param nameOrGraphic * @param options */ diff --git a/src/spec/GraphicsComponentSpec.ts b/src/spec/GraphicsComponentSpec.ts index b8c632a4b..25607a191 100644 --- a/src/spec/GraphicsComponentSpec.ts +++ b/src/spec/GraphicsComponentSpec.ts @@ -131,6 +131,30 @@ describe('A Graphics ECS Component', () => { }); }); + it('can remove graphics', () => { + const rect = new ex.Rectangle({ + width: 40, + height: 40 + }); + const sut = new ex.GraphicsComponent(); + + sut.add('rect', rect, { offset: ex.vec(1, 2), anchor: ex.vec(1, 1) }); + expect(sut.current).toBe(undefined); + expect(sut.currentOptions).toBe(undefined); + + expect(sut.getNames()).toEqual(['rect']); + + sut.use('rect'); + expect(sut.current).toEqual(rect); + expect(sut.currentOptions).toEqual(undefined); + + sut.remove('rect'); + + expect(sut.current).toBe(undefined); + expect(sut.currentOptions).toBe(undefined); + expect(sut.getNames()).toEqual([]); + }); + it('can show graphics by name if it exists', () => { const rect = new ex.Rectangle({ width: 40,