Skip to content

Commit

Permalink
feat: Add ex.GraphicsComponent.remove()
Browse files Browse the repository at this point in the history
  • Loading branch information
eonarheim committed Jan 24, 2024
1 parent 1add2b6 commit 4ec8f1c
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
11 changes: 11 additions & 0 deletions sandbox/tests/camera-animation/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Camera Animation</title>
</head>
<body>

</body>
</html>
2 changes: 1 addition & 1 deletion src/engine/EntityComponentSystem/Component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
17 changes: 16 additions & 1 deletion src/engine/Graphics/GraphicsComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
*/
Expand Down
24 changes: 24 additions & 0 deletions src/spec/GraphicsComponentSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 4ec8f1c

Please sign in to comment.