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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -227,3 +227,5 @@ dist
.nx/
.cursor/rules/nx-rules.mdc
.github/instructions/nx.instructions.md

compile_commands.json
4 changes: 2 additions & 2 deletions example/pong/src/collisions.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { type ECSRegistry } from "@nanoforge/ecs";
import { type Registry } from "@nanoforge/ecs";

import { Hitbox, Position } from "./components";

export function checkCollisions(registry: ECSRegistry, entity: any) {
export function checkCollisions(registry: Registry, entity: any) {
const entities = registry.getZipper([Hitbox, Position]);

const { x, y } = entity.Position;
Expand Down
12 changes: 6 additions & 6 deletions example/pong/src/systems.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { type Context } from "@nanoforge/common";
import { type ECSRegistry } from "@nanoforge/ecs";
import { type Registry } from "@nanoforge/ecs";
import { type InputLibrary } from "@nanoforge/input";
import { type SoundLibrary } from "@nanoforge/sound";

Expand All @@ -14,7 +14,7 @@ import {
Velocity,
} from "./components";

export function move(registry: ECSRegistry) {
export function move(registry: Registry) {
const entities = registry.getZipper([Bounce, Position, Velocity]);

entities.forEach((entity) => {
Expand All @@ -23,7 +23,7 @@ export function move(registry: ECSRegistry) {
});
}

export function bounce(registry: ECSRegistry, ctx: Context) {
export function bounce(registry: Registry, ctx: Context) {
const entities = registry.getZipper([Bounce, Position, Velocity]);

entities.forEach((entity) => {
Expand All @@ -39,7 +39,7 @@ export function bounce(registry: ECSRegistry, ctx: Context) {
});
}

export function controlPlayer(registry: ECSRegistry, ctx: Context) {
export function controlPlayer(registry: Registry, ctx: Context) {
const entities = registry.getZipper([Controller, Position, Hitbox, Velocity]);

entities.forEach((entity) => {
Expand All @@ -62,7 +62,7 @@ export function controlPlayer(registry: ECSRegistry, ctx: Context) {
});
}

export function drawCircle(registry: ECSRegistry) {
export function drawCircle(registry: Registry) {
const entities = registry.getZipper([CircleComponent, Position]);

entities.forEach((entity) => {
Expand All @@ -71,7 +71,7 @@ export function drawCircle(registry: ECSRegistry) {
});
}

export function moveRectangle(registry: ECSRegistry) {
export function moveRectangle(registry: Registry) {
const entities = registry.getZipper([RectangleComponent, Position, Hitbox]);

entities.forEach((entity) => {
Expand Down
3 changes: 2 additions & 1 deletion packages/ecs/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,9 @@
# .idea/modules
# *.iml
# *.ipr
!build/

# CMake
# CMakeg
cmake-build-*/

# Mongo Explorer plugin
Expand Down
1 change: 1 addition & 0 deletions packages/ecs/.prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ bun.lock

*.js
*.d.ts
build
10 changes: 8 additions & 2 deletions packages/ecs/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ $(HTML_NAME): $(OBJ)
$(TS_NAME): LDFLAGS += -s MODULARIZE=1 -s EXPORT_ES6=1 -s ENVIRONMENT=web
$(TS_NAME): $(OBJ)
@mkdir -p $(OUT_DIR)
$(CC) $(OBJ) $(LDFLAGS) --emit-tsd $(TS_NAME) -o $(JS_NAME)
$(CC) $(OBJ) $(LDFLAGS) --emit-tsd libecs-tmp.d.ts -o $(JS_NAME)
cat build/pre.ts > $(OUT_DIR)/$(TS_NAME)
cat lib/libecs-tmp.d.ts >> $(OUT_DIR)/$(TS_NAME)
$(RM) lib/libecs-tmp.d.ts

clean:
$(RM) $(OBJ)
Expand All @@ -57,7 +60,10 @@ fclean: clean
tests: LDFLAGS += -s MODULARIZE=1
tests: $(OBJ)
@mkdir -p $(OUT_DIR)
$(CC) $(OBJ) $(LDFLAGS) --emit-tsd $(TS_NAME) -o $(JS_NAME)
$(CC) $(OBJ) $(LDFLAGS) --emit-tsd libecs-tmp.d.ts -o $(JS_NAME)
cat build/pre.ts > $(OUT_DIR)/$(TS_NAME)
cat lib/libecs-tmp.d.ts >> $(OUT_DIR)/$(TS_NAME)
$(RM) lib/libecs-tmp.d.ts

re: fclean all

Expand Down
1 change: 1 addition & 0 deletions packages/ecs/build/pre.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import { Context } from "@nanoforge/common"
2 changes: 1 addition & 1 deletion packages/ecs/eslint.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import config from "@nanoforge/utils-eslint-config";

export default config;
export default [...config, { ignores: ["build"] }];
54 changes: 24 additions & 30 deletions packages/ecs/lib/libecs.d.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,15 @@
import { Context } from "@nanoforge/common";

// 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 {
}
interface WasmModule {}

export interface ClassHandle {
isAliasOf(other: ClassHandle): boolean;
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 @@ -50,40 +40,44 @@ export interface Entity extends ClassHandle {
getId(): number;
}

type Component = { name: string; [key: string]: any };

type System = (registry: Registry, ctx: Context) => void;

export interface Registry extends ClassHandle {
registerComponent(_0: {name: string, [key: string]: any}): SparseArray;
getComponentsConst(_0: {name: string, [key: string]: any}): SparseArray;
getComponents(_0: {name: string, [key: string]: any}): SparseArray;
registerComponent(_0: Component): SparseArray;
getComponentsConst(_0: Component): SparseArray;
getComponents(_0: Component): SparseArray;
spawnEntity(): Entity;
getZipper(_0: Component[]): any[];
killEntity(_0: Entity): void;
clearEntities(): void;
removeComponent(_0: Entity, _1: {name: string, [key: string]: any}): void;
addSystem(_0: (registry: Registry, ctx: any) => void): void;
removeComponent(_0: Entity, _1: Component): void;
addSystem(_0: System): 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;
getEntityComponentConst(_0: Entity, _1: Component): any | undefined;
getEntityComponent(_0: Entity, _1: Component): any | undefined;
addComponent(_0: Entity, _1: Component): any | undefined;
runSystems(_0: any): void;
getZipper(_0: any): any;
}

interface EmbindModule {
container: {
new(): container;
new (): container;
};
SparseArray: {
new(): SparseArray;
new (): SparseArray;
};
Entity: {
new(_0: number): Entity;
new (_0: number): Entity;
};
Registry: {
new(): Registry;
new (): Registry;
};
}

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

import { type MainModule, Module } from "../lib";
import { ECSRegistry } from "./ecs-registry";
import type { MainModule, Registry } from "../lib";
import { Module } from "../lib";

export class ECSLibrary extends BaseComponentSystemLibrary {
private module: MainModule;
private _registry: ECSRegistry;
private _registry: Registry;

private readonly path: string = "libecs.wasm";

Expand All @@ -29,14 +29,14 @@ export class ECSLibrary extends BaseComponentSystemLibrary {
async __init(context: InitContext): Promise<void> {
const wasmFile = context.libraries.getAssetManager().library.getAsset(this.path);
this.module = await Module({ locateFile: () => wasmFile.path });
this._registry = new ECSRegistry(new this.module.Registry());
this._registry = new this.module.Registry();
}

async __run(ctx: Context): Promise<void> {
this._registry.runSystems(ctx);
}

get registry(): ECSRegistry {
get registry(): Registry {
return this._registry;
}
}
78 changes: 0 additions & 78 deletions packages/ecs/src/ecs-registry.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/ecs/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import "@lib/libecs.wasm";

export { ECSLibrary } from "./ecs-library";
export type { ECSRegistry, Component, System } from "./ecs-registry";
export type { Component, System, Registry } from "../lib";
5 changes: 3 additions & 2 deletions packages/ecs/test/ecs-library.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import { AssetManagerLibrary } from "@nanoforge/asset-manager";
import { ClearContext, type IConfigRegistry, InitContext } from "@nanoforge/common";
import { EditableApplicationContext } from "@nanoforge/core/src/common/context/contexts/application.editable-context";
import { EditableLibraryManager } from "@nanoforge/core/src/common/library/manager/library.manager";
import { type ECSRegistry } from "@nanoforge/ecs";
import { ECSLibrary } from "@nanoforge/ecs/src/ecs-library";

import { type Registry } from "../lib";

class Position {
name: string = "Position";
constructor(
Expand All @@ -18,7 +19,7 @@ class Position {

describe("ECSLibrary", () => {
let ecs: ECSLibrary;
let registry: ECSRegistry;
let registry: Registry;
const assetManager = new AssetManagerLibrary();
const libraryManager = new EditableLibraryManager();
const appContext = new EditableApplicationContext(libraryManager);
Expand Down
2 changes: 1 addition & 1 deletion packages/ecs/tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"@nanoforge/common": ["../common"]
}
},
"exclude": ["node_modules", "dist", "test/**/*", "*.spec.ts"],
"exclude": ["node_modules", "dist", "test/**/*", "*.spec.ts", "build"],
"references": [
{
"path": "../common/tsconfig.build.json"
Expand Down
6 changes: 4 additions & 2 deletions packages/ecs/wasm/Registry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ namespace nfo {

EMSCRIPTEN_BINDINGS(Registry)
{
emscripten::register_type<Component>("{name: string, [key: string]: any}");
emscripten::register_type<System>("(registry: Registry, ctx: any) => void");
emscripten::register_type<Component>("Component", "{name: string, [key: string]: any}");
emscripten::register_type<System>("System", "(registry: Registry, ctx: Context) => void");
emscripten::register_type<ZipperInput>("Component[]");
emscripten::register_type<ZipperOutput>("any[]");

emscripten::class_<Registry>("Registry")
.constructor()
Expand Down
5 changes: 3 additions & 2 deletions packages/ecs/wasm/Registry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <any>
#include <functional>
#include <iostream>
#include <map>
#include <ranges>
#include <stdexcept>
#include <unordered_map>
Expand Down Expand Up @@ -179,7 +180,7 @@ namespace nfo {
return _next_entity;
}

emscripten::val get_zipper(const emscripten::val &comps)
ZipperOutput get_zipper(const ZipperInput &comps)
{
if (!comps.isArray())
throw std::runtime_error("getZipper: need an array of comps as parameter");
Expand All @@ -206,7 +207,7 @@ namespace nfo {
if (need_to_add)
arr.set(idx, obj);
}
return arr;
return ZipperOutput(arr);
}

private:
Expand Down
Loading
Loading