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
8 changes: 4 additions & 4 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ jobs:

- name: "Setup pnpm"
uses: pnpm/action-setup@v4
with:
run_install: false
# with:
# run_install: false

- name: "Setup node"
uses: actions/setup-node@v2
uses: actions/setup-node@v4
with:
node-version: "23.6.0"
node-version: 23.6.0
cache: "pnpm"

- name: "Install dependencies"
Expand Down
3 changes: 2 additions & 1 deletion example/pong/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@
"@nanoforge/core": "latest",
"@nanoforge/asset-manager": "latest",
"@nanoforge/ecs": "latest",
"@nanoforge/graphics-2d": "latest"
"@nanoforge/graphics-2d": "latest",
"@nanoforge/sound": "latest"
},
"engines": {
"node": "23.6.0",
Expand Down
8 changes: 7 additions & 1 deletion example/pong/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { ECSLibrary } from "@nanoforge/ecs";
import { Graphics2DLibrary } from "@nanoforge/graphics-2d";
import { InputLibrary } from "@nanoforge/input";
import { InputEnum } from "@nanoforge/input";
import { SoundLibrary } from "@nanoforge/sound";

import {
Bounce,
Expand All @@ -22,15 +23,20 @@ export const ecsLibrary = new ECSLibrary();
export const app = NanoforgeFactory.createClient();
export const graphics = new Graphics2DLibrary();
export const inputs = new InputLibrary();
export const sounds = new SoundLibrary();
export const assetManager = new AssetManagerLibrary();

export const main = async (options: IRunOptions) => {
app.useGraphics(graphics);
app.useComponentSystem(ecsLibrary);
app.useAssetManager(new AssetManagerLibrary());
app.useAssetManager(assetManager);
app.useInput(inputs);
app.useSound(sounds);

await app.init(options);

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

const ball = ecsLibrary.spawnEntity();
ecsLibrary.addComponent(ball, new Velocity(0.04, 0));
ecsLibrary.addComponent(ball, new Position(0.5, 0));
Expand Down
6 changes: 5 additions & 1 deletion example/pong/src/systems.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
RectangleComponent,
Velocity,
} from "./components";
import { ecsLibrary, graphics, inputs } from "./index";
import { ecsLibrary, graphics, inputs, sounds } from "./index";

export function move() {
const entities = ecsLibrary.getZipper([Bounce, Position, Velocity]);
Expand All @@ -25,9 +25,13 @@ export function bounce() {
entities.forEach((entity) => {
if (entity.Position.x >= 1.6 || entity.Position.x <= -1.6) {
entity.Velocity.x = -entity.Velocity.x;

sounds.play("test");
}
if (entity.Position.y >= 1 || entity.Position.y <= -1) {
entity.Velocity.y = -entity.Velocity.y;

sounds.play("test");
}
});
}
Expand Down
1 change: 1 addition & 0 deletions packages/common/src/exceptions/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { NfNotFound } from "./notFound";
3 changes: 3 additions & 0 deletions packages/common/src/exceptions/nfException.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface INfException extends Error {
get code(): number;
}
16 changes: 16 additions & 0 deletions packages/common/src/exceptions/notFound.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { type INfException } from "./nfException";

export class NfNotFound implements INfException {
get code(): number {
return 404;
}

message: string;
name: string;
stack?: string | undefined;
cause?: unknown;

constructor(item: string) {
this.message = `${item} not found.`;
}
}
1 change: 1 addition & 0 deletions packages/common/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./context";
export * from "./library";
export * from "./options";
export * from "./exceptions";
1 change: 1 addition & 0 deletions packages/common/src/library/libraries/abstracts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ export { BaseAssetManagerLibrary } from "./asset-manager.library.abstract";
export { BaseComponentSystemLibrary } from "./component-system.library.abstract";
export { BaseGraphicsLibrary } from "./graphics.library.abstract";
export { BaseInputLibrary } from "./input.library.abstract";
export { BaseSoundLibrary } from "./sound.library.abstract";
export { BaseNetworkLibrary } from "./network.library.abstract";
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { type InitContext } from "../../../context";
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 play(sound: string): void;

/**
* mutes or unmutes the sound.
*/
public abstract mute(): void;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export const COMPONENT_SYSTEM_LIBRARY = Symbol("COMPONENT_SYSTEM_LIBRARY");
export const GRAPHICS_LIBRARY = Symbol("GRAPHICS_LIBRARY");
export const NETWORK_LIBRARY = Symbol("NETWORK_LIBRARY");
export const SOUND_LIBRARY = Symbol("SOUND_LIBRARY");
export const ASSET_MANAGER_LIBRARY = Symbol("ASSET_MANAGER_LIBRARY");
export const INPUT_LIBRARY = Symbol("INPUT_LIBRARY");
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { type IExposedLibrary } from "../bases/exposed.library.type";

export interface ISoundLibrary extends IExposedLibrary {
/**
* mutes or unmutes the sound.
*/
mute(): void;
}
1 change: 1 addition & 0 deletions packages/common/src/library/libraries/interfaces/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ export { IAssetManagerLibrary } from "./finals/asset-manager.library.type";
export { IComponentSystemLibrary } from "./finals/component-system.library.type";
export { IGraphicsLibrary } from "./finals/graphics.library.type";
export { IInputLibrary } from "./finals/input.library.type";
export { ISoundLibrary } from "./finals/sound.library.type";
export { INetworkLibrary } from "./finals/network.library.type";
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import {
type IGraphicsLibrary,
type IInputLibrary,
type INetworkLibrary,
type ISoundLibrary,
NETWORK_LIBRARY,
SOUND_LIBRARY,
} from "../../libraries";
import { type LibraryHandle } from "../handle/library.handle";
import { BaseLibraryManager } from "./base-library.manager";
Expand All @@ -18,13 +20,15 @@ export enum DefaultLibrariesEnum {
GRAPHICS,
INPUT,
NETWORK,
SOUND,
}

const DEFAULT_LIBRARIES: { index: DefaultLibrariesEnum; sym: symbol }[] = [
{ index: DefaultLibrariesEnum.ASSET_MANAGER, sym: ASSET_MANAGER_LIBRARY },
{ index: DefaultLibrariesEnum.COMPONENT_SYSTEM, sym: COMPONENT_SYSTEM_LIBRARY },
{ index: DefaultLibrariesEnum.GRAPHICS, sym: GRAPHICS_LIBRARY },
{ index: DefaultLibrariesEnum.NETWORK, sym: NETWORK_LIBRARY },
{ index: DefaultLibrariesEnum.SOUND, sym: SOUND_LIBRARY },
];

export class LibraryManager extends BaseLibraryManager {
Expand Down Expand Up @@ -59,4 +63,8 @@ export class LibraryManager extends BaseLibraryManager {
>(): LibraryHandle<T> {
return this._get<T>(DefaultLibrariesEnum.ASSET_MANAGER);
}

public getSound<T extends ISoundLibrary = ISoundLibrary>(): LibraryHandle<T> {
return this._get<T>(DefaultLibrariesEnum.SOUND);
}
}
9 changes: 9 additions & 0 deletions packages/core/src/application/application-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
type IInputLibrary,
type ILibrary,
type INetworkLibrary,
type ISoundLibrary,
type LibraryHandle,
} from "@nanoforge/common";

Expand Down Expand Up @@ -68,4 +69,12 @@ export class ApplicationConfig {
public useInputLibrary(library: IInputLibrary) {
this._libraryManager.setInput(library);
}

public getSoundLibrary() {
return this._libraryManager.getSound();
}

public useSoundLibrary(library: ISoundLibrary) {
this._libraryManager.setSound(library);
}
}
4 changes: 4 additions & 0 deletions packages/core/src/application/nanoforge-application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,8 @@ export abstract class NanoforgeApplication {
public run() {
this._core.run();
}

public mute() {
this._core.mute();
}
}
6 changes: 5 additions & 1 deletion packages/core/src/application/nanoforge-client.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type IGraphicsLibrary, type IInputLibrary } from "@nanoforge/common";
import { type IGraphicsLibrary, type IInputLibrary, type ISoundLibrary } from "@nanoforge/common";

import { NanoforgeApplication } from "./nanoforge-application";

Expand All @@ -10,4 +10,8 @@ export class NanoforgeClient extends NanoforgeApplication {
public useInput(library: IInputLibrary) {
this.applicationConfig.useInputLibrary(library);
}

public useSound(library: ISoundLibrary) {
this.applicationConfig.useSoundLibrary(library);
}
}
6 changes: 6 additions & 0 deletions packages/core/src/common/library/manager/library.manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ import {
INPUT_LIBRARY,
type INetworkLibrary,
type IRunnerLibrary,
type ISoundLibrary,
type LibraryHandle,
LibraryManager,
NETWORK_LIBRARY,
SOUND_LIBRARY,
} from "@nanoforge/common";

import { EditableLibraryContext } from "../../context/contexts/library.editable-context";
Expand Down Expand Up @@ -59,6 +61,10 @@ export class EditableLibraryManager extends LibraryManager {
this._set(DefaultLibrariesEnum.INPUT, INPUT_LIBRARY, library, new EditableLibraryContext());
}

public setSound(library: ISoundLibrary): void {
this._set(DefaultLibrariesEnum.SOUND, SOUND_LIBRARY, library, new EditableLibraryContext());
}

public getLibraries(): LibraryHandle[] {
return this._libraries;
}
Expand Down
9 changes: 9 additions & 0 deletions packages/core/src/core/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ export class Core {
const intervalHandle = setInterval(render, 1000 / this.options.tickRate);
}

/**
* mutes / unmutes sounds.
*/
public mute(): void {
const soundLibrary = this.config.getSoundLibrary();

soundLibrary.library.mute();
}

private getInitContext(options: IRunOptions): InitContext {
return new InitContext(this.context, this.config.libraryManager, options);
}
Expand Down
Loading
Loading