Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions packages/ecs/lib/libecs.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
// TypeScript bindings for emscripten-generated code. Automatically generated at compile time.
declare namespace RuntimeExports {
let HEAPF32: any;
let HEAPF64: any;
let HEAP_DATA_VIEW: any;
let HEAP8: any;
let HEAPU8: any;
let HEAP16: any;
let HEAPU16: any;
let HEAP32: any;
let HEAPU32: any;
let HEAP64: any;
let HEAPU64: any;
}
interface WasmModule {
}

Expand All @@ -7,8 +20,6 @@ export interface ClassHandle {
delete(): void;
deleteLater(): this;
isDeleted(): boolean;
// @ts-ignore - If targeting lower than ESNext, this symbol might not exist.
[Symbol.dispose](): void;
clone(): this;
}
export interface container extends ClassHandle {
Expand Down Expand Up @@ -47,15 +58,15 @@ export interface Registry extends ClassHandle {
killEntity(_0: Entity): void;
clearEntities(): void;
removeComponent(_0: Entity, _1: {name: string, [key: string]: any}): void;
addSystem(_0: (registry: Registry) => void): void;
runSystems(): void;
addSystem(_0: (registry: Registry, ctx: any) => void): void;
clearSystems(): void;
entityFromIndex(_0: number): Entity;
removeSystem(_0: number): void;
maxEntities(): number;
getEntityComponentConst(_0: Entity, _1: {name: string, [key: string]: any}): any | undefined;
getEntityComponent(_0: Entity, _1: {name: string, [key: string]: any}): any | undefined;
addComponent(_0: Entity, _1: {name: string, [key: string]: any}): any | undefined;
runSystems(_0: any): void;
getZipper(_0: any): any;
}

Expand All @@ -74,5 +85,5 @@ interface EmbindModule {
};
}

export type MainModule = WasmModule & EmbindModule;
export type MainModule = WasmModule & typeof RuntimeExports & EmbindModule;
export default function MainModuleFactory (options?: unknown): Promise<MainModule>;
9 changes: 5 additions & 4 deletions packages/ecs/src/ecs-library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {

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

export type Component = { name: string; [key: string]: any };
export type System = (registry: Registry) => void;
Expand Down Expand Up @@ -36,8 +37,8 @@ export class ECSLibrary extends BaseComponentSystemLibrary {
this.registry = new this.module.Registry();
}

async run(): Promise<void> {
this.runSystems();
async run(ctx: ECSContext): Promise<void> {
this.runSystems(ctx);
}

addComponent(entity: Entity, component: Component): void {
Expand Down Expand Up @@ -68,8 +69,8 @@ export class ECSLibrary extends BaseComponentSystemLibrary {
this.registry.clearEntities();
}

runSystems(): void {
this.registry.runSystems();
runSystems(ctx: ECSContext): void {
this.registry.runSystems(ctx);
}

clearSystems(): void {
Expand Down
6 changes: 3 additions & 3 deletions packages/ecs/test/wasm/Registry.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ describe("Registry", () => {

for (let i = 0; i <= 15; i++) {
expect(counter).toBe(i);
r.runSystems();
r.runSystems(null);
}
expect(counter).toBe(16);
});
Expand Down Expand Up @@ -154,12 +154,12 @@ describe("Registry", () => {
expect(r.getComponents(Position).get(e.getId())).toStrictEqual(new Position(-2, -2));
expect(r.getComponents(Position).get(e2.getId())).toStrictEqual(new Position(2, 2));
expect(r.getComponents(Position).get(e3.getId())).toStrictEqual(new Position(0, 0));
r.runSystems();
r.runSystems(null);

expect(r.getComponents(Position).get(e.getId())).toStrictEqual(new Position(-1, -1));
expect(r.getComponents(Position).get(e2.getId())).toStrictEqual(new Position(1, 1));
expect(r.getComponents(Position).get(e3.getId())).toStrictEqual(new Position(0, 0));
r.runSystems();
r.runSystems(null);

expect(r.getComponents(Position).get(e.getId())).toStrictEqual(new Position(0, 0));
expect(r.getComponents(Position).get(e2.getId())).toStrictEqual(new Position(0, 0));
Expand Down
2 changes: 1 addition & 1 deletion packages/ecs/wasm/Registry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace nfo {
EMSCRIPTEN_BINDINGS(Registry)
{
emscripten::register_type<Component>("{name: string, [key: string]: any}");
emscripten::register_type<System>("(registry: Registry) => void");
emscripten::register_type<System>("(registry: Registry, ctx: any) => void");

emscripten::class_<Registry>("Registry")
.constructor()
Expand Down
10 changes: 5 additions & 5 deletions packages/ecs/wasm/Registry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,11 @@ namespace nfo {
_systems.clear();
}

void run_systems()
void run_systems(const emscripten::val &ctx)
{
std::vector<std::function<void(Registry &)>> systems_copy = _systems;
for (std::function<void(Registry &)> &system : systems_copy)
system(*this);
std::vector<std::function<void(Registry &, const emscripten::val &)>> systems_copy = _systems;
for (std::function<void(Registry &, const emscripten::val &)> &system : systems_copy)
system(*this, ctx);
}

void log(const Entity &entity) const
Expand Down Expand Up @@ -214,7 +214,7 @@ namespace nfo {

std::unordered_map<std::string, std::function<void(Registry &, Entity const &)>> _remove_functions;
std::unordered_map<std::string, std::function<bool(const Registry &, const Entity &)>> _loggers;
std::vector<std::function<void(Registry &)>> _systems;
std::vector<std::function<void(Registry &, const emscripten::val &)>> _systems;

std::vector<Entity> _dead_entities;
std::size_t _next_entity{0};
Expand Down
Loading