Add thruster sound effect and integrate it with the player
- Added new `thruster.mp3` asset and loaded it in **BootScene** (`sfx_thruster` key). - Extended `Player` class: * Created `thrusterSound` (looping) on construction. * Play sound while thrust is active and stop when thrust ends. * Ensure sound stops on player death and is destroyed on cleanup. - Minor cleanup to stop thruster audio when the player entity is destroyed.
This commit is contained in:
parent
59ab58b0a7
commit
0d1066aa2b
Binary file not shown.
|
|
@ -32,6 +32,8 @@ export default class Player {
|
||||||
this.lastFired = 0;
|
this.lastFired = 0;
|
||||||
this.invincible = false;
|
this.invincible = false;
|
||||||
this.invincibleUntil = 0;
|
this.invincibleUntil = 0;
|
||||||
|
|
||||||
|
this.thrusterSound = scene.sound.add('sfx_thruster', { loop: true, volume: 0.6 });
|
||||||
}
|
}
|
||||||
|
|
||||||
update(time, delta) {
|
update(time, delta) {
|
||||||
|
|
@ -67,8 +69,10 @@ export default class Player {
|
||||||
this.sprite.body.velocity.x += Math.cos(rad) * THRUST_ACCEL * dt;
|
this.sprite.body.velocity.x += Math.cos(rad) * THRUST_ACCEL * dt;
|
||||||
this.sprite.body.velocity.y += Math.sin(rad) * THRUST_ACCEL * dt;
|
this.sprite.body.velocity.y += Math.sin(rad) * THRUST_ACCEL * dt;
|
||||||
this.sprite.setTexture('player_thrust');
|
this.sprite.setTexture('player_thrust');
|
||||||
|
if (!this.thrusterSound.isPlaying) this.thrusterSound.play();
|
||||||
} else {
|
} else {
|
||||||
this.sprite.setTexture('player');
|
this.sprite.setTexture('player');
|
||||||
|
if (this.thrusterSound.isPlaying) this.thrusterSound.stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Cap total speed
|
// Cap total speed
|
||||||
|
|
@ -102,6 +106,7 @@ export default class Player {
|
||||||
// Hide the ship and go invincible immediately – call before delaying respawn
|
// Hide the ship and go invincible immediately – call before delaying respawn
|
||||||
die() {
|
die() {
|
||||||
this.alive = false;
|
this.alive = false;
|
||||||
|
if (this.thrusterSound.isPlaying) this.thrusterSound.stop();
|
||||||
this.invincible = true;
|
this.invincible = true;
|
||||||
this.invincibleUntil = this.scene.time.now + 99999;
|
this.invincibleUntil = this.scene.time.now + 99999;
|
||||||
this.sprite.body.setVelocity(0, 0);
|
this.sprite.body.setVelocity(0, 0);
|
||||||
|
|
@ -173,6 +178,7 @@ export default class Player {
|
||||||
|
|
||||||
destroy() {
|
destroy() {
|
||||||
this.alive = false;
|
this.alive = false;
|
||||||
|
if (this.thrusterSound) { this.thrusterSound.stop(); this.thrusterSound.destroy(); }
|
||||||
if (this.sprite && this.sprite.active) {
|
if (this.sprite && this.sprite.active) {
|
||||||
this.sprite.destroy();
|
this.sprite.destroy();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -23,6 +23,7 @@ export default class BootScene extends Phaser.Scene {
|
||||||
this.load.audio('sfx_missle', 'js/audio/fx/missle.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_impact', 'js/audio/fx/impact.mp3');
|
||||||
this.load.audio('sfx_alien_death', 'js/audio/fx/alien_death.mp3');
|
this.load.audio('sfx_alien_death', 'js/audio/fx/alien_death.mp3');
|
||||||
|
this.load.audio('sfx_thruster', 'js/audio/fx/thruster.mp3');
|
||||||
}
|
}
|
||||||
|
|
||||||
create() {
|
create() {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue