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
10 changes: 5 additions & 5 deletions example/pong/src/components.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Graphics } from "@nanoforge-dev/graphics-2d";
import { type Circle, type Rect } from "@nanoforge-dev/graphics-2d";
import type { InputEnum } from "@nanoforge-dev/input";

import { layer } from "./index";
Expand Down Expand Up @@ -38,19 +38,19 @@ export class Hitbox {

export class CircleComponent {
name = "CircleComponent";
component: Graphics.Circle;
component: Circle;

constructor(component: Graphics.Circle) {
constructor(component: Circle) {
this.component = component;
layer.add(this.component);
}
}

export class RectangleComponent {
name = "RectangleComponent";
component: Graphics.Rect;
component: Rect;

constructor(component: Graphics.Rect) {
constructor(component: Rect) {
this.component = component;
layer.add(this.component);
}
Expand Down
16 changes: 11 additions & 5 deletions example/pong/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { AssetManagerLibrary } from "@nanoforge-dev/asset-manager/src";
import { type IRunOptions } from "@nanoforge-dev/common";
import { NanoforgeFactory } from "@nanoforge-dev/core";
import { ECSLibrary } from "@nanoforge-dev/ecs";
import { Graphics, Graphics2DLibrary } from "@nanoforge-dev/graphics-2d";
import { Circle, Graphics2DLibrary, Layer, Rect } from "@nanoforge-dev/graphics-2d";
import { InputEnum } from "@nanoforge-dev/input";
import { InputLibrary } from "@nanoforge-dev/input/src";
import { SoundLibrary } from "@nanoforge-dev/sound";

import {
Expand All @@ -18,15 +20,19 @@ import { bounce, controlPlayer, drawCircle, move, moveRectangle } from "./system

export const app = NanoforgeFactory.createClient();

export const layer = new Graphics.Layer();
export const layer = new Layer();

export const main = async (options: IRunOptions) => {
const assetManager = new AssetManagerLibrary();
const graphics = new Graphics2DLibrary();
const ecsLibrary = new ECSLibrary();
const inputLibrary = new InputLibrary();
const sounds = new SoundLibrary();

app.useAssetManager(assetManager);
app.useGraphics(graphics);
app.useComponentSystem(ecsLibrary);
app.useInput(inputLibrary);
app.useSound(sounds);

await app.init(options);
Expand All @@ -47,7 +53,7 @@ export const main = async (options: IRunOptions) => {
registry.addComponent(
ball,
new CircleComponent(
new Graphics.Circle({
new Circle({
radius: 70,
fill: "red",
}),
Expand All @@ -61,7 +67,7 @@ export const main = async (options: IRunOptions) => {
registry.addComponent(player1, new Controller(InputEnum.KeyW, InputEnum.KeyS));
registry.addComponent(
player1,
new RectangleComponent(new Graphics.Rect({ fill: "blue", width: 50, height: 500 })),
new RectangleComponent(new Rect({ fill: "blue", width: 50, height: 500 })),
);

const player2 = registry.spawnEntity();
Expand All @@ -71,7 +77,7 @@ export const main = async (options: IRunOptions) => {
registry.addComponent(player2, new Controller(InputEnum.ArrowUp, InputEnum.ArrowDown));
registry.addComponent(
player2,
new RectangleComponent(new Graphics.Rect({ fill: "blue", width: 50, height: 500 })),
new RectangleComponent(new Rect({ fill: "blue", width: 50, height: 500 })),
);

registry.addSystem(move);
Expand Down
3 changes: 1 addition & 2 deletions packages/ecs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
"Tchips <[email protected]>"
],
"files": [
"dist",
"lib"
"dist"
],
"main": "./dist/index.cjs",
"module": "./dist/index.js",
Expand Down
57 changes: 1 addition & 56 deletions packages/ecs/tsup.config.ts
Original file line number Diff line number Diff line change
@@ -1,58 +1,3 @@
import fs from "node:fs";
import path from "node:path";

import { createTsupConfig } from "../../tsup.config.js";

const wasmPlugin = {
name: "wasm",
setup(build) {
// Resolve ".wasm" files to a path with a namespace
build.onResolve({ filter: /\.wasm$/ }, (args) => {
// If this is the import inside the stub module, import the
// binary itself. Put the path in the "wasm-binary" namespace
// to tell our binary load callback to load the binary file.
if (args.namespace === "wasm-stub") {
return {
path: args.path,
namespace: "wasm-binary",
};
}

// Otherwise, generate the JavaScript stub module for this
// ".wasm" file. Put it in the "wasm-stub" namespace to tell
// our stub load callback to fill it with JavaScript.
//
// Resolve relative paths to absolute paths here since this
// resolve callback is given "resolveDir", the directory to
// resolve imports against.
if (args.resolveDir === "") {
return; // Ignore unresolvable paths
}
return {
path: path.isAbsolute(args.path) ? args.path : path.join(args.resolveDir, args.path),
namespace: "wasm-stub",
};
});

// Virtual modules in the "wasm-stub" namespace are filled with
// the JavaScript code for compiling the WebAssembly binary. The
// binary itself is imported from a second virtual module.
build.onLoad({ filter: /.*/, namespace: "wasm-stub" }, async (args) => ({
contents: `import wasm from ${JSON.stringify(args.path)}
export default (imports) =>
WebAssembly.instantiate(wasm, imports).then(
result => result.instance.exports)`,
}));

// Virtual modules in the "wasm-binary" namespace contain the
// actual bytes of the WebAssembly file. This uses esbuild's
// built-in "binary" loader instead of manually embedding the
// binary data inside JavaScript code ourselves.
build.onLoad({ filter: /.*/, namespace: "wasm-binary" }, async (args) => ({
contents: await fs.promises.readFile(args.path),
loader: "binary",
}));
},
};

export default [createTsupConfig({ esbuildPlugins: [wasmPlugin] })];
export default [createTsupConfig()];
2 changes: 1 addition & 1 deletion packages/graphics-2d/src/graphics-2d.library.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BaseGraphicsLibrary, type InitContext } from "@nanoforge-dev/common";

import { Graphics } from ".";
import * as Graphics from "./exports/konva";

export class Graphics2DLibrary extends BaseGraphicsLibrary {
private _stage?: Graphics.Stage;
Expand Down
2 changes: 1 addition & 1 deletion packages/graphics-2d/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { Graphics2DLibrary } from "./graphics-2d.library";
export * as Graphics from "./exports/konva";
export * from "./exports/konva";
6 changes: 6 additions & 0 deletions tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,13 @@ export function createTsupConfig({
keepNames = true,
dts = true,
sourcemap = true,
esbuildOptions = (options) => {
options.assetNames = "assets/[name]";
},
esbuildPlugins = [],
treeshake = false,
outDir = "dist",
loader = { ".wasm": "copy" },
}: Options = {}) {
return defineConfig({
entry,
Expand All @@ -42,8 +46,10 @@ export function createTsupConfig({
keepNames,
dts,
sourcemap,
esbuildOptions,
esbuildPlugins,
treeshake,
outDir,
loader,
});
}
Loading