Skip to content

Commit bae592d

Browse files
committed
Merge remote-tracking branch 'refs/remotes/origin/main'
2 parents b0ed5de + 713f6fa commit bae592d

File tree

12 files changed

+162
-15
lines changed

12 files changed

+162
-15
lines changed

assets/audio/battle-theme.mp3

1.89 MB
Binary file not shown.

assets/audio/battle-theme.opus

777 KB
Binary file not shown.

assets/audio/main-theme.mp3

-110 KB
Binary file not shown.

assets/audio/main-theme.opus

-94.3 KB
Binary file not shown.

src/constants/audio.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
export const AudioKey = {
2+
MAIN_THEME: 'main-theme',
3+
BATTLE_THEME: 'battle-theme',
4+
};
5+
6+
export const AudioMarkerKey = {
7+
BATTLE_THEME_LOOP: 'battle-theme-loop',
8+
};
9+
10+
export const LOOP_MARKER_CONFIGS = {
11+
[AudioKey.BATTLE_THEME]: {
12+
name: AudioMarkerKey.BATTLE_THEME_LOOP,
13+
start: 6.3, // In Seconds
14+
duration: 101.7,
15+
config: {
16+
loop: true,
17+
seek: 6.3,
18+
}
19+
}
20+
};

src/constants/injector.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export const DependencyKey = {
2+
AUDIO_SYSTEM: 'AudioSystem',
3+
};

src/scenes/Battle.js

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,12 @@ import {
3434
JOYSTICK_RADIUS,
3535
JOYSTICK_THUMB_RADIUS,
3636
} from '../constants/touch.js';
37+
import { DependencyKey } from '../constants/injector.js';
38+
import { AudioKey, AudioMarkerKey, LOOP_MARKER_CONFIGS } from '../constants/audio.js';
3739

3840
export class Battle extends Scene {
41+
injector;
42+
3943
_player;
4044
_enemyPool;
4145
_laserPool;
@@ -49,6 +53,7 @@ export class Battle extends Scene {
4953
_scoreSystem;
5054
_statusSystem;
5155
_touchControlsSystem;
56+
_audioSystem;
5257

5358
_sessionStopWatch;
5459
_playerGracePeriodTween;
@@ -57,10 +62,15 @@ export class Battle extends Scene {
5762
super(SceneKey.BATTLE);
5863
}
5964

65+
setupDependencies() {
66+
this._audioSystem = this.injector.get(DependencyKey.AUDIO_SYSTEM);
67+
}
68+
6069
create() {
61-
const mainThemeSong = this.sound.get('main-theme');
62-
if (mainThemeSong.isPlaying) {
63-
mainThemeSong.stop();
70+
if(
71+
!this._audioSystem.get(AudioKey.BATTLE_THEME)?.isPlaying
72+
) {
73+
this._audioSystem.playLoop(AudioKey.BATTLE_THEME, { loop: false }, LOOP_MARKER_CONFIGS[AudioKey.BATTLE_THEME]);
6474
}
6575

6676
this.subscribeToEvents();
@@ -265,6 +275,7 @@ export class Battle extends Scene {
265275
}
266276

267277
onPauseRequested() {
278+
268279
this.scene.launch(SceneKey.PAUSE_MENU);
269280
}
270281

@@ -371,21 +382,24 @@ export class Battle extends Scene {
371382
}
372383

373384
onHudDestroyed() {
374-
this.scene.start(SceneKey.MAIN_MENU);
385+
this.scene.start(SceneKey.MAIN_MENU, { playMusic: true });
375386
}
376387

377388
onQuitGame() {
389+
this._audioSystem.stop(AudioKey.BATTLE_THEME);
378390
this.scene.stop(SceneKey.HUD);
379391
}
380392

381393
onResumeGame() {
394+
this._audioSystem.resume(AudioKey.BATTLE_THEME);
382395
this.scene.resume(SceneKey.HUD);
383396
this.scene.resume(this);
384397
}
385398

386399
onPauseGame() {
387400
this.scene.pause(SceneKey.HUD);
388401
this.scene.pause(this);
402+
this._audioSystem.pause(AudioKey.BATTLE_THEME);
389403
}
390404

391405
onDataChanged(_parent, key, value) {
@@ -466,6 +480,7 @@ export class Battle extends Scene {
466480
this.clearPhysicsPools();
467481
this.updateHighScore();
468482
this._spawnSystem.deactivateEnemySpawnTimer();
483+
this._audioSystem.stop(AudioKey.BATTLE_THEME);
469484

470485
this.time.delayedCall(1000, () => {
471486
this.scene.launch(SceneKey.GAME_OVER, gameStats);
@@ -494,6 +509,7 @@ export class Battle extends Scene {
494509
this._spawnSystem.reset();
495510
crossSceneEventEmitter.emit(CrossSceneEvent.SCORE_RESET);
496511
this._sessionStopWatch.reset();
512+
this._audioSystem.playLoop(AudioKey.BATTLE_THEME, { loop: false }, AudioMarkerKey.BATTLE_THEME_LOOP);
497513
}
498514

499515
clearPhysicsPools() {

src/scenes/Credits.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ export class Credits extends Scene {
8787
backButton.on('pointerover', onButtonHover);
8888
backButton.on('pointerout', onButtonOut);
8989
backButton.on('pointerup', () => {
90-
this.scene.start(SceneKey.MAIN_MENU);
90+
this.scene.start(SceneKey.MAIN_MENU, { playMusic: false });
9191
});
9292
}
9393
}

src/scenes/MainMenu.js

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,29 @@ import {
88
import { SceneKey } from '../constants/scene.js';
99
import { onButtonHover, onButtonOut } from '../utils/ui.js';
1010
import { RegistryKey } from '../constants/data.js';
11+
import { DependencyKey } from '../constants/injector.js';
12+
import { AudioKey } from '../constants/audio.js';
1113

1214
export class MainMenu extends Scene {
15+
injector;
16+
_audioSystem;
17+
1318
_music;
1419

1520
constructor() {
1621
super(SceneKey.MAIN_MENU);
1722
}
1823

19-
create() {
20-
if (!this._music) {
21-
this._music = this.sound.add('main-theme');
22-
}
24+
setupDependencies() {
25+
this._audioSystem = this.injector.get(DependencyKey.AUDIO_SYSTEM);
26+
}
2327

28+
create(data) {
2429
if (
25-
this.game.registry.get(RegistryKey.PLAY_SOUND) &&
26-
!this._music.isPlaying
30+
data.playMusic &&
31+
!this._audioSystem.get(AudioKey.MAIN_THEME)?.isPlaying
2732
) {
28-
this._music.play({ loop: true });
33+
this._audioSystem.play(AudioKey.MAIN_THEME);
2934
}
3035

3136
const logo = this.add.image(320, 60, 'logo').setOrigin(0.5, 0);
@@ -96,6 +101,10 @@ export class MainMenu extends Scene {
96101
}
97102

98103
startNewGame() {
104+
if (this._audioSystem.get(AudioKey.MAIN_THEME)?.isPlaying) {
105+
this._audioSystem.stop(AudioKey.MAIN_THEME);
106+
}
107+
99108
this.scene.start(SceneKey.BATTLE);
100109
}
101110

src/scenes/Preloader.js

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ import { LocalStorageKey, RegistryKey } from '../constants/data.js';
44
import { COLORS, MENU_ITEM_CONFIG } from '../constants/menu.js';
55
import { onButtonHover, onButtonOut } from '../utils/ui.js';
66
import { MenuSystem } from '../systems/menuSystem.js';
7+
import { Injector } from '../utils/injector.js';
8+
import { DependencyKey } from '../constants/injector.js';
9+
import { AudioSystem } from '../systems/audioSystem.js';
10+
import { AudioKey } from '../constants/audio.js';
711

812
const PROGRESS_BAR_WIDTH = 300;
913
const PROGRESS_BAR_HEIGHT = 32;
@@ -16,6 +20,8 @@ const SCENES_TO_LOAD = [
1620
'Credits',
1721
];
1822

23+
let dependencyInjector;
24+
1925
export class Preloader extends Scene {
2026
sceneImports;
2127
sceneLoaderPromise;
@@ -59,6 +65,8 @@ export class Preloader extends Scene {
5965
this.progressBarFill.width = (PROGRESS_BAR_WIDTH - 3) * progress;
6066
});
6167

68+
dependencyInjector = new Injector();
69+
6270
// Inject our CSS (for Usuzi Font)
6371
const element = document.createElement('style');
6472
document.head.appendChild(element);
@@ -73,12 +81,17 @@ export class Preloader extends Scene {
7381

7482
// Load the assets for the game - Replace with your own assets
7583
this.load.setPath('assets');
76-
this.load.audio('main-theme', [
84+
this.load.audio(AudioKey.MAIN_THEME, [
7785
'audio/main-theme.opus',
7886
'audio/main-theme.ogg',
7987
'audio/main-theme.mp3',
8088
]);
8189

90+
this.load.audio(AudioKey.BATTLE_THEME, [
91+
'audio/battle-theme.opus',
92+
'audio/battle-theme.mp3',
93+
]);
94+
8295
this.load.image('logo', 'last-one-flying-logo.png');
8396
this.load.image('player', 'player.png');
8497
this.load.image('player-laser-beam', 'player-laser-beam.png');
@@ -121,10 +134,21 @@ export class Preloader extends Scene {
121134
families: ['usuzi'],
122135
},
123136
active: async () => {
137+
const { AudioSystem } = await import('../systems/audioSystem.js');
138+
dependencyInjector.register(
139+
DependencyKey.AUDIO_SYSTEM,
140+
new AudioSystem(this.sound),
141+
);
142+
124143
const importResults = await this.sceneLoaderPromise;
125144
SCENES_TO_LOAD.map((sceneName, i) => {
126145
const loadedModule = importResults[i];
127-
this.scene.add(sceneName, loadedModule[sceneName]);
146+
const addedScene = this.scene.add(sceneName, loadedModule[sceneName]);
147+
addedScene.injector = dependencyInjector;
148+
149+
if (addedScene.setupDependencies) {
150+
addedScene.setupDependencies();
151+
}
128152
});
129153

130154
this.progressBar.setVisible(false);
@@ -159,11 +183,14 @@ export class Preloader extends Scene {
159183

160184
startGameWithSound() {
161185
this.game.registry.set(RegistryKey.PLAY_SOUND, true);
162-
this.scene.start(SceneKey.MAIN_MENU);
186+
this.scene.start(SceneKey.MAIN_MENU, {
187+
playMusic: true,
188+
});
163189
}
164190

165191
startGameMuted() {
166192
this.game.registry.set(RegistryKey.PLAY_SOUND, false);
193+
this.sound.setMute(true);
167194
this.scene.start(SceneKey.MAIN_MENU);
168195
}
169196
}

0 commit comments

Comments
 (0)