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
1 change: 0 additions & 1 deletion example/pong/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ export const main = async (options: IRunOptions) => {
const registry = ecsLibrary.registry;

graphics.stage.add(layer);
console.log(graphics.stage.width());

sounds.load("test", "https://universal-soundbank.com/sounds/18782.mp3");

Expand Down
16 changes: 8 additions & 8 deletions example/pong/src/systems.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { type ECSContext, type ECSRegistry } from "@nanoforge/ecs";
import { type Context } from "@nanoforge/common";
import { type ECSRegistry } from "@nanoforge/ecs";
import { type InputLibrary } from "@nanoforge/input";
import { type SoundLibrary } from "@nanoforge/sound";

Expand All @@ -22,37 +23,36 @@ export function move(registry: ECSRegistry) {
});
}

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

entities.forEach((entity) => {
if (entity.Position.x >= 1800 || entity.Position.x <= 100) {
entity.Velocity.x = -entity.Velocity.x;

ctx.libraries.getSound<SoundLibrary>().library.play("test");
ctx.libs.getSound<SoundLibrary>().play("test");
}
if (entity.Position.y >= 1000 || entity.Position.y <= 100) {
entity.Velocity.y = -entity.Velocity.y;

ctx.libraries.getSound<SoundLibrary>().library.play("test");
ctx.libs.getSound<SoundLibrary>().play("test");
}
});
}

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

entities.forEach((entity) => {
if (
ctx.libraries.getInput<InputLibrary>().library.isKeyPressed(entity.Controller.up) &&
ctx.libs.getInput<InputLibrary>().isKeyPressed(entity.Controller.up) &&
!checkCollisions(registry, entity)
) {
entity.Position.y -= entity.Velocity.y;
} else {
entity.Position.y += entity.Velocity.y;
}
if (
ctx.libraries.getInput<InputLibrary>().library.isKeyPressed(entity.Controller.down) &&
ctx.libs.getInput<InputLibrary>().isKeyPressed(entity.Controller.down) &&
!checkCollisions(registry, entity)
) {
entity.Position.y += entity.Velocity.y;
Expand Down
4 changes: 2 additions & 2 deletions packages/asset-manager/src/asset-manager.library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ export class AssetManagerLibrary extends BaseAssetManagerLibrary {
private _wasm: Map<string, string>;
private _wgsl: Map<string, string>;

get name(): string {
get __name(): string {
return "AssetManagerLibrary";
}

public async init(context: InitContext): Promise<void> {
public async __init(context: InitContext): Promise<void> {
this._assets = context.files.assets;
this._wasm = context.files.wasm;
this._wgsl = context.files.wgsl;
Expand Down
2 changes: 1 addition & 1 deletion packages/asset-manager/test/asset-manager.library.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe("Asset Manager Library", () => {
wgsl: new Map([["/test.wgsl", "blob:http://localhost:3000/test.wgsl"]]),
},
});
library.init(context);
library.__init(context);

it("Should get asset", async () => {
expect((await library.getAsset("test.png")).path).toEqual(
Expand Down
12 changes: 11 additions & 1 deletion packages/common/src/context/contexts/application.context.ts
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
export class ApplicationContext {}
export class ApplicationContext {
private _isRunning: boolean = false;

get isRunning(): boolean {
return this._isRunning;
}

setIsRunning(value: boolean): void {
this._isRunning = value;
}
}
20 changes: 20 additions & 0 deletions packages/common/src/context/contexts/client.context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { type ClientLibraryManager } from "../../library";
import type { ApplicationContext } from "./application.context";

export class Context {
private readonly _applicationContext: ApplicationContext;
private readonly _libraryManager: ClientLibraryManager;

constructor(applicationContext: ApplicationContext, libraryManager: ClientLibraryManager) {
this._applicationContext = applicationContext;
this._libraryManager = libraryManager;
}

get app(): ApplicationContext {
return this._applicationContext;
}

get libs(): ClientLibraryManager {
return this._libraryManager;
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,3 @@
import { type IRunnerLibrary } from "../../../library";
import { BaseContext } from "./base.context";

export class ExecutionContext<T extends IRunnerLibrary> extends BaseContext {
protected _isRunning: boolean = true;
protected _currentLibrary: T;

get isRunning() {
return this._isRunning;
}

get lib(): T {
return this._currentLibrary;
}
}
export class ExecutionContext extends BaseContext {}
1 change: 1 addition & 0 deletions packages/common/src/context/contexts/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./executions";
export { ApplicationContext } from "./application.context";
export { Context } from "./client.context";
export { LibraryContext, LibraryStatusEnum } from "./library.context";
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ import { type IAssetManagerLibrary } from "../interfaces";
import { Library } from "../library";

export abstract class BaseAssetManagerLibrary extends Library implements IAssetManagerLibrary {
public abstract init(context: InitContext): Promise<void>;
public abstract __init(context: InitContext): Promise<void>;
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { type ExecutionContext } from "../../../context";
import { type Context } from "../../../context";
import { type IComponentSystemLibrary } from "../interfaces";
import { Library } from "../library";

export abstract class BaseComponentSystemLibrary
extends Library
implements IComponentSystemLibrary
{
abstract run(context: ExecutionContext<this>): Promise<void>;
abstract __run(context: Context): Promise<void>;
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { ExecutionContext } from "../../../context";
import { type Context } from "../../../context";
import { type IGraphicsLibrary } from "../interfaces";
import { Library } from "../library";

export abstract class BaseGraphicsLibrary extends Library implements IGraphicsLibrary {
abstract run(context: ExecutionContext<this>): Promise<void>;
abstract __run(context: Context): Promise<void>;
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import { type InitContext } from "../../../context";
import { type IInputLibrary } from "../interfaces";
import { Library } from "../library";

export abstract class BaseInputLibrary extends Library implements IInputLibrary {
public abstract init(context: InitContext): Promise<void>;
}
export abstract class BaseInputLibrary extends Library implements IInputLibrary {}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { type IMusicLibrary } from "../interfaces";
import { Library } from "../library";

export abstract class BaseMusicLibrary extends Library implements IMusicLibrary {
public abstract init(context: InitContext): Promise<void>;
public abstract __init(context: InitContext): Promise<void>;

public abstract play(sound: string): void;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { type ISoundLibrary } from "../interfaces";
import { Library } from "../library";

export abstract class BaseSoundLibrary extends Library implements ISoundLibrary {
public abstract init(context: InitContext): Promise<void>;
public abstract __init(context: InitContext): Promise<void>;

public abstract play(sound: string): void;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { type ExecutionContext } from "../../../../context";
import { type Context } from "../../../../context";
import { type ILibrary } from "../../library.type";

export interface IRunnerLibrary extends ILibrary {
run(context: ExecutionContext<IRunnerLibrary>): Promise<void>;
__run(context: Context): Promise<void>;
}
8 changes: 4 additions & 4 deletions packages/common/src/library/libraries/library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ export abstract class Library implements ILibrary {
);
}

get relationship(): RelationshipHandler {
get __relationship(): RelationshipHandler {
return this._relationship;
}

abstract get name(): string;
abstract get __name(): string;

// eslint-disable-next-line @typescript-eslint/no-unused-vars
public async init(_context: InitContext): Promise<void> {}
public async __init(_context: InitContext): Promise<void> {}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
public async clear(_context: ClearContext): Promise<void> {}
public async __clear(_context: ClearContext): Promise<void> {}
}
10 changes: 5 additions & 5 deletions packages/common/src/library/libraries/library.type.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { type ApplicationContext, type ClearContext } from "../../context";
import { type ClearContext, type InitContext } from "../../context";
import { type RelationshipHandler } from "../relationship/relationship-handler";

export interface ILibrary {
get name(): string;
get __name(): string;

get relationship(): RelationshipHandler;
get __relationship(): RelationshipHandler;

init(context: ApplicationContext): Promise<void>;
__init(context: InitContext): Promise<void>;

clear(context: ClearContext): Promise<void>;
__clear(context: ClearContext): Promise<void>;
}

export interface ILibraryOptions {
Expand Down
1 change: 1 addition & 0 deletions packages/common/src/library/manager/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { LibraryHandle } from "./handle/library.handle";
export { ClientLibraryManager } from "./managers/client-library.manager";
export { DefaultLibrariesEnum, LibraryManager } from "./managers/library.manager";
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import {
type IAssetManagerLibrary,
type IComponentSystemLibrary,
type IGraphicsLibrary,
type IInputLibrary,
type ILibrary,
type IMusicLibrary,
type INetworkLibrary,
type ISoundLibrary,
} from "../../libraries";
import { type LibraryManager } from "./library.manager";

export class ClientLibraryManager {
private readonly _libraryManager: LibraryManager;

constructor(libraryManager: LibraryManager) {
this._libraryManager = libraryManager;
}

public get<T extends ILibrary = ILibrary>(sym: symbol): T {
return this._libraryManager.get<T>(sym).library;
}

public getComponentSystem<T extends IComponentSystemLibrary = IComponentSystemLibrary>(): T {
return this._libraryManager.getComponentSystem<T>().library;
}

public getGraphics<T extends IGraphicsLibrary = IGraphicsLibrary>(): T {
return this._libraryManager.getGraphics<T>().library;
}

public getNetwork<T extends INetworkLibrary = INetworkLibrary>(): T {
return this._libraryManager.getNetwork<T>().library;
}

public getInput<T extends IInputLibrary = IInputLibrary>(): T {
return this._libraryManager.getInput<T>().library;
}

public getAssetManager<T extends IAssetManagerLibrary = IAssetManagerLibrary>(): T {
return this._libraryManager.getAssetManager<T>().library;
}

public getSound<T extends ISoundLibrary = ISoundLibrary>(): T {
return this._libraryManager.getSound<T>().library;
}

public getMusic<T extends IMusicLibrary = IMusicLibrary>(): T {
return this._libraryManager.getMusic<T>().library;
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
import { ExecutionContext, type IRunnerLibrary } from "@nanoforge/common";
import { ExecutionContext } from "@nanoforge/common";

export class EditableExecutionContext<T extends IRunnerLibrary> extends ExecutionContext<T> {
setIsRunning(isRunning: boolean) {
this._isRunning = isRunning;
}

setCurrentLibrary(library: T) {
this._currentLibrary = library;
}
}
export class EditableExecutionContext extends ExecutionContext {}
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export class EditableLibraryManager extends LibraryManager {

private _getRunnerLibraries(): LibraryHandle<IRunnerLibrary>[] {
return this._libraries.filter(
(handle) => handle && typeof handle.library["run"] === "function",
(handle) => handle && typeof handle.library["__run"] === "function",
) as LibraryHandle<IRunnerLibrary>[];
}
}
6 changes: 3 additions & 3 deletions packages/core/src/common/library/relationship-functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ class RelationshipStatic {
for (const handle of libraries) {
const key = handle.symbol;

for (const before of handle.library.relationship.runBefore) {
for (const before of handle.library.__relationship.runBefore) {
this._pushToDependencies(key, before, dependencies);
}
for (const after of handle.library.relationship.runAfter) {
for (const after of handle.library.__relationship.runAfter) {
this._pushToDependencies(after, key, dependencies);
}
}
Expand Down Expand Up @@ -100,7 +100,7 @@ class RelationshipStatic {
if (cache.includes(handle.symbol)) throw new Error("Circular dependencies !");

cache.push(handle.symbol);
for (const dep of handle.library.relationship.dependencies) {
for (const dep of handle.library.__relationship.dependencies) {
if (this._symbolIsInList(dep, response)) continue;

const depHandle = libraries.find((lib) => lib?.symbol === dep) as LibraryHandle;
Expand Down
35 changes: 0 additions & 35 deletions packages/core/src/config/index.ts

This file was deleted.

Loading
Loading