diff --git a/sketch.js b/sketch.js index 45ffea7..34e3e39 100644 --- a/sketch.js +++ b/sketch.js @@ -13,6 +13,10 @@ var flagpole; var gravity; var game_score; var finish_position_x; +var audio = {}; +var sound_enabled = false; +var music_enabled = false; +var game_state; var trees_x = []; var clouds = []; @@ -92,7 +96,7 @@ function GameCharacter() { }; this._drawEyes = function (draw_left, draw_right, eyes_height) { fill(0); - if (this.curLives <= 0) { + if (game_state == "FAILURE") { textAlign(CENTER); textSize(this.x_step * 2); text( @@ -270,8 +274,15 @@ function GameCharacter() { pop(); }; this.takeLife = function () { + playSound(audio.hurt); gameChar.curLives--; gameChar.invincibility_time += 180; + if (this.curLives == 0) { + playSound(audio.failure_drum); + game_state = "FAILURE"; + // recheck music to stop playing + playMusic(); + } }; this._checkPlayerDie = function () { if (frameCount - this.death_timer > 60) { @@ -281,13 +292,20 @@ function GameCharacter() { startGame(); } else if (this.curLives == 1) { this.curLives--; + playSound(audio.failure_drum); } else { this.curLives = 0; + game_state = "FAILURE"; + // recheck music to stop playing + playMusic(); } } }; this.act = function () { + // undercapping invincibility time this.invincibility_time = max(0, this.invincibility_time - 1); + // capping x coordinate to the left + this.x = max(-150, this.x); if (!this.isPlummeting) { if (this.isLeft && !this.isRight) { this.x -= this.speed; @@ -361,6 +379,9 @@ function GameCharacter() { if (memorizedIndex != this.curGroundIndex) { this.y -= (height - floorPos_y) / 3; this.scale -= 0.1; + if (this.curGroundIndex == 0) playSound(audio.a_note); + if (this.curGroundIndex == 1) playSound(audio.b_note); + if (this.curGroundIndex == 2) playSound(audio.c_note); } }; this.goDown = function () { @@ -372,6 +393,9 @@ function GameCharacter() { if (memorizedIndex != this.curGroundIndex) { this.y += (height - floorPos_y) / 3; this.scale += 0.1; + if (this.curGroundIndex == 0) playSound(audio.a_note); + if (this.curGroundIndex == 1) playSound(audio.b_note); + if (this.curGroundIndex == 2) playSound(audio.c_note); } }; } @@ -473,7 +497,7 @@ function Enemy(curGroundIndex, x, y, size) { }; this.collidesWith = function (who) { if ( - dist(who.x, who.y, this.x, this.y) < this.size / 2 && + dist(who.x, who.y - 20 /*to account for gameChar's center*/, this.x, this.y) < this.size / 2 && who.curGroundIndex == this.curGroundIndex ) return true; @@ -484,6 +508,23 @@ function Enemy(curGroundIndex, x, y, size) { this.y = this.y + cos((frameCount / random(10, 20)) * random(0.5, 10)); }; } +function preload() { + soundFormats('mp3', 'wav'); + audio = { + a_note: loadSound('assets/audio/a_note.wav'), + b_note: loadSound('assets/audio/b_note.wav'), + c_note: loadSound('assets/audio/c_note.wav'), + coin: loadSound('assets/audio/coin.wav'), + failure_drum: loadSound('assets/audio/failure_drum.mp3'), + hurt: loadSound('assets/audio/hurt.wav'), + jump: loadSound('assets/audio/jump.wav'), + stream_short: loadSound('assets/audio/stream-short.wav'), + victory: loadSound('assets/audio/victory.wav'), + music: loadSound('assets/audio/Floating Cities.mp3') + } + audio.music.playMode('untilDone'); + audio.stream_short.playMode('untilDone'); +} function setup() { createCanvas(1024, 576); palette = { @@ -513,8 +554,11 @@ function setup() { startGame((level_start = true)); } function startGame(level_start) { + game_state = "LEVEL"; floorPos_y = 432; text_size = sqrt(width) + sqrt(height); + if (music_enabled) playMusic(); + audio.stream_short.stop(); // levels of depth where the character, collectable and enemies stand. groundPositions = [ floorPos_y + (height - floorPos_y) / 6, @@ -524,7 +568,7 @@ function startGame(level_start) { camera_speed = 0.9; gravity = 1; game_score = 0; - finish_position_x = 6000; + finish_position_x = 4000; textSize(width / 20); flagpole = { x: finish_position_x, @@ -544,6 +588,7 @@ function startGame(level_start) { gameChar.y = floorPos_y + (height - floorPos_y) / 6; gameChar.scale = 1; gameChar.curGroundIndex = 0; + audio.stream_short.stop(); gameChar.isPlummeting = false; gameChar.isFalling = false; gameChar.isJumping = false; @@ -785,23 +830,32 @@ function complexDraw() { function drawInterface() { push(); fill(0); - stroke(0); text('Score: ' + game_score, 12, text_size); + textSize(text_size/2); + if (music_enabled) phrase = "enabled"; else phrase = "disabled"; + text('Music - ' + phrase, 300, text_size/2); + if (sound_enabled) phrase = "enabled"; else phrase = "disabled"; + text('Sound - ' + phrase, 300, text_size); + textSize(text_size); + stroke(255); + strokeWeight(2); drawLives(); - if (gameChar.curLives < 1) + if (game_state == "FAILURE") { text('Game over. Press space to continue...', 0, height / 2); + } else if (flagpole.isReached) text('Level complete. Press space to continue...', 0, height / 2); if (showDebugData) { text(gameChar.curGroundIndex, 99, 99); text(gameChar.getCurGroundY(), 188, 99); text(gameChar.sprite, 277, 99); - + console.log(keyCode, key); // draw game character's trajectory drawCharTrace(); drawFps(); } pop(); + } function drawCharTrace() { debug_charTrace.push([gameChar.x, gameChar.y]); @@ -822,6 +876,7 @@ function checkRiver(t_river) { gameChar.x < river.x + river.width / 2 && gameChar.y > floorPos_y ) { + playSound(audio.stream_short); gameChar.isPlummeting = true; } } @@ -837,6 +892,7 @@ function checkCollectable(t_collectable) { ) < t_collectable.size / 2 ) { + playSound(audio.coin); t_collectable.isFound = true; game_score++; } @@ -1135,9 +1191,12 @@ function checkFlagpole() { // this function will trigger anyway. if (gameChar.x >= finish_position_x) { flagpole.isReached = true; + playSound(audio.victory); + game_state = "WIN"; + // Stops music by recheking the game state + playMusic(); } } - function drawFps() { push(); fps = Math.round(frameRate() * 10) / 10; @@ -1155,7 +1214,7 @@ function drawFps() { pop(); } function keyPressed() { - if (gameChar.curLives < 1 || flagpole.isReached) { + if (game_state == "FAILURE" || game_state == "WIN") { if (keyCode == 32 /*Space*/) { /*Hard resets the game*/ startGame((level_start = true)); @@ -1174,14 +1233,27 @@ function keyPressed() { !gameChar.isFalling && !gameChar.isJumping ) { + playSound(audio.jump); gameChar.isJumping = true; gameChar.curJumpingStrength = gameChar.baseJumpingStrength; } } if (keyCode == 77 /*M*/) showDebugData = !showDebugData; if (keyCode == 78 /*N*/) shadows_enabled = !shadows_enabled; + if (keyCode == 79 /*P*/) sound_enabled = !sound_enabled; + if (keyCode == 80 /*O*/) {music_enabled = !music_enabled; playMusic()}; } function keyReleased() { if (keyCode == 65 /*A*/ || keyCode == LEFT_ARROW) gameChar.isLeft = false; if (keyCode == 68 /*D*/ || keyCode == RIGHT_ARROW) gameChar.isRight = false; } +function playSound(sound) { + if (sound_enabled) sound.play(); +} +function playMusic() { + if (music_enabled && game_state == "LEVEL") { + audio.music.play(); + } else { + audio.music.stop(); + } +}