Skip to content

Commit c1d73f6

Browse files
committed
feat(music): finish music library and add an interface for mutable libraries
1 parent d9f72df commit c1d73f6

File tree

15 files changed

+148
-19
lines changed

15 files changed

+148
-19
lines changed

packages/common/src/library/libraries/abstracts/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ export { BaseGraphicsLibrary } from "./graphics.library.abstract";
44
export { BaseInputLibrary } from "./input.library.abstract";
55
export { BaseSoundLibrary } from "./sound.library.abstract";
66
export { BaseNetworkLibrary } from "./network.library.abstract";
7+
export { BaseMusicLibrary } from "./music.library.abstract";
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { type InitContext } from "../../../context";
2+
import { type IMusicLibrary } from "../interfaces";
3+
import { Library } from "../library";
4+
5+
export abstract class BaseMusicLibrary extends Library implements IMusicLibrary {
6+
public abstract init(context: InitContext): Promise<void>;
7+
8+
public abstract play(sound: string): void;
9+
10+
/**
11+
* mutes or unmutes the sound.
12+
*/
13+
public abstract mute(): void;
14+
}

packages/common/src/library/libraries/consts/library-label.const.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ export const COMPONENT_SYSTEM_LIBRARY = Symbol("COMPONENT_SYSTEM_LIBRARY");
22
export const GRAPHICS_LIBRARY = Symbol("GRAPHICS_LIBRARY");
33
export const NETWORK_LIBRARY = Symbol("NETWORK_LIBRARY");
44
export const SOUND_LIBRARY = Symbol("SOUND_LIBRARY");
5+
export const MUSIC_LIBRARY = Symbol("MUSIC_LIBRARY");
56
export const ASSET_MANAGER_LIBRARY = Symbol("ASSET_MANAGER_LIBRARY");
67
export const INPUT_LIBRARY = Symbol("INPUT_LIBRARY");
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { type ILibrary } from "../../library.type";
2+
3+
export interface IMutableLibrary extends ILibrary {
4+
/**
5+
* mutes or unmutes the sound.
6+
*/
7+
mute(): void;
8+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { type IExposedLibrary } from "../bases/exposed.library.type";
2+
import { type IMutableLibrary } from "../bases/mutable.library.type";
3+
4+
export interface IMusicLibrary extends IExposedLibrary, IMutableLibrary {}
Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
import { type IExposedLibrary } from "../bases/exposed.library.type";
2+
import { type IMutableLibrary } from "../bases/mutable.library.type";
23

3-
export interface ISoundLibrary extends IExposedLibrary {
4-
/**
5-
* mutes or unmutes the sound.
6-
*/
7-
mute(): void;
8-
}
4+
export interface ISoundLibrary extends IExposedLibrary, IMutableLibrary {}

packages/common/src/library/libraries/interfaces/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@ export { IGraphicsLibrary } from "./finals/graphics.library.type";
66
export { IInputLibrary } from "./finals/input.library.type";
77
export { ISoundLibrary } from "./finals/sound.library.type";
88
export { INetworkLibrary } from "./finals/network.library.type";
9+
export { IMusicLibrary } from "./finals/music.library.type";
10+
export { IMutableLibrary } from "./bases/mutable.library.type";

packages/common/src/library/manager/managers/library.manager.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ import {
66
type IComponentSystemLibrary,
77
type IGraphicsLibrary,
88
type IInputLibrary,
9+
type IMusicLibrary,
910
type INetworkLibrary,
1011
type ISoundLibrary,
12+
MUSIC_LIBRARY,
1113
NETWORK_LIBRARY,
1214
SOUND_LIBRARY,
1315
} from "../../libraries";
@@ -21,6 +23,7 @@ export enum DefaultLibrariesEnum {
2123
INPUT,
2224
NETWORK,
2325
SOUND,
26+
MUSIC,
2427
}
2528

2629
const DEFAULT_LIBRARIES: { index: DefaultLibrariesEnum; sym: symbol }[] = [
@@ -29,6 +32,7 @@ const DEFAULT_LIBRARIES: { index: DefaultLibrariesEnum; sym: symbol }[] = [
2932
{ index: DefaultLibrariesEnum.GRAPHICS, sym: GRAPHICS_LIBRARY },
3033
{ index: DefaultLibrariesEnum.NETWORK, sym: NETWORK_LIBRARY },
3134
{ index: DefaultLibrariesEnum.SOUND, sym: SOUND_LIBRARY },
35+
{ index: DefaultLibrariesEnum.MUSIC, sym: MUSIC_LIBRARY },
3236
];
3337

3438
export class LibraryManager extends BaseLibraryManager {
@@ -67,4 +71,8 @@ export class LibraryManager extends BaseLibraryManager {
6771
public getSound<T extends ISoundLibrary = ISoundLibrary>(): LibraryHandle<T> {
6872
return this._get<T>(DefaultLibrariesEnum.SOUND);
6973
}
74+
75+
public getMusic<T extends IMusicLibrary = IMusicLibrary>(): LibraryHandle<T> {
76+
return this._get<T>(DefaultLibrariesEnum.MUSIC);
77+
}
7078
}

packages/core/src/application/application-config.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
type IGraphicsLibrary,
55
type IInputLibrary,
66
type ILibrary,
7+
type IMusicLibrary,
78
type INetworkLibrary,
89
type ISoundLibrary,
910
type LibraryHandle,
@@ -77,4 +78,16 @@ export class ApplicationConfig {
7778
public useSoundLibrary(library: ISoundLibrary) {
7879
this._libraryManager.setSound(library);
7980
}
81+
82+
public getMusicLibrary() {
83+
return this._libraryManager.getMusic();
84+
}
85+
86+
public useMusicLibrary(library: IMusicLibrary) {
87+
this._libraryManager.setMusic(library);
88+
}
89+
90+
public getMutableLibraries() {
91+
return this._libraryManager.getMutableLibraries();
92+
}
8093
}

packages/core/src/common/library/manager/library.manager.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,15 @@ import {
88
type IGraphicsLibrary,
99
type IInputLibrary,
1010
type ILibrary,
11+
type IMusicLibrary,
12+
type IMutableLibrary,
1113
INPUT_LIBRARY,
1214
type INetworkLibrary,
1315
type IRunnerLibrary,
1416
type ISoundLibrary,
1517
type LibraryHandle,
1618
LibraryManager,
19+
MUSIC_LIBRARY,
1720
NETWORK_LIBRARY,
1821
SOUND_LIBRARY,
1922
} from "@nanoforge/common";
@@ -65,6 +68,10 @@ export class EditableLibraryManager extends LibraryManager {
6568
this._set(DefaultLibrariesEnum.SOUND, SOUND_LIBRARY, library, new EditableLibraryContext());
6669
}
6770

71+
public setMusic(library: IMusicLibrary): void {
72+
this._set(DefaultLibrariesEnum.MUSIC, MUSIC_LIBRARY, library, new EditableLibraryContext());
73+
}
74+
6875
public getLibraries(): LibraryHandle[] {
6976
return this._libraries;
7077
}
@@ -81,6 +88,12 @@ export class EditableLibraryManager extends LibraryManager {
8188
return Relationship.getLibrariesByDependencies(this._libraries, true);
8289
}
8390

91+
public getMutableLibraries(): LibraryHandle<IMutableLibrary>[] {
92+
return this._libraries.filter(
93+
(handle) => handle && typeof handle.library["mute"] === "function",
94+
) as LibraryHandle<IMutableLibrary>[];
95+
}
96+
8497
private _getRunnerLibraries(): LibraryHandle<IRunnerLibrary>[] {
8598
return this._libraries.filter(
8699
(handle) => handle && typeof handle.library["run"] === "function",

0 commit comments

Comments
 (0)