Skip to content

Commit

Permalink
feat(ecs): set container when adding component to entity
Browse files Browse the repository at this point in the history
  • Loading branch information
ruggero-visintin committed Dec 7, 2023
1 parent e460c4d commit 4e80c04
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/ecs/entities/BaseEntity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export class BaseEntity implements IEntity {

public addComponent(key: string, component: IComponent): void {
this.components.set(key, component);
component.setContainer(this);
}

public getComponent<Component extends IComponent>(key: string): Component | undefined {
Expand Down
13 changes: 12 additions & 1 deletion test/unit/ecs/entities/BaseEntity.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
import { BaseComponent, BaseEntity } from "../../../../src";

describe('ecs/entities/BaseEntity', () => {
const baseEntity = new BaseEntity();
let baseEntity = new BaseEntity();

beforeEach(() => {
baseEntity = new BaseEntity();
});

describe('.addComponent()', () => {
it('Should add component to entity', () => {
const testComponent = new BaseComponent();

baseEntity.addComponent('testComponent', testComponent);
expect(baseEntity.getComponent<BaseComponent>('testComponent')).toEqual(testComponent);
});

it('Should set the entity as container of the given component', () => {
const testComponent = new BaseComponent();

baseEntity.addComponent('testComponent', testComponent);
expect(testComponent.getContainer()).toEqual(baseEntity);
})
})

describe('.getComponent()', () => {
Expand Down

0 comments on commit 4e80c04

Please sign in to comment.