Skip to content

Commit 37dc713

Browse files
committed
Play main menu music
1 parent e93e7fc commit 37dc713

File tree

5 files changed

+47
-10
lines changed

5 files changed

+47
-10
lines changed

src/game/gad-renderer.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,12 @@ export default class GADRenderer {
106106
}
107107
}
108108

109-
public renderVideo(videoSprite: PIXI.Sprite) {
109+
public renderVideo(videoSprite: PIXI.Sprite, onEnd: () => void) {
110110
// @ts-ignore
111111
const videoTexture: VideoBaseTexture = videoSprite.texture.baseTexture;
112112
videoTexture.source.addEventListener("ended", () => {
113113
this.stage.removeChild(videoSprite);
114+
onEnd();
114115
}, {once: true});
115116
this.stage.addChild(videoSprite);
116117
this.videos.push(videoSprite);

src/game/menu-structure.ts

+22-3
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,42 @@ import * as PIXI from "pixi.js";
22
import FileSystem from "../filesystem";
33
import {textureFromUint8ArrayMP4} from "../util/pixi";
44
import GADRenderer from "./gad-renderer";
5+
import MusicPlayer from "./music-player";
56

67
type Callback = () => void;
78

89
interface ScreenConfig {
10+
onLoad: Callback;
911
callbacks: Callback[];
1012
}
1113

1214
export default class MenuStructure extends PIXI.utils.EventEmitter {
1315
private readonly structure: {[k: string]: ScreenConfig} = {
1416
menu_main: {
17+
onLoad: () => this.playMainMenuMusic(),
1518
callbacks: [
1619
async () => this.renderScreen("menu_missions"),
1720
async () => console.log("Multiplayer"),
1821
async () => {
1922
// Credits
23+
this.musicPlayer.stop();
2024
const videoSprite = await this.loadVideoSprite(10);
2125
videoSprite.width = 453 - 14;
2226
videoSprite.height = 367 - 14;
2327
videoSprite.position.set(500 + 7, 359 + 7);
24-
this.gadRenderer.renderVideo(videoSprite);
28+
this.gadRenderer.renderVideo(videoSprite, () => this.playMainMenuMusic);
2529
},
2630
async () => {
2731
// Intro
2832
const videoSprite = await this.loadVideoSprite(58);
33+
this.musicPlayer.stop();
2934
this.gadRenderer.renderVideoFullscreen(videoSprite, () => this.renderScreen("menu_main"));
3035
},
3136
async () => console.log("Exit"),
3237
],
3338
},
3439
menu_missions: {
40+
onLoad: () => this.playMainMenuMusic(),
3541
callbacks: [
3642
async () => console.log("New Game"),
3743
async () => console.log("Load Game"),
@@ -45,9 +51,15 @@ export default class MenuStructure extends PIXI.utils.EventEmitter {
4551
],
4652
},
4753
menu_loading: {
54+
onLoad: () => {
55+
// Nothing to do
56+
},
4857
callbacks: [],
4958
},
5059
menu_mission_details: {
60+
onLoad: () => {
61+
// Nothing to do
62+
},
5163
callbacks: [
5264
async () => console.log("Start Mission"),
5365
async () => this.renderScreen("menu_missions"),
@@ -65,14 +77,21 @@ export default class MenuStructure extends PIXI.utils.EventEmitter {
6577
},
6678
};
6779

68-
constructor(private readonly fs: FileSystem, private readonly gadRenderer: GADRenderer) {
80+
constructor(private readonly fs: FileSystem, private readonly gadRenderer: GADRenderer,
81+
private readonly musicPlayer: MusicPlayer) {
6982
super();
7083
}
7184

7285
public async renderScreen(screen: string) {
7386
const data = JSON.parse(await this.fs.openAndGetContentAsText(`/screens/${screen}.json`));
7487
console.log(data);
75-
await this.gadRenderer.render(data, this.structure[screen].callbacks);
88+
const config = this.structure[screen];
89+
config.onLoad();
90+
await this.gadRenderer.render(data, config.callbacks);
91+
}
92+
93+
private playMainMenuMusic() {
94+
this.musicPlayer.play("1st Beginning", true);
7695
}
7796

7897
private async loadVideoSprite(videoNumber: number) {

src/game/music-player.ts

+21-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import FileSystem from "../filesystem";
44

55
export default class MusicPlayer {
66
private playing = false;
7-
private readonly songs: PIXI.sound.Sound[] = [];
7+
private readonly songs: Array<{name: string, sound: PIXI.sound.Sound}> = [];
88
private currentSongIdx: number;
99

1010
constructor(private readonly fs: FileSystem) { }
@@ -14,12 +14,23 @@ export default class MusicPlayer {
1414
for (const file of files) {
1515
console.log(`Loading music ${file.name}`);
1616
const data = (await this.fs.openAndGetContentAsUint8Array(file)).buffer as ArrayBuffer;
17-
this.songs.push(await this.loadSound(data));
17+
this.songs.push({name: file.name, sound: await this.loadSound(data)});
1818
console.log(`Finished loading music ${file.name}`);
1919
}
2020
}
2121

22-
public play() {
22+
public play(name: string, loop: boolean = false) {
23+
const song = this.songs.find((each) => each.name === name + ".wav");
24+
if (song === undefined) {
25+
console.warn(`Song ${name} not found.`);
26+
return;
27+
}
28+
if (!song.sound.isPlaying) {
29+
song.sound.play({loop: loop});
30+
}
31+
}
32+
33+
public playAll() {
2334
if (this.songs.length === 0) {
2435
console.warn("No music to play found :(");
2536
return;
@@ -28,17 +39,23 @@ export default class MusicPlayer {
2839
console.warn("Music is already playing!");
2940
return;
3041
}
42+
this.stop();
43+
3144
this.playing = true;
3245
this.currentSongIdx = -1;
3346
this.playNext();
3447
}
3548

49+
public stop() {
50+
this.songs.forEach((song) => song.sound.stop());
51+
}
52+
3653
private playNext() {
3754
this.currentSongIdx++;
3855
if (this.currentSongIdx === this.songs.length) {
3956
this.currentSongIdx = 0;
4057
}
41-
this.songs[this.currentSongIdx].play(this.playNext.bind(this));
58+
this.songs[this.currentSongIdx].sound.play(this.playNext.bind(this));
4259
}
4360

4461
private loadSound(data: ArrayBuffer, preload: boolean = false) {

src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ const Viewport = require("pixi-viewport");
9494
} else {
9595
// menuViewport.fit(); // TODO: Makes usage of sliders harder
9696
const gadRenderer = new GADRenderer(menuViewport, spriteLoader);
97-
const menuStructure = new MenuStructure(fs, gadRenderer);
97+
const menuStructure = new MenuStructure(fs, gadRenderer, musicPlayer);
9898
menuStructure.on("load-game", async (gameName: string) => {
9999
await menu.loadByName(gameName);
100100
menuViewport.visible = false;

src/menu.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export default class Menu {
3939
const gameRenderer = new GameRenderer(world, this.islandRenderer, this.viewport);
4040
const gameLogic = new Game(gameRenderer, this.configLoader);
4141
await gameLogic.begin(world);
42-
this.musicPlayer.play();
42+
this.musicPlayer.playAll();
4343

4444
// this.viewport.parent.addChild(new PIXI.Text(
4545
// `Money: ${world.players.find((player) => player.id === 0).money}`,

0 commit comments

Comments
 (0)