From be6973558240182f3069b88d0ae59a1c2e58a4c4 Mon Sep 17 00:00:00 2001 From: WhiteTreePaladin Date: Sun, 11 Sep 2011 03:30:41 +0000 Subject: [PATCH] Trying to clean up music in the scripts. It is still broken. This was SVN commit r10249. --- .../gui/common/functions_utility_music.js | 228 ++----------- binaries/data/mods/public/gui/common/music.js | 185 +++++++++++ .../data/mods/public/gui/loading/loading.js | 5 +- .../data/mods/public/gui/page_session.xml | 1 - .../data/mods/public/gui/pregame/mainmenu.js | 18 +- .../data/mods/public/gui/pregame/mainmenu.xml | 3 + binaries/data/mods/public/gui/session/menu.js | 16 +- .../data/mods/public/gui/session/session.js | 75 ++++- .../data/mods/public/gui/summary/summary.js | 2 +- .../data/mods/public/gui/summary/summary.xml | 307 +++++++++--------- 10 files changed, 468 insertions(+), 372 deletions(-) create mode 100644 binaries/data/mods/public/gui/common/music.js diff --git a/binaries/data/mods/public/gui/common/functions_utility_music.js b/binaries/data/mods/public/gui/common/functions_utility_music.js index 35362c9354..fe1dddc669 100644 --- a/binaries/data/mods/public/gui/common/functions_utility_music.js +++ b/binaries/data/mods/public/gui/common/functions_utility_music.js @@ -4,21 +4,7 @@ */ -/* - * These constants are used in session and summary - */ -var g_MusicGain = 0.3; -const RELATIVE_MUSIC_PATH = "audio/music/"; -var g_PeaceTracks = []; -var g_BattleTracks = []; - -const MAIN_MENU = "main_menu"; -const DEFEAT_CUE = "gen_loss_cue"; -const DEFEAT_MUSIC = "gen_loss_track"; -const VICTORY_MUSIC = "win_1"; -//const AMBIENT_SOUND = "audio/ambient/dayscape/day_temperate_gen_03.ogg"; -const BUTTON_SOUND = "audio/interface/ui/ui_button_longclick.ogg"; // ==================================================================== @@ -127,195 +113,45 @@ function crossFade (outHandle, inHandle, fadeDuration) // ==================================================================== +//const AMBIENT_SOUND = "audio/ambient/dayscape/day_temperate_gen_03.ogg"; -/* - * At some point, this ought to be extended to do dynamic music selection and - * crossfading - it at least needs to pick the music track based on the player's - * civ and peace/battle - */ +//const AMBIENT_TEMPERATE = "temperate"; +//var currentAmbient; -/* - * These functions are used in session and summary - * - */ - -function storeTracks(civMusic) -{ - for each (var music in civMusic) - { - if ("peace" == music["Type"]) - { - g_PeaceTracks.push(music["File"]); - } - else if ("battle" == music["Type"]) - { - g_BattleTracks.push(music["File"]); - } - } -} - -function getRandomPeaceTrack() -{ - return RELATIVE_MUSIC_PATH + g_PeaceTracks[getRandom(0, g_PeaceTracks.length-1)]; -} - -function getRandomBattleTrack() -{ - return RELATIVE_MUSIC_PATH + g_BattleTracks[getRandom(0, g_BattleTracks.length-1)]; -} - -function playMainMenuMusic() -{ - if (global.curr_music) - switchMusic(MAIN_MENU, 0.0, true); - else - playMusic(MAIN_MENU, 0.0, true); -} - -function stopMainMenuMusic() -{ - if (global.main_menu_music) - global.main_menu_music.fade(-1, 0.0, 5.0); -} - -function playButtonSound() -{ - var buttonSound = new Sound(BUTTON_SOUND); - buttonSound.play(); -} - -function playDefeatMusic() -{ - switchMusic(DEFEAT_CUE, 0.0, false); - switchMusic(DEFEAT_MUSIC, 10.0, true); -} - -function playVictoryMusic() -{ - switchMusic(VICTORY_MUSIC, 0.0, true); -} - -function startSessionSounds(civMusic) -{ - storeTracks(civMusic); - playRandomAmbientSound(); - playRandomCivMusic(); -} - -function startMusic() -{ - playRandomCivMusic(); -} - -function playRandomCivMusic() -{ - global.curr_music = new Sound(getRandomPeaceTrack()); - if (global.curr_music) - { - global.curr_music.loop(); - global.curr_music.fade(0.0, g_MusicGain, 10.0); - } -} - -function playRandomAmbientSound() -{ - // Seem to need the underscore at the end of "temperate" to avoid crash - // (Might be caused by trying to randomly load day_temperate.xml) -// global.curr_ambient = newRandomSound("ambient", "temperate_", "dayscape"); -// if (global.curr_ambient) -// { -// global.curr_ambient.loop(); -// global.curr_ambient.setGain(0.8); -// } - - // Just play this track for now. We don't randomly change to new tracks. - // Some of the existing ones are too annoying if played constantly. - global.curr_ambient = new Sound("audio/ambient/dayscape/day_temperate_gen_03.ogg"); - if (global.curr_ambient) - { - global.curr_ambient.loop(); - global.curr_ambient.setGain(0.8); - } - -} - -function playMusic(track, fadeInPeriod, isLooping) -{ - global.curr_music = new Sound(RELATIVE_MUSIC_PATH + track + ".ogg"); - - if (global.curr_music) - { - if (isLooping) - global.curr_music.loop(); - else - global.curr_music.play(); - - if (fadeInPeriod) - global.curr_music.fade(0.0, g_MusicGain, fadeInPeriod); - } -} - -function switchMusic(track, fadeInPeriod, isLooping) -{ - if (global.curr_music) - global.curr_music.fade(-1, 0.0, 5.0); - - playMusic(track, fadeInPeriod, isLooping); -} - -function stopSound() -{ - stopMusic(); - stopAmbient(); -} - -function stopMusic() -{ - if (global.curr_music) - { - global.curr_music.fade(-1, 0.0, 5.0); - global.curr_music = null; - } -} - -function stopAmbientSounds() -{ - if (global.curr_ambient) - { - global.curr_ambient.fade(-1, 0.0, 5.0); - global.curr_ambient = null; - } -} - -function isMusicPlaying() -{ - if (global.curr_music) - return true; - - return false; -} - -//function isEndingMusicPlaying() +//function playRandomAmbient(type) //{ -// if (global.curr_music) +// switch (type) // { -// if (global.curr_music[DEFEAT_CUE] || -// global.curr_music[DEFEAT_MUSIC] || -// global.curr_music[VICTORY_MUSIC]) -// { -// return true; -// } -// } +// case AMBIENT_TEMPERATE: +// // Seem to need the underscore at the end of "temperate" to avoid crash +// // (Might be caused by trying to randomly load day_temperate.xml) +// currentAmbient = newRandomSound("ambient", "temperate_", "dayscape"); +// if (currentAmbient) +// { +// currentAmbient.loop(); +// currentAmbient.setGain(0.8); +// } +// break; // -// return false; +// default: +// console.write("Unrecognized ambient type: " + type); +// break; +// } //} // -// -//function isMusicPlaying() +//function stopAmbient() //{ -// if (global.curr_music) -// return global.curr_music.isPlaying(); -// -// return false; +// if (currentAmbient) +// { +// currentAmbient.fade(-1, 0.0, 5.0); +// currentAmbient = null; +// } //} + +//const BUTTON_SOUND = "audio/interface/ui/ui_button_longclick.ogg"; +//function playButtonSound() +//{ +// var buttonSound = new Sound(BUTTON_SOUND); +// buttonSound.play(); +//} \ No newline at end of file diff --git a/binaries/data/mods/public/gui/common/music.js b/binaries/data/mods/public/gui/common/music.js new file mode 100644 index 0000000000..c6e6999613 --- /dev/null +++ b/binaries/data/mods/public/gui/common/music.js @@ -0,0 +1,185 @@ + +function initMusic() +{ + // Probably will need to put this in a place where it won't get + // reinitialized after every match. Otherwise, it will not remember + // the current track + + // Might need to use pregame for that sort of setup and move all the + // menu stuff to a main_menu page + + global.music = new Music(); +} + +function Music() +{ + this.RELATIVE_MUSIC_PATH = "audio/music/"; + this.MUSIC_PEACE = "peace"; + this.MUSIC_BATTLE = "battle"; + + this.tracks = { + MAIN_MENU_TRACK : "main_menu", + VICTORY_TRACK : "win_1", + DEFEAT_TRACK : "gen_loss_track", + DEFEAT_CUE_TRACK : "gen_loss_cue" + }; + + this.states = { + OFF : 0, + MENU : 1, + PEACE : 2, + BATTLE : 3, + VICTORY :4, + DEFEAT : 5, + DEFEAT_CUE : 6 + }; + + this.musicGain = 0.3; + + this.peaceTracks = []; + this.battleTracks = []; + + this.currentState = 0; + this.oldState = 0; + + this.currentMusic = null; +} + +// Needs to be called in onTick() to work +Music.prototype.update = function() +{ + if (this.currentState != this.oldState) + { + this.oldState = this.currentState; + + switch (this.currentState) + { + case this.states.OFF: + if (this.currentMusic) + { + this.currentMusic.fade(-1, 0.0, 5.0); + this.currentMusic = null; + } + break; + + case this.states.MENU: + this.switchMusic(this.tracks.MAIN_MENU_TRACK, 0.0, true); + break; + + case this.states.PEACE: + this.switchMusic(this.getRandomPeaceTrack(), 0.0, true); + break; + + case this.states.BATTLE: + this.switchMusic(this.getRandomBattleTrack(), 0.0, true); + break; + + case this.states.VICTORY: + this.switchMusic(this.tracks.VICTORY_TRACK, 0.0, true); + break; + + case this.states.DEFEAT: + this.switchMusic(this.tracks.DEFEAT_TRACK, 0.0, true); + break; + + case this.states.DEFEAT_CUE: + this.switchMusic(this.tracks.DEFEAT_CUE_TRACK, 0.0, false); + break; + + default: + console.write("Unknown music state: " + this.currentState); + break; + } + } + else if (!this.isMusicPlaying()) + { + console.write("not playing " + this.currentState); + + if (this.currentState == this.states.DEFEAT_CUE) + this.setState(this.states.DEFEAT); + } +}; + +Music.prototype.setState = function(state) +{ + this.currentState = state; +}; + +Music.prototype.storeTracks = function(civMusic) +{ + for each (var music in civMusic) + { + var type = music["Type"]; + + switch (type) + { + case this.MUSIC_PEACE: + this.peaceTracks.push(music["File"]); + break; + + case this.MUSIC_BATTLE: + this.battleTracks.push(music["File"]); + break; + + default: + console.write("Unrecognized music type: " + type); + break; + } + } +}; + +Music.prototype.getRandomPeaceTrack = function() +{ + return this.peaceTracks[getRandom(0, this.peaceTracks.length-1)]; +}; + +Music.prototype.getRandomBattleTrack = function() +{ + return this.battleTracks[getRandom(0, this.battleTracks.length-1)]; +}; + +Music.prototype.switchMusic = function(track, fadeInPeriod, isLooping) +{ + if (this.currentMusic) + { + this.currentMusic.fade(-1, 0.0, 5.0); + this.currentMusic = null; + } + + +console.write(this.RELATIVE_MUSIC_PATH + track + ".ogg"); + + this.currentMusic = new Sound(this.RELATIVE_MUSIC_PATH + track + ".ogg"); + +console.write(this.currentMusic); + + + if (this.currentMusic) + { + if (isLooping) + this.currentMusic.loop(); + else + this.currentMusic.play(); + + if (fadeInPeriod) + this.currentMusic.fade(0.0, this.musicGain, fadeInPeriod); + } +}; + +Music.prototype.isMusicPlaying = function() +{ + if (this.currentMusic) + return true; + + return false; +}; + +Music.prototype.startMusic = function() +{ + this.setState(this.states.PEACE); +}; + +Music.prototype.stopMusic = function() +{ + this.setState(this.states.OFF); +}; \ No newline at end of file diff --git a/binaries/data/mods/public/gui/loading/loading.js b/binaries/data/mods/public/gui/loading/loading.js index be9149da7e..ed7caeea40 100644 --- a/binaries/data/mods/public/gui/loading/loading.js +++ b/binaries/data/mods/public/gui/loading/loading.js @@ -91,8 +91,9 @@ function displayProgress() function reallyStartGame() { // Stop the music - if (global.curr_music) - global.curr_music.fade(-1, 0.0, 5.0); // fade to 0 over 5 seconds +// if (global.curr_music) +// global.curr_music.fade(-1, 0.0, 5.0); // fade to 0 over 5 seconds + // This is a reserved function name that is executed by the engine when it is ready // to start the game (i.e. loading progress has reached 100%). diff --git a/binaries/data/mods/public/gui/page_session.xml b/binaries/data/mods/public/gui/page_session.xml index 260ea7a4f4..eaf6a69ba7 100644 --- a/binaries/data/mods/public/gui/page_session.xml +++ b/binaries/data/mods/public/gui/page_session.xml @@ -5,7 +5,6 @@ common/sprite1.xml common/icon_sprites.xml -common/init.xml common/common_sprites.xml common/common_styles.xml diff --git a/binaries/data/mods/public/gui/pregame/mainmenu.js b/binaries/data/mods/public/gui/pregame/mainmenu.js index 29b56984de..937e552385 100644 --- a/binaries/data/mods/public/gui/pregame/mainmenu.js +++ b/binaries/data/mods/public/gui/pregame/mainmenu.js @@ -4,7 +4,10 @@ const MARGIN = 4; // menu border size function init() { - playMainMenuMusic(); + initMusic(); + + // Play main menu music + global.music.setState(global.music.states.MENU); userReportEnabledText = getGUIObjectByName("userReportEnabledText").caption; @@ -82,6 +85,9 @@ function formatUserReportStatus(status) function onTick() { + // Play tracks based on current music state + global.music.update(); + // Animate backgrounds scrollBackgrounds(); @@ -109,6 +115,14 @@ function onTick() * MENU FUNCTIONS */ +// Temporarily adding this here +//const BUTTON_SOUND = "audio/interface/ui/ui_button_longclick.ogg"; +//function playButtonSound() +//{ +// var buttonSound = new Sound(BUTTON_SOUND); +// buttonSound.play(); +//} + // Slide menu function updateMenuPosition() { @@ -158,7 +172,7 @@ function openMenu(newSubmenu, position, buttonHeight, numButtons) // Closes the menu and resets position function closeMenu() { - playButtonSound(); +// playButtonSound(); // remove old submenu type getGUIObjectByName(currentSubmenuType).hidden = true; diff --git a/binaries/data/mods/public/gui/pregame/mainmenu.xml b/binaries/data/mods/public/gui/pregame/mainmenu.xml index d584cae2be..cf84bfa8f8 100644 --- a/binaries/data/mods/public/gui/pregame/mainmenu.xml +++ b/binaries/data/mods/public/gui/pregame/mainmenu.xml @@ -5,6 +5,9 @@