diff --git a/src/app/GameUI.ts b/src/app/GameUI.ts index cf4f6c4..970591b 100644 --- a/src/app/GameUI.ts +++ b/src/app/GameUI.ts @@ -11,8 +11,13 @@ module SpaceTac { // Current focused star system star: Game.Star; - constructor() { - super(1280, 720, Phaser.AUTO, '-space-tac'); + // Audio manager + audio: View.Audio; + + constructor(headless: boolean = false) { + super(1280, 720, headless ? Phaser.HEADLESS : Phaser.AUTO, '-space-tac'); + + this.audio = new View.Audio(this); this.session = new Game.GameSession(); this.star = null; diff --git a/src/app/view/BaseView.ts b/src/app/view/BaseView.ts index f6bb265..6390b36 100644 --- a/src/app/view/BaseView.ts +++ b/src/app/view/BaseView.ts @@ -43,6 +43,10 @@ module SpaceTac.View { this.gameui.loadGame(); this.game.state.start("router"); }); + var key_m = this.input.keyboard.addKey(Phaser.Keyboard.M); + key_m.onUp.add(() => { + this.gameui.audio.toggleMute(); + }); } } } diff --git a/src/app/view/Preload.ts b/src/app/view/Preload.ts index 421b9af..b34da55 100644 --- a/src/app/view/Preload.ts +++ b/src/app/view/Preload.ts @@ -66,6 +66,10 @@ module SpaceTac.View { // Load sounds this.loadSound("battle/ship-change.wav"); this.loadSound("battle/weapon-bullets.wav"); + + // Load musics + this.loadSound("music/walking-along.mp3"); + this.loadSound("music/full-on.mp3"); } create() { @@ -77,7 +81,7 @@ module SpaceTac.View { } private loadSound(path: string) { - var key = path.replace(/\//g, "-").replace(".wav", ""); + var key = path.replace(/\//g, "-").replace(".wav", "").replace(".mp3", ""); this.load.audio(key, "assets/sounds/" + path); } } diff --git a/src/app/view/battle/Arena.ts b/src/app/view/battle/Arena.ts index 0626874..1d8e73d 100644 --- a/src/app/view/battle/Arena.ts +++ b/src/app/view/battle/Arena.ts @@ -120,7 +120,7 @@ module SpaceTac.View { } this.playing = arena_ship; - Sound.playOnce(this.game, "battle-ship-change"); + this.battleview.gameui.audio.playOnce("battle-ship-change"); } } } diff --git a/src/app/view/battle/BattleView.ts b/src/app/view/battle/BattleView.ts index 2c67bde..b3b2754 100644 --- a/src/app/view/battle/BattleView.ts +++ b/src/app/view/battle/BattleView.ts @@ -110,6 +110,9 @@ module SpaceTac.View { // "Battle" animation this.displayFightMessage(); + // BGM + this.gameui.audio.startMusic("full-on"); + // Key mapping var key_space = this.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR); key_space.onUp.add(this.onSpaceKeyPressed, this); diff --git a/src/app/view/common/Audio.ts b/src/app/view/common/Audio.ts new file mode 100644 index 0000000..9f7d018 --- /dev/null +++ b/src/app/view/common/Audio.ts @@ -0,0 +1,61 @@ +module SpaceTac.View { + "use strict"; + + // Utility functions for sounds + export class Audio { + + private game: Phaser.Game; + + private music: Phaser.Sound; + + constructor(game: Phaser.Game) { + this.game = game; + this.music = null; + } + + // Check if the sound system is up and running + isActive(): boolean { + return this.game.sound.context ? true : false; + } + + // Play a ponctual sound + playOnce(key: string): void { + if (this.isActive()) { + this.game.sound.play(key); + } + } + + // Start a background music + startMusic(key: string): void { + if (this.isActive()) { + if (this.music && this.music.key !== key) { + this.stopMusic(); + } + if (!this.music) { + this.music = this.game.sound.play("music-" + key, 1, true); + } + } + } + + // Stop currently playing background music + stopMusic(): void { + if (this.isActive()) { + if (this.music) { + this.music.stop(); + this.music = null; + } + } + } + + // Toggle the mute status of the sound system + toggleMute(): void { + if (this.isActive()) { + if (this.game.sound.volume > 0) { + this.game.sound.volume = 0; + } else { + this.game.sound.volume = 1; + } + } + } + } +} diff --git a/src/app/view/common/Sound.ts b/src/app/view/common/Sound.ts deleted file mode 100644 index da71527..0000000 --- a/src/app/view/common/Sound.ts +++ /dev/null @@ -1,14 +0,0 @@ -module SpaceTac.View { - "use strict"; - - // Utility functions for sounds - export class Sound { - - // Play a ponctual sound - static playOnce(game: Phaser.Game, key: string): void { - if (game.sound.context) { - game.sound.play("battle-ship-change"); - } - } - } -} diff --git a/src/app/view/map/StarSystemView.ts b/src/app/view/map/StarSystemView.ts index 2e03a66..b499d6c 100644 --- a/src/app/view/map/StarSystemView.ts +++ b/src/app/view/map/StarSystemView.ts @@ -50,6 +50,8 @@ module SpaceTac.View { this.button_jump.visible = false; this.drawAll(); + + this.gameui.audio.startMusic("walking-along"); } // Leaving the view, unbind and destroy diff --git a/src/app/view/map/UniverseMapView.ts b/src/app/view/map/UniverseMapView.ts index a3c777a..93535b2 100644 --- a/src/app/view/map/UniverseMapView.ts +++ b/src/app/view/map/UniverseMapView.ts @@ -40,6 +40,8 @@ module SpaceTac.View { this.drawAll(); + this.gameui.audio.startMusic("walking-along"); + // Inputs this.input.keyboard.addKey(Phaser.Keyboard.R).onUp.addOnce(this.revealAll, this); } diff --git a/src/app/view/specs/TestGame.ts b/src/app/view/specs/TestGame.ts index 9f9087d..0c8bdf8 100644 --- a/src/app/view/specs/TestGame.ts +++ b/src/app/view/specs/TestGame.ts @@ -11,7 +11,7 @@ module SpaceTac.View.Specs { spyOn(console, "log").and.stub(); spyOn(console, "warn").and.stub(); - var game = new Phaser.Game(500, 500, Phaser.HEADLESS); + var game = new GameUI(true); if (!state) { state = new Phaser.State(); diff --git a/src/assets/sounds/music/full-on.mp3 b/src/assets/sounds/music/full-on.mp3 new file mode 100644 index 0000000..4fcc160 Binary files /dev/null and b/src/assets/sounds/music/full-on.mp3 differ diff --git a/src/assets/sounds/music/walking-along.mp3 b/src/assets/sounds/music/walking-along.mp3 new file mode 100644 index 0000000..7c61828 Binary files /dev/null and b/src/assets/sounds/music/walking-along.mp3 differ