Skip to content
This repository has been archived by the owner on Jun 1, 2022. It is now read-only.

Commit

Permalink
Merge pull request #253 from DavidPeicho/fix/remove-entity-components
Browse files Browse the repository at this point in the history
Fix entity not calling `dispose` on components when removed
  • Loading branch information
robertlong authored Sep 10, 2020
2 parents 498fb64 + 3f89706 commit 3d626a1
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
3 changes: 1 addition & 2 deletions src/EntityManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ export class EntityManager {
if (!~index) throw new Error("Tried to remove entity not in list");

entity.alive = false;
this.entityRemoveAllComponents(entity, immediately);

if (entity.numStateComponents === 0) {
// Remove from entity list
Expand All @@ -215,8 +216,6 @@ export class EntityManager {
this.entitiesToRemove.push(entity);
}
}

this.entityRemoveAllComponents(entity, immediately);
}

_releaseEntity(entity, index) {
Expand Down
38 changes: 37 additions & 1 deletion test/unit/entitymanager.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import test from "ava";
import { World } from "../../src";
import { Component, World } from "../../src";

test("entity id", (t) => {
var world = new World();
Expand Down Expand Up @@ -43,3 +43,39 @@ test("deferred entity remove", (t) => {
t.is(world.entityManager.count(), 0);
t.is(world.entityManager.entitiesToRemove.length, 0);
});

test("remove entity clears and reset components first ", (t) => {
class MyComponent extends Component {
constructor() {
super();
this.isReset = false;
}
dispose() {
this.isReset = true;
super.dispose();
}
}
const world = new World();
world.registerComponent(MyComponent, false);

let entity = world.createEntity();
entity.addComponent(MyComponent);

let component = entity.getComponent(MyComponent);
t.is(component.isReset, false);

// Deletes component immeditatly.
entity.remove(true);
t.is(component.isReset, true);

// Deletes component is a deferred manner.

entity = world.createEntity();
entity.addComponent(MyComponent);
component = entity.getComponent(MyComponent);
t.is(component.isReset, false);

entity.remove();
world.entityManager.processDeferredRemoval();
t.is(component.isReset, true);
});

0 comments on commit 3d626a1

Please sign in to comment.