Skip to content

Commit

Permalink
temp
Browse files Browse the repository at this point in the history
  • Loading branch information
Lusito committed Jun 1, 2021
1 parent c993d46 commit e566948
Show file tree
Hide file tree
Showing 30 changed files with 5,312 additions and 7,065 deletions.
11,352 changes: 4,865 additions & 6,487 deletions package-lock.json

Large diffs are not rendered by default.

20 changes: 11 additions & 9 deletions package.json
Expand Up @@ -26,7 +26,7 @@
"scripts": {
"build": "rimraf dist && tsc -p tsconfig-build.json",
"coveralls": "cat ./coverage/lcov.info | coveralls",
"doc": "rimraf docs && typedoc --out docs --exclude \"src/*.spec.ts\" src && copyfiles -a -f \"doc-assets/*\" docs",
"doc": "typedoc && copyfiles -a -f \"doc-assets/*\" docs",
"lint": "npm run lint:style && npm run lint:es && npm run lint:package",
"lint:es": "eslint \"{src,test}/**/*.{ts,tsx}\" --ext \".ts,.tsx\" --ignore-path .prettierignore",
"lint:es:fix": "npm run lint:es -- --fix",
Expand All @@ -39,21 +39,23 @@
"test": "jest"
},
"dependencies": {
"typed-signals": "2.2.0"
"typed-signals": "^2.2.0",
"typedi": "^0.10.0"
},
"devDependencies": {
"@abraham/reflection": "^0.8.0",
"@lusito/eslint-config": "^1.5.0",
"@lusito/prettier-config": "^1.5.0",
"@lusito/stylelint-config": "^1.5.0",
"@types/jest": "^26.0.20",
"@types/jest": "^26.0.23",
"copyfiles": "^2.4.1",
"coveralls": "^3.1.0",
"eslint-plugin-jest": "^24.1.3",
"jest": "^26.6.3",
"eslint-plugin-jest": "^24.3.6",
"jest": "^27.0.3",
"rimraf": "^3.0.2",
"sort-package-json": "^1.48.1",
"ts-jest": "^26.4.4",
"typedoc": "^0.20.17",
"typescript": "^4.1.3"
"sort-package-json": "^1.50.0",
"ts-jest": "^27.0.1",
"typedoc": "^0.20.36",
"typescript": "4.2.4"
}
}
2 changes: 1 addition & 1 deletion src/core/Component.ts
Expand Up @@ -2,7 +2,7 @@ import { getConstructorFor, Constructor } from "../utils/Constructor";

/**
* Base class for all components. A Component is intended as a data holder
* and provides data to be processed in an EntitySystem.
* and provides data to be processed in an {@link EntitySystem}.
*/
export abstract class Component {
/**
Expand Down
133 changes: 62 additions & 71 deletions src/core/Engine.spec.ts
@@ -1,3 +1,6 @@
/* eslint-disable dot-notation */
import { Service } from "typedi";

import { Component } from "./Component";
import { Entity } from "./Entity";
import { Engine } from "./Engine";
Expand Down Expand Up @@ -28,58 +31,47 @@ class EntityListenerMock {
abstract class EntitySystemMockBase extends EntitySystem {
updates: number[] | null = [];

addedToEngineSpy: jest.SpyInstance;

removedFromEngineSpy: jest.SpyInstance;
id = 0;

updateSpy: jest.SpyInstance;

constructor(updates: number[] | null = null) {
constructor() {
super();
this.updates = updates;
this.addedToEngineSpy = jest.spyOn(this as any, "addedToEngine");
this.removedFromEngineSpy = jest.spyOn(this as any, "removedFromEngine");
this.onEnable = jest.fn();
this.onDisable = jest.fn();
this.updateSpy = jest.spyOn(this as any, "update");
}

public update(): void {
if (this.updates != null) this.updates.push(this.getPriority());
if (this.updates != null) this.updates.push(this.id);
}
}

@Service()
class EntitySystemMock extends EntitySystemMockBase {}

class EntitySystemMockA extends EntitySystemMockBase {
constructor(updates: number[] | null = null) {
super(updates);
}
}
@Service()
class EntitySystemMockA extends EntitySystemMockBase {}

class EntitySystemMockB extends EntitySystemMockBase {
constructor(updates: number[] | null = null) {
super(updates);
}
}
@Service()
class EntitySystemMockB extends EntitySystemMockBase {}

class CounterComponent extends Component {
counter = 0;
}

@Service()
class CounterSystem extends EntitySystem {
entities: Entity[] | null = null;

constructor(private onEntityRemove: (e: Entity) => void) {
super();
}
onEntityRemove: (e: Entity) => void = (() => {}) as any;

protected addedToEngine(engine: Engine): void {
super.addedToEngine(engine);
this.entities = engine.getEntitiesFor(Family.all(CounterComponent).get());
protected onEnable(): void {
this.entities = this.engine.getEntitiesFor(Family.all(CounterComponent).get());
}

public update() {
const engine = this.getEngine();
if (!engine) return;
const { engine } = this;
const entities = this.entities!;
for (let i = 0; i < entities.length; ++i) {
const e = entities[i];
Expand All @@ -94,16 +86,14 @@ class CounterSystem extends EntitySystem {
}
}

@Service()
class UpdateSystem<T extends (engine: Engine) => any> extends EntitySystem {
result: ReturnType<T> | null = null;

constructor(private callback: T) {
super();
}
callback: T = ((() => 0) as any) as T;

public update() {
const engine = this.getEngine();
if (engine) this.result = this.callback(engine);
this.result = this.callback(this.engine);
}
}

Expand All @@ -113,40 +103,34 @@ class PositionComponent extends Component {
y = 0.0;
}

@Service()
class CombinedSystem extends EntitySystem {
entities: Entity[] | null = null;

counter = 0;

protected addedToEngine(engine: Engine): void {
super.addedToEngine(engine);
this.entities = engine.getEntitiesFor(Family.all(PositionComponent).get());
protected onEnable(): void {
this.entities = this.engine.getEntitiesFor(Family.all(PositionComponent).get());
}

public update(): void {
const engine = this.getEngine();
if (engine) {
if (this.counter >= 6 && this.counter <= 8) engine.removeEntity(this.entities![2]);
this.counter++;
}
if (this.counter >= 6 && this.counter <= 8) this.engine.removeEntity(this.entities![2]);
this.counter++;
}
}

@Service()
class RemoveEntityTwiceSystem extends EntitySystem {
private entities: Entity[] | null = null;

constructor(private onEntityCreated: (e: Entity) => void) {
super();
}
onEntityCreated: (e: Entity) => void = (() => 0) as any;

protected addedToEngine(engine: Engine): void {
super.addedToEngine(engine);
this.entities = engine.getEntitiesFor(Family.all(PositionComponent).get());
protected onEnable(): void {
this.entities = this.engine.getEntitiesFor(Family.all(PositionComponent).get());
}

public update() {
const engine = this.getEngine();
if (!engine) return;
const { engine } = this;
for (let i = 0; i < 10; i++) {
const entity = engine.createEntity();
this.onEntityCreated(entity);
Expand All @@ -164,7 +148,8 @@ class RemoveEntityTwiceSystem extends EntitySystem {
describe("Engine", () => {
test("#isUpdating()", () => {
const engine = new Engine();
const system = engine.addSystem(new UpdateSystem((e) => e.isUpdating()));
const system = engine.addSystem(UpdateSystem);
system.callback = (e) => e.isUpdating();
engine.update(deltaTime);
expect(system.result).toBe(true);
});
Expand All @@ -174,7 +159,8 @@ describe("Engine", () => {
engine.addEntity(engine.createEntity());
engine.addEntity(engine.createEntity());
expect(engine.getEntities()).toHaveLength(2);
engine.addSystem(new UpdateSystem((e) => e.removeAllEntities()));
const system = engine.addSystem(UpdateSystem);
system.callback = (e) => e.removeAllEntities();
engine.update(deltaTime);
expect(engine.getEntities()).toHaveLength(0);
});
Expand Down Expand Up @@ -243,39 +229,39 @@ describe("Engine", () => {
expect(engine.getSystem(EntitySystemMockA)).toBe(null);
expect(engine.getSystem(EntitySystemMockB)).toBe(null);

const systemA = engine.addSystem(new EntitySystemMockA());
const systemB = engine.addSystem(new EntitySystemMockB());
const systemA = engine.addSystem(EntitySystemMockA);
const systemB = engine.addSystem(EntitySystemMockB);

expect(engine.getSystem(EntitySystemMockA)).toBe(systemA);
expect(engine.getSystem(EntitySystemMockB)).toBe(systemB);
expect(systemA.addedToEngineSpy).toHaveBeenCalledTimes(1);
expect(systemB.addedToEngineSpy).toHaveBeenCalledTimes(1);
expect(systemA["onEnable"]).toHaveBeenCalledTimes(1);
expect(systemB["onEnable"]).toHaveBeenCalledTimes(1);

engine.removeSystem(EntitySystemMockA);
engine.removeSystem(EntitySystemMockB);

expect(engine.getSystem(EntitySystemMockA)).toBe(null);
expect(engine.getSystem(EntitySystemMockB)).toBe(null);
expect(systemA.removedFromEngineSpy).toHaveBeenCalledTimes(1);
expect(systemB.removedFromEngineSpy).toHaveBeenCalledTimes(1);
expect(systemA["onEnable"]).toHaveBeenCalledTimes(1);
expect(systemB["onDisable"]).toHaveBeenCalledTimes(1);
});

test("getSystems", () => {
const engine = new Engine();

expect(engine.getSystems()).toHaveLength(0);

engine.addSystem(new EntitySystemMockA());
engine.addSystem(new EntitySystemMockB());
engine.addSystem(EntitySystemMockA);
engine.addSystem(EntitySystemMockB);

expect(engine.getSystems()).toHaveLength(2);
});

test("systemUpdate", () => {
const engine = new Engine();

const systemA = engine.addSystem(new EntitySystemMockA());
const systemB = engine.addSystem(new EntitySystemMockB());
const systemA = engine.addSystem(EntitySystemMockA);
const systemB = engine.addSystem(EntitySystemMockB);

const numUpdates = 10;

Expand Down Expand Up @@ -304,10 +290,12 @@ describe("Engine", () => {

const engine = new Engine();

engine.addSystem(new EntitySystemMockA(updates)).setPriority(2);
engine.addSystem(new EntitySystemMockB(updates)).setPriority(1);

engine.sortSystems();
const systemA = engine.addSystem(EntitySystemMockA, 2);
systemA.id = 2;
systemA.updates = updates;
const systemB = engine.addSystem(EntitySystemMockB, 1);
systemB.id = 1;
systemB.updates = updates;

engine.update(deltaTime);

Expand All @@ -323,12 +311,12 @@ describe("Engine", () => {
test("ignoreSystem", () => {
const engine = new Engine();

const system = engine.addSystem(new EntitySystemMock());
const system = engine.addSystem(EntitySystemMock);

const numUpdates = 10;

for (let i = 0; i < numUpdates; ++i) {
system.setProcessing(i % 2 === 0);
engine.setSystemEnabled(EntitySystemMock, i % 2 === 0);
engine.update(deltaTime);
expect(system.updateSpy).toHaveBeenCalledTimes(Math.floor(i / 2) + 1);
}
Expand Down Expand Up @@ -508,7 +496,8 @@ describe("Engine", () => {
test("entitySystemRemovalWhileIterating", () => {
const engine = new Engine();

engine.addSystem(new CounterSystem((e) => expect(e.isScheduledForRemoval()).toBe(true)));
const system = engine.addSystem(CounterSystem);
system.onEntityRemove = (e) => expect(e.isScheduledForRemoval()).toBe(true);

for (let i = 0; i < 20; ++i) {
const entity = engine.createEntity();
Expand Down Expand Up @@ -618,7 +607,8 @@ describe("Engine", () => {

test("createManyEntitiesNoStackOverflow", () => {
const engine = new Engine();
engine.addSystem(new CounterSystem((e) => expect(e.isScheduledForRemoval()).toBe(true)));
const system = engine.addSystem(CounterSystem);
system.onEntityRemove = (e) => expect(e.isScheduledForRemoval()).toBe(true);

for (let i = 0; i < 15000; i++) {
const e = engine.createEntity();
Expand Down Expand Up @@ -684,12 +674,12 @@ describe("Engine", () => {
const engine = new Engine();

expect(engine.getSystems()).toHaveLength(0);
const system1 = engine.addSystem(new EntitySystemMockA());
const system1 = engine.addSystem(EntitySystemMockA);

expect(engine.getSystems()).toHaveLength(1);
expect(engine.getSystem(EntitySystemMockA)).toBe(system1);

const system2 = engine.addSystem(new EntitySystemMockA());
const system2 = engine.addSystem(EntitySystemMockA);

expect(engine.getSystems()).toHaveLength(1);
expect(engine.getSystem(EntitySystemMockA)).toBe(system2);
Expand All @@ -698,7 +688,7 @@ describe("Engine", () => {
test("entityRemovalListenerOrder", () => {
const engine = new Engine();

const combinedSystem = engine.addSystem(new CombinedSystem());
const combinedSystem = engine.addSystem(CombinedSystem);

const signal = engine.getEntityRemovedSignal(Family.all(PositionComponent).get());
signal.connect((entity) => {
Expand All @@ -720,7 +710,8 @@ describe("Engine", () => {

test("removeEntityTwice", () => {
const engine = new Engine();
engine.addSystem(new RemoveEntityTwiceSystem((e) => expect(e.flags).toBe(0)));
const system = engine.addSystem(RemoveEntityTwiceSystem);
system.onEntityCreated = (e) => expect(e.flags).toBe(0);

for (let j = 0; j < 2; j++) engine.update(0);
});
Expand Down

0 comments on commit e566948

Please sign in to comment.