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

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
robertlong committed Sep 10, 2020
2 parents ca18575 + efb4ae1 commit 48c3c65
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 30 deletions.
28 changes: 15 additions & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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
14 changes: 7 additions & 7 deletions src/System.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export interface SystemQueries {
/**
* A system that manipulates entities in the world.
*/
export abstract class System {
export abstract class System<EntityType extends Entity = Entity> {
/**
* Defines what Components the System will query for.
* This needs to be user defined.
Expand All @@ -30,22 +30,22 @@ export abstract class System {

static isSystem: true;

constructor(world: World, attributes?: Attributes);
constructor(world: World<EntityType>, attributes?: Attributes);

/**
* The results of the queries.
* Should be used inside of execute.
*/
queries: {
[queryName: string]: {
results: Entity[],
added?: Entity[],
removed?: Entity[],
changed?: Entity[],
results: EntityType[],
added?: EntityType[],
removed?: EntityType[],
changed?: EntityType[],
}
}

world: World;
world: World<EntityType>;

/**
* Whether the system will execute during the world tick.
Expand Down
2 changes: 1 addition & 1 deletion src/SystemStateComponent.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Component } from "./Component";
import { Component } from "./Component.js";

export class SystemStateComponent extends Component {}

Expand Down
2 changes: 1 addition & 1 deletion src/TagComponent.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Component } from "./Component";
import { Component } from "./Component.js";

export class TagComponent extends Component {
constructor() {
Expand Down
12 changes: 7 additions & 5 deletions src/World.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ export interface WorldOptions {
/**
* The World is the root of the ECS.
*/
export class World {
export class World<EntityType extends Entity = Entity> {

/**
* Whether the world tick should execute.
*/
Expand All @@ -28,11 +29,11 @@ export class World {
*/
registerComponent<C extends Component<any>>(Component: ComponentConstructor<C>, objectPool?: ObjectPool<C> | false): this;

/**
/**
* Evluate whether a component has been registered to this world or not.
* @param Component Type of component to to evaluate
*/
hasRegisteredComponent<C extends Component<any>>(Component: Component<C>): boolean;
hasRegisteredComponent<C extends Component<any>>(Component: ComponentConstructor<C>): boolean;

/**
* Register a system.
Expand Down Expand Up @@ -68,7 +69,7 @@ export class World {
* Resume execution of this world.
*/
play(): void

/**
* Stop execution of this world.
*/
Expand All @@ -77,5 +78,6 @@ export class World {
/**
* Create a new entity
*/
createEntity(name?: string): Entity
createEntity(name?: string): EntityType

}
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 48c3c65

Please sign in to comment.