Skip to content

Commit

Permalink
fix: allow entity names to be set at any time (#2775)
Browse files Browse the repository at this point in the history
Fixes a minor annoyance where you cannot set an entities name after construction
  • Loading branch information
eonarheim authored Sep 30, 2023
1 parent 28d720c commit 1f0b134
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 5 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).

### Changed

-
- Allow entity names to be set after construction! Entities will now default to a name "Entity#1234" followed by an id.

<!--------------------------------- DO NOT EDIT BELOW THIS LINE --------------------------------->
<!--------------------------------- DO NOT EDIT BELOW THIS LINE --------------------------------->
Expand Down
6 changes: 6 additions & 0 deletions src/engine/EntityComponentSystem/Entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,18 @@ export class Entity implements OnInitialize, OnPreUpdate, OnPostUpdate {
protected _setName(name: string) {
if (name) {
this._name = name;
} else {
this._name = `Entity#${this.id}`;
}
}
public get name(): string {
return this._name;
}

public set name(name: string) {
this._setName(name);
}

/**
* Whether this entity is active, if set to false it will be reclaimed
*/
Expand Down
Binary file modified src/engine/Graphics/Context/debug-font.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions src/engine/Graphics/Context/debug-text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ export class DebugText {
this._spriteSheet = SpriteSheet.fromImageSource({
image: this._imageSource,
grid: {
rows: 3,
rows: 4,
columns: 16,
spriteWidth: 16,
spriteHeight: 16
}
});
this._spriteFont = new SpriteFont({
alphabet: '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ,!\'&."?-()+ ',
alphabet: '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ,!\'&."?-()+# ',
caseInsensitive: true,
spriteSheet: this._spriteSheet,
spacing: -6
Expand Down
2 changes: 1 addition & 1 deletion src/spec/EntityManagerSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ describe('An EntityManager', () => {
entityManager.addEntity(entity);
entityManager.addEntity(entity2);
expect(entityManager.getByName('some-e')).toEqual([entity]);
expect(entityManager.getByName('anonymous')).toEqual([entity2]);
expect(entityManager.getByName(entity2.name)).toEqual([entity2]);
});

it('can clear entities', () => {
Expand Down
8 changes: 7 additions & 1 deletion src/spec/EntitySpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,15 @@ describe('An entity', () => {
expect(e.name).toBe('my-name');
});

it('can have a name set after construction', () => {
const e = new ex.Entity();
e.name = 'MyCoolName';
expect(e.name).toBe('MyCoolName');
});

it('has a default name', () => {
const e = new ex.Entity();
expect(e.name).toBe('anonymous');
expect(e.name).toMatch(/^Entity#\d+$/);
});

it('can be killed', () => {
Expand Down

0 comments on commit 1f0b134

Please sign in to comment.