Skip to content

Commit ad55ade

Browse files
committed
feat: Add ReactiveRenderSystem, ReactiveSyncSystem, and ReactiveUpdateSystem classes with event system integration
1 parent 92f8411 commit ad55ade

File tree

6 files changed

+50
-10
lines changed

6 files changed

+50
-10
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { EventSystem } from "@/core/events/EventSystem";
2+
import { GameCoreService } from "@/core/service/GameCoreService";
3+
import { RenderSystem } from "@/core/ecs/RenderSystem";
4+
5+
export abstract class ReactiveRenderSystem extends RenderSystem {
6+
@GameCoreService(EventSystem)
7+
protected eventSystem!: EventSystem;
8+
9+
public execute(elapsed: number, frame: number): void {}
10+
}

src/core/ecs/ReactiveSyncSystem.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { EventSystem } from "@/core/events/EventSystem";
2+
import { GameCoreService } from "@/core/service/GameCoreService";
3+
import { SyncSystem } from "@/core/ecs/SyncSystem";
4+
5+
export abstract class ReactiveSyncSystem extends SyncSystem {
6+
@GameCoreService(EventSystem)
7+
protected eventSystem!: EventSystem;
8+
9+
public execute(elapsed: number, frame: number): void {}
10+
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import { EventSystem } from "@/core/events/EventSystem";
22
import { GameCoreService } from "@/core/service/GameCoreService";
33
import { UpdateSystem } from "@/core/ecs/UpdateSystem";
44

5-
export abstract class ReactiveSystem extends UpdateSystem {
5+
export abstract class ReactiveUpdateSystem extends UpdateSystem {
66
@GameCoreService(EventSystem)
77
protected eventSystem!: EventSystem;
88

99
public execute(elapsed: number, frame: number): void {}
10-
}
10+
}

src/core/ecs/SyncSystem.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { System } from "./System";
2+
3+
export abstract class SyncSystem extends System {}

src/core/ecs/World.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { GameStateConstructor } from "@/core/GameState";
1111
import { GameCoreService } from "@/core/service/GameCoreService";
1212
import { EventSystem } from "@/core/events/EventSystem";
1313
import { binaryInsert } from "@/core/utils/Arrays";
14+
import { SyncSystem } from "./SyncSystem";
1415

1516
@GameCoreService()
1617
export class World {
@@ -20,6 +21,7 @@ export class World {
2021
private readonly systems: Map<string, System>;
2122

2223
private updateSchedule: System[];
24+
private syncSchedule: System[];
2325
private renderSchedule: System[];
2426

2527
@GameCoreService(EventSystem)
@@ -32,6 +34,7 @@ export class World {
3234
this.systems = new Map<string, System>();
3335

3436
this.updateSchedule = [];
37+
this.syncSchedule = [];
3538
this.renderSchedule = [];
3639
}
3740

@@ -41,6 +44,12 @@ export class World {
4144
system.execute(elapsed, frame);
4245
}
4346
}
47+
48+
for (const system of this.syncSchedule) {
49+
if (system.isEnabled()) {
50+
system.execute(elapsed, frame);
51+
}
52+
}
4453
}
4554

4655
public render(elapsed: number, frame: number) {
@@ -186,6 +195,8 @@ export class World {
186195
private scheduleSystem(system: System) {
187196
if (system instanceof UpdateSystem) {
188197
binaryInsert(this.updateSchedule, system, System.byPriority);
198+
} else if (system instanceof SyncSystem) {
199+
binaryInsert(this.syncSchedule, system, System.byPriority);
189200
} else if (system instanceof RenderSystem) {
190201
binaryInsert(this.renderSchedule, system, System.byPriority);
191202
} else {
@@ -205,6 +216,8 @@ export class World {
205216
private unscheduleSystem(system: System) {
206217
if (system instanceof UpdateSystem) {
207218
this.updateSchedule = this.updateSchedule.filter(s => s !== system);
219+
} else if (system instanceof SyncSystem) {
220+
this.syncSchedule = this.syncSchedule.filter(s => s !== system);
208221
} else if (system instanceof RenderSystem) {
209222
this.renderSchedule = this.renderSchedule.filter(s => s !== system);
210223
} else {
@@ -226,6 +239,10 @@ export class World {
226239
return this.updateSchedule;
227240
}
228241

242+
public getSyncSchedule(): System[] {
243+
return this.syncSchedule;
244+
}
245+
229246
public getRenderSchedule(): System[] {
230247
return this.renderSchedule;
231248
}

test/core/ecs/ReactiveSystem.spec.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { test, expect, suite, vi } from "vitest";
2-
import { ReactiveSystem } from "@/core/ecs/ReactiveSystem";
2+
import { ReactiveUpdateSystem } from "@/core/ecs/ReactiveUpdateSystem";
33
import { Query } from "@/core/ecs/Query";
44
import { ServiceRegistry } from "@/core/service/ServiceRegistry";
55
import { EventSystem } from "@/core/events/EventSystem";
@@ -9,14 +9,14 @@ import { Entity } from "@/core/ecs/Entity";
99
suite("ReactiveSystem Test Suite", () => {
1010
let entityChanged = false;
1111

12-
class TestReactiveSystem extends ReactiveSystem {
12+
class TestReactiveSystem extends ReactiveUpdateSystem {
1313
queries = {
1414
query: new Query({
1515
allowlist: [],
1616
blocklist: []
1717
})
1818
};
19-
19+
2020
public initialize(): void {
2121
this.eventSystem.subscribeOnce("entityChanged", () => {
2222
entityChanged = true;
@@ -43,10 +43,10 @@ suite("ReactiveSystem Test Suite", () => {
4343

4444
test("ReactiveSystem execute method does nothing by default", () => {
4545
const system = new TestReactiveSystem(0);
46-
46+
4747
// Execute should not throw and does nothing
4848
expect(() => system.execute(16, 1)).not.toThrow();
49-
49+
5050
// Verify it was called (even though it does nothing)
5151
const executeSpy = vi.spyOn(system, 'execute');
5252
system.execute(16, 1);
@@ -57,17 +57,17 @@ suite("ReactiveSystem Test Suite", () => {
5757
const system = new TestReactiveSystem(5);
5858

5959
expect(system.isEnabled()).toBeTruthy();
60-
60+
6161
system.disable();
6262
expect(system.isEnabled()).toBeFalsy();
63-
63+
6464
system.enable();
6565
expect(system.isEnabled()).toBeTruthy();
6666
});
6767

6868
test("ReactiveSystem can access queries", () => {
6969
const system = new TestReactiveSystem(0);
70-
70+
7171
expect(system.queries).toBeDefined();
7272
expect(system.queries.query).toBeInstanceOf(Query);
7373
});

0 commit comments

Comments
 (0)