Add audio assets and integrate music/SFX handling

- Added new sound effect and music MP3 files to the project.
- Updated **BootScene** to preload all music tracks and sound effects.
- Implemented music playback:
  * MenuScene now plays looping title music and stops it on shutdown.
  * GameScene now stops any playing music on shutdown and starts level‑specific background music with fallback to the last track.
- Added SFX triggers for player shooting, alien shooting, missile launch, impacts, alien death, and player death.
- Cleaned up formatting and minor code style issues in GameScene (group/array declarations, tweens, UI text creation, level setup loops, etc.).
- Ensured proper cleanup of music objects when scenes change.
This commit is contained in:
Brian Fertig 2026-02-21 12:59:46 -07:00
parent f194886ed9
commit 26a80b6b89
17 changed files with 69 additions and 26 deletions

BIN
js/audio/fx/alien_death.mp3 Normal file

Binary file not shown.

BIN
js/audio/fx/alien_shoot.mp3 Normal file

Binary file not shown.

BIN
js/audio/fx/impact.mp3 Normal file

Binary file not shown.

BIN
js/audio/fx/missle.mp3 Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
js/audio/music/title.mp3 Normal file

Binary file not shown.

BIN
js/audio/music/track01.mp3 Normal file

Binary file not shown.

BIN
js/audio/music/track02.mp3 Normal file

Binary file not shown.

BIN
js/audio/music/track03.mp3 Normal file

Binary file not shown.

BIN
js/audio/music/track04.mp3 Normal file

Binary file not shown.

BIN
js/audio/music/track05.mp3 Normal file

Binary file not shown.

BIN
js/audio/music/track06.mp3 Normal file

Binary file not shown.

BIN
js/audio/music/track07.mp3 Normal file

Binary file not shown.

View File

@ -7,6 +7,24 @@ export default class BootScene extends Phaser.Scene {
super({ key: 'BootScene' });
}
preload() {
this.load.audio('title', 'js/audio/music/title.mp3');
this.load.audio('track01', 'js/audio/music/track01.mp3');
this.load.audio('track02', 'js/audio/music/track02.mp3');
this.load.audio('track03', 'js/audio/music/track03.mp3');
this.load.audio('track04', 'js/audio/music/track04.mp3');
this.load.audio('track05', 'js/audio/music/track05.mp3');
this.load.audio('track06', 'js/audio/music/track06.mp3');
this.load.audio('track07', 'js/audio/music/track07.mp3');
this.load.audio('sfx_player_shoot', 'js/audio/fx/player_shoot.mp3');
this.load.audio('sfx_player_death', 'js/audio/fx/player_death.mp3');
this.load.audio('sfx_alien_shoot', 'js/audio/fx/alien_shoot.mp3');
this.load.audio('sfx_missle', 'js/audio/fx/missle.mp3');
this.load.audio('sfx_impact', 'js/audio/fx/impact.mp3');
this.load.audio('sfx_alien_death', 'js/audio/fx/alien_death.mp3');
}
create() {
this._createTextures();
this.scene.start('MenuScene');

View File

@ -49,6 +49,9 @@ export default class GameScene extends Phaser.Scene {
this.cameras.main.setPostPipeline('CRTPipeline');
}
// Stop level music when this scene shuts down (e.g. returning to menu)
this.events.once('shutdown', () => { if (this.music) this.music.stop(); });
// Custom reticle cursor – hide the OS cursor and draw our own
this.game.canvas.style.cursor = 'none';
this.reticleOuter = this.add.image(800, 450, 'reticle_outer').setDepth(20);
@ -115,6 +118,16 @@ export default class GameScene extends Phaser.Scene {
// ─── Level Management ────────────────────────────────────────────────────
_playLevelMusic() {
if (this.music) { this.music.stop(); this.music.destroy(); this.music = null; }
const trackNum = String(this.level).padStart(2, '0');
const key = `track${trackNum}`;
// Fall back to the highest available track if this level doesn't have one yet
const resolvedKey = this.cache.audio.exists(key) ? key : 'track07';
this.music = this.sound.add(resolvedKey, { loop: true, volume: 0.7 });
this.music.play();
}
startLevel() {
this.levelComplete = false;
@ -144,6 +157,7 @@ export default class GameScene extends Phaser.Scene {
this.updateUI();
this.showMessage(`LEVEL ${this.level}`, 2000);
this._playLevelMusic();
}
// Random position along any screen edge, away from center
@ -186,18 +200,21 @@ export default class GameScene extends Phaser.Scene {
}
spawnPlayerBullet(x, y, angle) {
this.sound.play('sfx_player_shoot', { volume: 0.2 });
this.playerBullets.push(
new Bullet(this, x, y, angle, 'player', this.playerBulletsGroup)
);
}
spawnAlienBullet(x, y, angle, ownerShip = null) {
this.sound.play('sfx_alien_shoot', { volume: 0.45 });
this.alienBullets.push(
new Bullet(this, x, y, angle, 'alien', this.alienBulletsGroup, ownerShip)
);
}
spawnHomingMissile(x, y, ownerShip) {
this.sound.play('sfx_missle', { volume: 0.5 });
const player = this.player;
const angle = Phaser.Math.RadToDeg(
Math.atan2(player.sprite.y - y, player.sprite.x - x)
@ -233,6 +250,7 @@ export default class GameScene extends Phaser.Scene {
this.addScore(200);
this.spawnImpactSparks(bulletSprite.x, bulletSprite.y);
this.sound.play('sfx_alien_death', { volume: 0.65 });
alien.explode();
this.aliens = this.aliens.filter(a => a !== alien);
@ -276,6 +294,7 @@ export default class GameScene extends Phaser.Scene {
handlePlayerHit() {
if (!this.player.hit()) return; // invincible – ignore
this.sound.play('sfx_player_death', { volume: 0.7 });
this.player.die(); // hide ship and go invincible immediately
this.lives--;
this.updateUI();
@ -319,6 +338,7 @@ export default class GameScene extends Phaser.Scene {
}
spawnImpactSparks(x, y) {
this.sound.play('sfx_impact', { volume: 1.0 });
const count = Phaser.Math.Between(3, 5);
for (let i = 0; i < count; i++) {
const gfx = this.add.graphics();

View File

@ -46,6 +46,11 @@ export default class MenuScene extends Phaser.Scene {
if (this.renderer.type === Phaser.WEBGL) {
this.cameras.main.setPostPipeline('CRTPipeline');
}
// Title music — looping; stop when this scene shuts down
this.music = this.sound.add('title', { loop: true, volume: 0.7 });
this.music.play();
this.events.once('shutdown', () => { if (this.music) this.music.stop(); });
}
update() {