From 56addaeb7a520e306e91afa8eb9a90f19da987b1 Mon Sep 17 00:00:00 2001 From: Erico Vieira Porto Date: Wed, 2 Nov 2016 18:36:11 -0200 Subject: [PATCH] a working battle music implementation --- src/battle.js | 10 +++++++++- src/bgmusic.js | 35 +++++++++++++++++++++++++---------- src/descriptors/init.json | 4 +++- src/engine.js | 15 ++++++++++++++- src/resources.js | 3 ++- 5 files changed, 53 insertions(+), 14 deletions(-) diff --git a/src/battle.js b/src/battle.js index fc2741d..0356728 100644 --- a/src/battle.js +++ b/src/battle.js @@ -177,7 +177,9 @@ battle.action.skill = function(skill) { actions.showText(skill) } -battle.start = function(monsterlist) { +battle.start = function(monsterlist, musicBattle, musicHuzzah) { + battle.musicHuzzah = musicHuzzah + battle.musicBattle = musicBattle battle.holdAtomStack = engine.atomStack engine.atomStack = new Array(); battle.monster = []; @@ -204,6 +206,7 @@ battle.start = function(monsterlist) { battle.skills = resources.hms.Skills battle.setOrderStack(); + bgmusic.play(battle.musicBattle) actions.fadeIn('blackFadeIn;doNotKeep') } @@ -265,6 +268,7 @@ battle.resolveIfSideDead = function() { battle.order.pop(); } battle.ended = true + bgmusic.play(battle.musicHuzzah) actions.showText("You win!") battle.end("win") return true @@ -541,6 +545,10 @@ battle.end = function(battleresult) { engine.atomStack.push([function() { engine.atomStack = battle.holdAtomStack }, '']) + battle.holdAtomStack.push([function() { + bgmusic.popSong() + }, '']) + } battle.isPartyAlive = function() { diff --git a/src/bgmusic.js b/src/bgmusic.js index 9a608cb..d828dc2 100644 --- a/src/bgmusic.js +++ b/src/bgmusic.js @@ -2,17 +2,32 @@ bgmusic = { setup: function(){ + this.songStack = []; this.playing = ''; }, - play: function(song) { - if(this.playing != song){ - if(this.playing in resources.music){ - resources.music[this.playing].pause(); - } - this.playing = song; - resources.music[song].volume = 0.7 - resources.music[song].currentTime = 0; - resources.music[song].play(); - } + play: function(song, volume, currentTime) { + volume = (typeof volume === "undefined") ? 0.7 : volume; + currentTime = (typeof currentTime === "undefined") ? 0 : currentTime; + if(this.playing != song){ + if(this.playing in resources.music){ + resources.music[this.playing].pause(); + } + + this.playing = song; + resources.music[song].volume = volume; + resources.music[song].currentTime = currentTime; + resources.music[song].play(); + } + }, + pushSong: function(){ + var song = this.playing; + this.songStack.push({ song: song, + volume: resources.music[song].volume, + currentTime: resources.music[song].currentTime, + }); + }, + popSong: function(){ + var sp = this.songStack.pop(); + this.play(sp.song, sp.volume, sp.currentTime) } } diff --git a/src/descriptors/init.json b/src/descriptors/init.json index 9f59296..e30980a 100644 --- a/src/descriptors/init.json +++ b/src/descriptors/init.json @@ -35,7 +35,9 @@ }, "World": { "initLevel": "playerhouse", - "initMusic": "opening" + "initMusic": "opening", + "battleMusic": "battle_1", + "battleVictoryMusic": "battle_win" }, "itemsFile": "items.json" } diff --git a/src/engine.js b/src/engine.js index f4c9f87..41d590c 100644 --- a/src/engine.js +++ b/src/engine.js @@ -480,7 +480,20 @@ engine.actions.subtractItem = function(param) { } engine.actions.battle = function(param) { - battle.start(param) + var monsterlist = []; + var musicBattle = resources.init['World']['battleMusic']; + var musicHuzzah = resources.init['World']['battleVictoryMusic']; + for(var i=0; i