Skip to content

Commit 4553209

Browse files
committed
feat(ecs): add ecs context to the systems
1 parent f97521b commit 4553209

File tree

5 files changed

+28
-17
lines changed

5 files changed

+28
-17
lines changed

packages/ecs/lib/libecs.d.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,17 @@
11
// TypeScript bindings for emscripten-generated code. Automatically generated at compile time.
2+
declare namespace RuntimeExports {
3+
let HEAPF32: any;
4+
let HEAPF64: any;
5+
let HEAP_DATA_VIEW: any;
6+
let HEAP8: any;
7+
let HEAPU8: any;
8+
let HEAP16: any;
9+
let HEAPU16: any;
10+
let HEAP32: any;
11+
let HEAPU32: any;
12+
let HEAP64: any;
13+
let HEAPU64: any;
14+
}
215
interface WasmModule {
316
}
417

@@ -7,8 +20,6 @@ export interface ClassHandle {
720
delete(): void;
821
deleteLater(): this;
922
isDeleted(): boolean;
10-
// @ts-ignore - If targeting lower than ESNext, this symbol might not exist.
11-
[Symbol.dispose](): void;
1223
clone(): this;
1324
}
1425
export interface container extends ClassHandle {
@@ -47,15 +58,15 @@ export interface Registry extends ClassHandle {
4758
killEntity(_0: Entity): void;
4859
clearEntities(): void;
4960
removeComponent(_0: Entity, _1: {name: string, [key: string]: any}): void;
50-
addSystem(_0: (registry: Registry) => void): void;
51-
runSystems(): void;
61+
addSystem(_0: (registry: Registry, ctx: any) => void): void;
5262
clearSystems(): void;
5363
entityFromIndex(_0: number): Entity;
5464
removeSystem(_0: number): void;
5565
maxEntities(): number;
5666
getEntityComponentConst(_0: Entity, _1: {name: string, [key: string]: any}): any | undefined;
5767
getEntityComponent(_0: Entity, _1: {name: string, [key: string]: any}): any | undefined;
5868
addComponent(_0: Entity, _1: {name: string, [key: string]: any}): any | undefined;
69+
runSystems(_0: any): void;
5970
getZipper(_0: any): any;
6071
}
6172

@@ -74,5 +85,5 @@ interface EmbindModule {
7485
};
7586
}
7687

77-
export type MainModule = WasmModule & EmbindModule;
88+
export type MainModule = WasmModule & typeof RuntimeExports & EmbindModule;
7889
export default function MainModuleFactory (options?: unknown): Promise<MainModule>;

packages/ecs/src/ecs-library.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ import { type AssetManagerLibrary } from "@nanoforge/asset-manager";
22
import {
33
ASSET_MANAGER_LIBRARY,
44
BaseComponentSystemLibrary,
5-
type ExecutionContext,
65
GRAPHICS_LIBRARY,
76
type InitContext,
87
} from "@nanoforge/common";
98

109
import type { Entity, MainModule, Registry, SparseArray } from "../lib";
1110
import { Module } from "../lib";
11+
import { type ECSContext } from "./ecs-context.type";
1212

1313
export type Component = { name: string; [key: string]: any };
1414
export type System = (registry: Registry) => void;
@@ -37,7 +37,7 @@ export class ECSLibrary extends BaseComponentSystemLibrary {
3737
this.registry = new this.module.Registry();
3838
}
3939

40-
async run(ctx: ExecutionContext<this>): Promise<void> {
40+
async run(ctx: ECSContext): Promise<void> {
4141
this.runSystems(ctx);
4242
}
4343

@@ -69,7 +69,7 @@ export class ECSLibrary extends BaseComponentSystemLibrary {
6969
this.registry.clearEntities();
7070
}
7171

72-
runSystems(ctx: ExecutionContext<this>): void {
72+
runSystems(ctx: ECSContext): void {
7373
this.registry.runSystems(ctx);
7474
}
7575

packages/ecs/test/wasm/Registry.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ describe("Registry", () => {
114114

115115
for (let i = 0; i <= 15; i++) {
116116
expect(counter).toBe(i);
117-
r.runSystems();
117+
r.runSystems(null);
118118
}
119119
expect(counter).toBe(16);
120120
});
@@ -154,12 +154,12 @@ describe("Registry", () => {
154154
expect(r.getComponents(Position).get(e.getId())).toStrictEqual(new Position(-2, -2));
155155
expect(r.getComponents(Position).get(e2.getId())).toStrictEqual(new Position(2, 2));
156156
expect(r.getComponents(Position).get(e3.getId())).toStrictEqual(new Position(0, 0));
157-
r.runSystems();
157+
r.runSystems(null);
158158

159159
expect(r.getComponents(Position).get(e.getId())).toStrictEqual(new Position(-1, -1));
160160
expect(r.getComponents(Position).get(e2.getId())).toStrictEqual(new Position(1, 1));
161161
expect(r.getComponents(Position).get(e3.getId())).toStrictEqual(new Position(0, 0));
162-
r.runSystems();
162+
r.runSystems(null);
163163

164164
expect(r.getComponents(Position).get(e.getId())).toStrictEqual(new Position(0, 0));
165165
expect(r.getComponents(Position).get(e2.getId())).toStrictEqual(new Position(0, 0));

packages/ecs/wasm/Registry.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ namespace nfo {
2323
EMSCRIPTEN_BINDINGS(Registry)
2424
{
2525
emscripten::register_type<Component>("{name: string, [key: string]: any}");
26-
emscripten::register_type<System>("(registry: Registry) => void");
26+
emscripten::register_type<System>("(registry: Registry, ctx: any) => void");
2727

2828
emscripten::class_<Registry>("Registry")
2929
.constructor()

packages/ecs/wasm/Registry.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -159,11 +159,11 @@ namespace nfo {
159159
_systems.clear();
160160
}
161161

162-
void run_systems()
162+
void run_systems(const emscripten::val &ctx)
163163
{
164-
std::vector<std::function<void(Registry &)>> systems_copy = _systems;
165-
for (std::function<void(Registry &)> &system : systems_copy)
166-
system(*this);
164+
std::vector<std::function<void(Registry &, const emscripten::val &)>> systems_copy = _systems;
165+
for (std::function<void(Registry &, const emscripten::val &)> &system : systems_copy)
166+
system(*this, ctx);
167167
}
168168

169169
void log(const Entity &entity) const
@@ -214,7 +214,7 @@ namespace nfo {
214214

215215
std::unordered_map<std::string, std::function<void(Registry &, Entity const &)>> _remove_functions;
216216
std::unordered_map<std::string, std::function<bool(const Registry &, const Entity &)>> _loggers;
217-
std::vector<std::function<void(Registry &)>> _systems;
217+
std::vector<std::function<void(Registry &, const emscripten::val &)>> _systems;
218218

219219
std::vector<Entity> _dead_entities;
220220
std::size_t _next_entity{0};

0 commit comments

Comments
 (0)