Skip to content

Commit

Permalink
Merge pull request #3 from gp2u/audio
Browse files Browse the repository at this point in the history
Audio
  • Loading branch information
masonicGIT committed Oct 27, 2017
2 parents 242cf91 + 762a4e6 commit c96895c
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 58 deletions.
2 changes: 1 addition & 1 deletion debug.htm
Expand Up @@ -4,7 +4,7 @@
<head>

<title>Pac-Man</title>
<!-- last updated: Sun 15 Oct 2017 21:12:13 AEDT -->
<!-- last updated: Tue 17 Oct 2017 16:50:08 AEDT -->
<!-- Lock viewport on mobile devices. -->
<!--
<meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;" />
Expand Down
2 changes: 1 addition & 1 deletion index.htm
Expand Up @@ -4,7 +4,7 @@
<head>

<title>Pac-Man</title>
<!-- last updated: Sun 15 Oct 2017 21:12:13 AEDT -->
<!-- last updated: Tue 17 Oct 2017 16:50:08 AEDT -->
<!-- Lock viewport on mobile devices. -->
<!--
<meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;" />
Expand Down
70 changes: 41 additions & 29 deletions pacman.js
Expand Up @@ -133,19 +133,17 @@ function preloadAudio() {
this.eating = new audioTrack('sounds/eating.mp3', 0.5);
this.startMusic = new audioTrack('sounds/start-music.mp3');

this.ghostReset = function() {
console.log('***** ghostReset');
this.ghostReset = function(noResetTime) {
for (var s in this) {
if (s == 'silence' || s == 'ghostReset' ) return;
console.log('silencing: ' + s);
if (s.match(/^ghost/)) this[s].stopLoop();
if (s.match(/^ghost/)) this[s].stopLoop(noResetTime);
}
};

this.silence = function() {
this.silence = function(noResetTime) {
for (var s in this) {
if (s == 'silence' || s == 'ghostReset' ) return;
this[s].stopLoop();
this[s].stopLoop(noResetTime);
}
}
}
Expand Down Expand Up @@ -7581,7 +7579,6 @@ var GHOST_GOING_HOME = 2;
var GHOST_ENTERING_HOME = 3;
var GHOST_PACING_HOME = 4;
var GHOST_LEAVING_HOME = 5;
var ghostsOutside = 1;

// Ghost constructor
var Ghost = function() {
Expand Down Expand Up @@ -7746,29 +7743,41 @@ Ghost.prototype.reverse = function() {
// set after the update() function is called so that we are still frozen
// for 3 seconds before traveling home uninterrupted.
Ghost.prototype.goHome = function() {
ghostsOutside--;
audio.silence();
audio.eatingGhost.play();
setTimeout(audio.ghostReturnToHome.play(), 500);
this.mode = GHOST_EATEN;
};

// Following the pattern that state changes be made via signaling (e.g. reversing, going home)
// the ghost is commanded to leave home similarly.
// (not sure if this is correct yet)
Ghost.prototype.leaveHome = function() {
ghostsOutside++;
this.playSiren();
this.playSounds();
this.sigLeaveHome = true;
};

Ghost.prototype.playSiren = function() {
if (ghostsOutside > 0)
audio.ghostNormalMove.startLoop(true);
}

Ghost.prototype.stopSiren = function() {
audio.ghostNormalMove.stopLoop(true);
Ghost.prototype.playSounds = function() {
var ghostsOutside = 0;
var ghostsGoingHome = 0;
for (var i=0; i<4; i++) {
if (ghosts[i].mode == GHOST_OUTSIDE) ghostsOutside++;
if (ghosts[i].mode == GHOST_GOING_HOME) ghostsGoingHome++;
}
if (ghostsGoingHome > 0) {
audio.ghostNormalMove.stopLoop();
audio.ghostReturnToHome.startLoop(true);
return;
}
else {
audio.ghostReturnToHome.stopLoop();
}
if (ghostsOutside > 0 ) {
if (! this.scared)
audio.ghostNormalMove.startLoop(true);
}
else {
audio.ghostNormalMove.stopLoop();
}
}

// function called when pacman eats an energizer
Expand Down Expand Up @@ -7809,7 +7818,7 @@ Ghost.prototype.homeSteer = (function(){
// walk to the door, or go through if already there
if (this.pixel.x == map.doorPixel.x) {
this.mode = GHOST_ENTERING_HOME;
audio.ghostReturnToHome.stop();
this.playSounds();
this.setDir(DIR_DOWN);
this.faceDirEnum = this.dirEnum;
}
Expand Down Expand Up @@ -8239,7 +8248,6 @@ Player.prototype.update = function(j) {

// eat something
if (map) {
console.log(this.tile.x, this.lastMeal.x, this.tile.y, this.lastMeal.y);
var t = map.getTile(this.tile.x, this.tile.y);
if (t == '.' || t == 'o') {
this.lastMeal.x = this.tile.x;
Expand Down Expand Up @@ -8950,7 +8958,7 @@ var energizer = (function() {
save: save,
load: load,
reset: function() {
audio.ghostTurnToBlue.stop();
audio.ghostTurnToBlue.stopLoop();
count = 0;
active = false;
points = 100;
Expand All @@ -8968,7 +8976,8 @@ var energizer = (function() {
}
},
activate: function() {
audio.ghostTurnToBlue.play();
audio.ghostNormalMove.stopLoop();
audio.ghostTurnToBlue.startLoop();
active = true;
count = 0;
points = 100;
Expand Down Expand Up @@ -9051,6 +9060,9 @@ BaseFruit.prototype = {
testCollide: function() {
if (this.isPresent() && this.isCollide()) {
addScore(this.getPoints());
audio.silence(true);
audio.eatingFruit.play();
setTimeout(ghosts[0].playSounds, 500);
this.reset();
this.scoreFramesLeft = this.scoreDuration*60;
}
Expand Down Expand Up @@ -9499,6 +9511,7 @@ var state;
// switches to another game state
var switchState = function(nextState,fadeDuration, continueUpdate1, continueUpdate2) {
state = (fadeDuration) ? fadeNextState(state,nextState,fadeDuration,continueUpdate1, continueUpdate2) : nextState;
audio.silence();
state.init();
if (executive.isPaused()) {
executive.togglePause();
Expand Down Expand Up @@ -9620,7 +9633,6 @@ var homeState = (function(){
menu.addTextIconButton("LEARN",
function() {
exitTo(learnState);
audio.silence();
},
function(ctx,x,y,frame) {
atlas.drawGhostSprite(ctx,x,y,Math.floor(frame/8)%2,DIR_RIGHT,false,false,false,blinky.color);
Expand All @@ -9629,7 +9641,6 @@ var homeState = (function(){
return {
init: function() {
menu.enable();
audio.silence();
audio.coffeeBreakMusic.startLoop();
},
draw: function() {
Expand Down Expand Up @@ -9982,7 +9993,6 @@ var preNewGameState = (function() {

return {
init: function() {
audio.silence();
audio.startMusic.play();
menu.enable();
gameTitleState.init();
Expand Down Expand Up @@ -10654,7 +10664,6 @@ var newGameState = (function() {

return {
init: function() {
audio.silence();
clearCheats();
frames = 0;
level = startLevel-1;
Expand Down Expand Up @@ -10696,7 +10705,6 @@ var readyState = (function(){

return {
init: function() {
audio.silence();
audio.startMusic.play();
var i;
for (i=0; i<5; i++)
Expand Down Expand Up @@ -10847,6 +10855,7 @@ var playState = {
ghosts[i].mode = GHOST_GOING_HOME;
ghosts[i].targetting = 'door';
}
ghosts[0].playSounds();
}

if (!skip) {
Expand All @@ -10869,8 +10878,9 @@ var playState = {

// finish level if all dots have been eaten
if (map.allDotsEaten()) {
//this.draw();
//this.draw();
switchState(finishState);
audio.extend.play();
break;
}

Expand Down Expand Up @@ -10995,7 +11005,6 @@ var deadState = (function() {
triggers: {
0: { // freeze
init: function() {
audio.silence();
audio.die.play();
},
update: function() {
Expand Down Expand Up @@ -11416,8 +11425,11 @@ var playCutScene = function(cutScene, nextState) {
map = undefined;
renderer.drawMap(true);

// miss the audio silence and time it cleanly for pacman cut scene 1
setTimeout(audio.coffeeBreakMusic.startLoop, 1200);
cutScene.nextState = nextState;
switchState(cutScene, 60);

};

var pacmanCutscene1 = newChildObject(scriptState, {
Expand Down
37 changes: 24 additions & 13 deletions src/Ghost.js
Expand Up @@ -8,7 +8,6 @@ var GHOST_GOING_HOME = 2;
var GHOST_ENTERING_HOME = 3;
var GHOST_PACING_HOME = 4;
var GHOST_LEAVING_HOME = 5;
var ghostsOutside = 1;

// Ghost constructor
var Ghost = function() {
Expand Down Expand Up @@ -173,29 +172,41 @@ Ghost.prototype.reverse = function() {
// set after the update() function is called so that we are still frozen
// for 3 seconds before traveling home uninterrupted.
Ghost.prototype.goHome = function() {
ghostsOutside--;
audio.silence();
audio.eatingGhost.play();
setTimeout(audio.ghostReturnToHome.play(), 500);
this.mode = GHOST_EATEN;
};

// Following the pattern that state changes be made via signaling (e.g. reversing, going home)
// the ghost is commanded to leave home similarly.
// (not sure if this is correct yet)
Ghost.prototype.leaveHome = function() {
ghostsOutside++;
this.playSiren();
this.playSounds();
this.sigLeaveHome = true;
};

Ghost.prototype.playSiren = function() {
if (ghostsOutside > 0)
audio.ghostNormalMove.startLoop(true);
}

Ghost.prototype.stopSiren = function() {
audio.ghostNormalMove.stopLoop(true);
Ghost.prototype.playSounds = function() {
var ghostsOutside = 0;
var ghostsGoingHome = 0;
for (var i=0; i<4; i++) {
if (ghosts[i].mode == GHOST_OUTSIDE) ghostsOutside++;
if (ghosts[i].mode == GHOST_GOING_HOME) ghostsGoingHome++;
}
if (ghostsGoingHome > 0) {
audio.ghostNormalMove.stopLoop();
audio.ghostReturnToHome.startLoop(true);
return;
}
else {
audio.ghostReturnToHome.stopLoop();
}
if (ghostsOutside > 0 ) {
if (! this.scared)
audio.ghostNormalMove.startLoop(true);
}
else {
audio.ghostNormalMove.stopLoop();
}
}

// function called when pacman eats an energizer
Expand Down Expand Up @@ -236,7 +247,7 @@ Ghost.prototype.homeSteer = (function(){
// walk to the door, or go through if already there
if (this.pixel.x == map.doorPixel.x) {
this.mode = GHOST_ENTERING_HOME;
audio.ghostReturnToHome.stop();
this.playSounds();
this.setDir(DIR_DOWN);
this.faceDirEnum = this.dirEnum;
}
Expand Down
1 change: 0 additions & 1 deletion src/Player.js
Expand Up @@ -205,7 +205,6 @@ Player.prototype.update = function(j) {

// eat something
if (map) {
console.log(this.tile.x, this.lastMeal.x, this.tile.y, this.lastMeal.y);
var t = map.getTile(this.tile.x, this.tile.y);
if (t == '.' || t == 'o') {
this.lastMeal.x = this.tile.x;
Expand Down
3 changes: 3 additions & 0 deletions src/cutscenes.js
Expand Up @@ -8,8 +8,11 @@ var playCutScene = function(cutScene, nextState) {
map = undefined;
renderer.drawMap(true);

// miss the audio silence and time it cleanly for pacman cut scene 1
setTimeout(audio.coffeeBreakMusic.startLoop, 1200);
cutScene.nextState = nextState;
switchState(cutScene, 60);

};

var pacmanCutscene1 = newChildObject(scriptState, {
Expand Down
5 changes: 3 additions & 2 deletions src/energizer.js
Expand Up @@ -60,7 +60,7 @@ var energizer = (function() {
save: save,
load: load,
reset: function() {
audio.ghostTurnToBlue.stop();
audio.ghostTurnToBlue.stopLoop();
count = 0;
active = false;
points = 100;
Expand All @@ -78,7 +78,8 @@ var energizer = (function() {
}
},
activate: function() {
audio.ghostTurnToBlue.play();
audio.ghostNormalMove.stopLoop();
audio.ghostTurnToBlue.startLoop();
active = true;
count = 0;
points = 100;
Expand Down
3 changes: 3 additions & 0 deletions src/fruit.js
Expand Up @@ -52,6 +52,9 @@ BaseFruit.prototype = {
testCollide: function() {
if (this.isPresent() && this.isCollide()) {
addScore(this.getPoints());
audio.silence(true);
audio.eatingFruit.play();
setTimeout(ghosts[0].playSounds, 500);
this.reset();
this.scoreFramesLeft = this.scoreDuration*60;
}
Expand Down
8 changes: 4 additions & 4 deletions src/sound.js
Expand Up @@ -68,17 +68,17 @@ function preloadAudio() {
this.eating = new audioTrack('sounds/eating.mp3', 0.5);
this.startMusic = new audioTrack('sounds/start-music.mp3');

this.ghostReset = function() {
this.ghostReset = function(noResetTime) {
for (var s in this) {
if (s == 'silence' || s == 'ghostReset' ) return;
if (s.match(/^ghost/)) this[s].stopLoop();
if (s.match(/^ghost/)) this[s].stopLoop(noResetTime);
}
};

this.silence = function() {
this.silence = function(noResetTime) {
for (var s in this) {
if (s == 'silence' || s == 'ghostReset' ) return;
this[s].stopLoop();
this[s].stopLoop(noResetTime);
}
}
}

0 comments on commit c96895c

Please sign in to comment.