diff --git a/app/ecs-systems/plant-growth.js b/app/ecs-systems/plant-growth.js index 2d5b98f..af78aa9 100644 --- a/app/ecs-systems/plant-growth.js +++ b/app/ecs-systems/plant-growth.js @@ -2,6 +2,8 @@ import {System} from '@/ecs/index.js'; export default class PlantGrowth extends System { + frequency = 1; + static queries() { return { default: ['Plant'], diff --git a/app/ecs-systems/water.js b/app/ecs-systems/water.js index 20ba83e..b360bd4 100644 --- a/app/ecs-systems/water.js +++ b/app/ecs-systems/water.js @@ -2,6 +2,8 @@ import {System} from '@/ecs/index.js'; export default class Water extends System { + frequency = 1; + tick(elapsed) { const {Water} = this.ecs.get(1); for (const tile in Water.water) { diff --git a/app/ecs/ecs.js b/app/ecs/ecs.js index 70b4744..7725eec 100644 --- a/app/ecs/ecs.js +++ b/app/ecs/ecs.js @@ -344,32 +344,31 @@ export default class Ecs { } tick(elapsed) { - for (const systemName in this.Systems) { - if (this.Systems[systemName].active) { - this.Systems[systemName].tick(elapsed); - } - } - for (const systemName in this.Systems) { - if (this.Systems[systemName].active) { - this.Systems[systemName].finalize(elapsed); - } - } - this.tickDestruction(); - } - - tickDestruction() { - const unique = new Set(); + const destroying = new Set(); for (const systemName in this.Systems) { const System = this.Systems[systemName]; if (System.active) { - for (let j = 0; j < System.destroying.length; j++) { - unique.add(System.destroying[j]); + if (System.frequency) { + System.elapsed += elapsed; + if (System.elapsed < System.frequency) { + continue; + } + } + while (!System.frequency || System.elapsed >= System.frequency) { + System.tick(System.frequency ? System.elapsed : elapsed); + for (let j = 0; j < System.destroying.length; j++) { + destroying.add(System.destroying[j]); + } + System.tickDestruction(); + if (!System.frequency) { + break; + } + System.elapsed -= System.frequency; } - System.tickDestruction(); } } - if (unique.size > 0) { - this.destroyMany(unique.values()); + if (destroying.size > 0) { + this.destroyMany(destroying.values()); } } diff --git a/app/ecs/ecs.test.js b/app/ecs/ecs.test.js index e51b03d..e1a84c1 100644 --- a/app/ecs/ecs.test.js +++ b/app/ecs/ecs.test.js @@ -209,13 +209,9 @@ test('creates entities when ticking systems', () => { }); test('schedules entities to be deleted when ticking systems', () => { - let entity; const ecs = new Ecs({ Systems: { Despawn: class extends System { - finalize() { - entity = ecs.get(1); - } tick() { this.destroyEntity(1); } @@ -225,8 +221,6 @@ test('schedules entities to be deleted when ticking systems', () => { ecs.system('Despawn').active = true; ecs.create(); ecs.tick(1); - expect(entity) - .to.not.be.undefined; expect(ecs.get(1)) .to.be.undefined; }); diff --git a/app/ecs/system.js b/app/ecs/system.js index 3f9454f..86895a4 100644 --- a/app/ecs/system.js +++ b/app/ecs/system.js @@ -10,6 +10,10 @@ export default class System { ecs; + elapsed = 0; + + frequency; + queries = {}; constructor(ecs) { @@ -45,8 +49,6 @@ export default class System { } } - finalize() {} - async insertComponents(entityId, components) { return this.ecs.insert(entityId, components); }