-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
1,271 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
//all asteroid related functions | ||
function generateAsteroid(asteroid) { | ||
asteroid.x = Math.random() * (astRangeX) + padding; | ||
asteroid.y = Math.random() * (astRangeY) + sBHeight + padding; | ||
while (detectRangeCollision(ship, asteroid)) { | ||
asteroid.x = Math.random() * (astRangeX) + padding; | ||
asteroid.y = Math.random() * (astRangeY) + sBHeight + padding; | ||
console.log(JSON.stringify(asteroid.name) + " regenerated"); | ||
} | ||
givePlayerImmunity(500); | ||
asteroid.exist = true; | ||
asteroid.moving = false; | ||
} | ||
|
||
function moveCheese() { | ||
if (cheese.exist && detectRange(ship, cheese)) { | ||
switch (ship.direction) { | ||
case 1: | ||
cheese.y -= ship.speed * 0.5; | ||
detectCheeseBorderCol(cheese); | ||
break; | ||
case 2: | ||
cheese.x += ship.speed * 0.5; | ||
detectCheeseBorderCol(cheese); | ||
break; | ||
case 3: | ||
cheese.y += ship.speed * 0.5; | ||
detectCheeseBorderCol(cheese); | ||
break; | ||
case 4: | ||
cheese.x -= ship.speed * 0.5; | ||
detectCheeseBorderCol(cheese); | ||
break; | ||
case 5: | ||
cheese.y -= ship.speed * 0.5; | ||
cheese.x += ship.speed * 0.5; | ||
detectCheeseBorderCol(cheese); | ||
break; | ||
case 6: | ||
cheese.y += ship.speed * 0.5; | ||
cheese.x += ship.speed * 0.5; | ||
detectCheeseBorderCol(cheese); | ||
break; | ||
case 7: | ||
cheese.y += ship.speed * 0.5; | ||
cheese.x -= ship.speed * 0.5; | ||
detectCheeseBorderCol(cheese); | ||
break; | ||
case 8: | ||
cheese.y -= ship.speed * 0.5; | ||
cheese.x -= ship.speed * 0.5; | ||
detectCheeseBorderCol(cheese); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
function handleAsteroids() { //moving & drawing asteroids as score goes up | ||
if (cheese.exist) { | ||
cheese.drawAst(); | ||
moveCheese(); | ||
} | ||
if (level == 1 && redAst.exist) { | ||
redAst.drawAst(); | ||
if (!redAst.moving) { | ||
clearInterval(redAst.moveInterval); | ||
redAst.moveInterval = setInterval(() => redAst.move(), 30); | ||
redAst.moving = true; | ||
} | ||
} | ||
if (level == 2 && redAst2.exist) { | ||
redAst.drawAst(); | ||
redAst2.drawAst(); | ||
if (!redAst2.moving) { | ||
clearInterval(redAst2.moveInterval); | ||
redAst2.moveInterval = setInterval(() => redAst2.move(), 30); | ||
redAst2.moving = true; | ||
} | ||
} | ||
if (((level == 3) || (level == 4)) && redAst3.exist) { | ||
redAst.drawAst(); | ||
redAst2.drawAst(); | ||
redAst3.drawAst(); | ||
if (!redAst3.moving) { | ||
clearInterval(redAst3.moveInterval); | ||
redAst3.moveInterval = setInterval(() => redAst3.move(), 30); | ||
redAst3.moving = true; | ||
} | ||
} | ||
if (level == 5 && redAst4.exist) { | ||
redAst.drawAst(); | ||
redAst2.drawAst(); | ||
redAst3.drawAst(); | ||
redAst4.drawAst(); | ||
if (!redAst4.moving) { | ||
clearInterval(redAst4.moveInterval); | ||
redAst4.moveInterval = setInterval(() => redAst4.move(), 30); | ||
redAst4.moving = true; | ||
} | ||
} | ||
if (level > 5) { | ||
redAst.drawAst(); | ||
redAst2.drawAst(); | ||
redAst3.drawAst(); | ||
redAst4.drawAst(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
//Audio | ||
let currentMusic; //buffer var for music | ||
let backgroundMusicOn = false; | ||
let menuMusicOn = false; | ||
let menuMusicPaused = false; | ||
|
||
//New sounds; no sources since they need to be set every time apparently | ||
let soundFX = new Audio(); | ||
let dingSound = new Audio(); | ||
let popSound = new Audio(); | ||
|
||
function playSoundFX(source) { | ||
soundFX.src = source; //source being changed too often; interrupting audio loading | ||
soundFX.play(); | ||
} | ||
function playDingSound() { | ||
dingSound.src = "Audio/ding.wav"; | ||
dingSound.play(); | ||
} | ||
function playPopSound() { | ||
popSound.src = "Audio/pop.ogg"; //need to reset the audio source everytime | ||
popSound.play(); | ||
} | ||
|
||
//background music | ||
let bkgMusic = new Audio(); | ||
bkgMusic.addEventListener('playing', function () { | ||
backgroundMusicOn = true; | ||
}) | ||
bkgMusic.addEventListener('ended', function () { | ||
if (bkgMusic.src == "http://127.0.0.1:5500/Audio/NESBossIntroSketchyLogic.wav") { | ||
currentMusic = "Audio/NESBossMainSketchyLogic.wav"; | ||
bkgMusic.src = currentMusic; | ||
} | ||
bkgMusic.play(); | ||
}) | ||
|
||
let introPlayed = false; | ||
function playBkgMusic() { | ||
if (!currentMusic) { | ||
currentMusic = "Audio/rainingBitsGundatsch.ogg"; | ||
} | ||
bkgMusic.src = currentMusic; | ||
} | ||
|
||
function changeBkgMusic(source) { | ||
currentMusic = source; | ||
playSoundFX("Audio/click.wav"); | ||
} | ||
|
||
//menu music | ||
let menuMusic = new Audio(); | ||
let menuMusicNumber = 0; | ||
menuMusic.addEventListener('playing', function () { | ||
menuMusicOn = true; | ||
}) | ||
menuMusic.addEventListener('ended', function () { | ||
menuMusicOn = false; | ||
}) | ||
menuMusic.addEventListener('pause', function () { | ||
menuMusicPaused = true; | ||
}) | ||
|
||
function randomizeMenuMusic() { | ||
menuMusic.pause(); | ||
menuMusicNumber = Math.floor((Math.random() * 3) + 1); | ||
switch (menuMusicNumber) { | ||
case 1: | ||
menuMusic.src = "Audio/menuDeepSeaUmplix.mp3"; | ||
break; | ||
case 2: | ||
menuMusic.src = "Audio/menuMagicSpaceCodeManu.mp3"; | ||
break; | ||
case 3: | ||
menuMusic.src = "Audio/menuLSLBMorris.wav"; | ||
break; | ||
} | ||
if (userInteracted) { | ||
menuMusic.play(); | ||
} | ||
} | ||
|
||
function changeMenuMusic(source) { | ||
menuMusic.pause(); | ||
menuMusic.src = source; | ||
menuMusic.play(); | ||
playSoundFX("Audio/click.wav"); | ||
} | ||
|
||
function playMenuMusic() { | ||
if (!menuMusic.src) { | ||
randomizeMenuMusic(); | ||
} | ||
if (userInteracted && (!menuMusicOn || menuMusicPaused)) { | ||
menuMusic.play(); | ||
} | ||
} | ||
|
||
function prevMusicPage() { | ||
//if on page 1 go to page 3 | ||
if (!musicPage1.classList.contains("hidden")) { | ||
musicPage1.classList.add("hidden"); | ||
musicPage3.classList.remove("hidden"); | ||
} | ||
//if on page 2 go to page 1 | ||
else if (!musicPage2.classList.contains("hidden")) { | ||
musicPage2.classList.add("hidden"); | ||
musicPage1.classList.remove("hidden"); | ||
} | ||
//if on page 3 go to page 2 | ||
else if (!musicPage3.classList.contains("hidden")) { | ||
musicPage3.classList.add("hidden"); | ||
musicPage2.classList.remove("hidden"); | ||
} | ||
playSoundFX("Audio/click.wav"); | ||
} | ||
function nextMusicPage() { | ||
//if on page 1 go to page 2 | ||
if (!musicPage1.classList.contains("hidden")) { | ||
musicPage1.classList.add("hidden"); | ||
musicPage2.classList.remove("hidden"); | ||
} | ||
//if on page 2 go to page 3 | ||
else if (!musicPage2.classList.contains("hidden")) { | ||
musicPage2.classList.add("hidden"); | ||
musicPage3.classList.remove("hidden"); | ||
} | ||
//if on page 3 go to page 1 | ||
else if (!musicPage3.classList.contains("hidden")) { | ||
musicPage3.classList.add("hidden"); | ||
musicPage1.classList.remove("hidden"); | ||
} | ||
playSoundFX("Audio/click.wav"); | ||
} | ||
|
||
function updateVolume() { | ||
soundFX.volume = sFXRange.value / 100; | ||
popSound.volume = sFXRange.value / 100; | ||
dingSound.volume = sFXRange.value / 100; | ||
bkgMusic.volume = musicRange.value / 180; | ||
menuMusic.volume = musicRange.value / 200; | ||
if (sFXRange.value > 0) { | ||
sFXButton.src = "Audio/audioUnmuted.png"; | ||
} | ||
else if (sFXRange.value == 0) { | ||
sFXButton.src = "Audio/audioMuted.png"; | ||
} | ||
if (musicRange.value > 0) { | ||
musicToggleButton.src = "Audio/musicUnmuted.jpg"; | ||
} | ||
else if (musicRange.value == 0) { | ||
musicToggleButton.src = "Audio/musicMuted.png"; | ||
} | ||
window.localStorage.setItem('sFXRange', JSON.stringify(sFXRange.value)); | ||
window.localStorage.setItem('musicRange', JSON.stringify(musicRange.value)); | ||
} | ||
|
||
//menu music start thing to make chrome happy | ||
document.body.addEventListener('click', function () { | ||
userInteracted = true; | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
//collision detection; first "if" is for x-values/domain, second "if" is for y-values/range | ||
function detectCollision(player, obstacle) { | ||
if (playerControl && !ship.immunity) { | ||
if (player.x > obstacle.x + obstacle.width || | ||
player.x + player.width < obstacle.x || | ||
player.y > obstacle.y + obstacle.height || | ||
player.y + player.height < obstacle.y) { | ||
return false; | ||
} | ||
return true; //use "detectCollision" in an if statement | ||
} | ||
} | ||
|
||
function detectRangeCollision(player, obstacle) { | ||
if (playerControl && !ship.immunity) { | ||
if (player.x > obstacle.x + obstacle.width + tolerance || | ||
player.x + player.width + tolerance < obstacle.x || | ||
player.y > obstacle.y + obstacle.height + tolerance || | ||
player.y + player.height + tolerance < obstacle.y) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
} | ||
|
||
function detectAllCollisions() { | ||
if (detectCollision(ship, ast)) { | ||
generateAsteroid(ast); | ||
score += scoreAmt; | ||
detectLevelUp(); | ||
changeLevelUp(); | ||
playPopSound(); | ||
} | ||
if ((redAst.exist && detectCollision(ship, redAst))) { | ||
gameOver(); | ||
} | ||
if ((redAst2.exist && detectCollision(ship, redAst2))) { | ||
gameOver(); | ||
} | ||
if ((redAst3.exist && detectCollision(ship, redAst3))) { | ||
gameOver(); | ||
} | ||
if ((redAst4.exist && detectCollision(ship, redAst4))) { | ||
gameOver(); | ||
} | ||
if (cheese.exist && detectCollision(ship, cheese)) { | ||
slowDown = true; | ||
cheeseCDFrame = 1; | ||
givePlayerImmunity(300); | ||
score += 5; | ||
detectLevelUp(); | ||
changeLevelUp(); | ||
playPopSound(); | ||
|
||
cheese.x = Math.random() * (astRangeX) + padding; //regenerate cheese coordinates | ||
cheese.y = Math.random() * (astRangeY) + sBHeight + padding; | ||
startSD(); | ||
} | ||
} | ||
|
||
//slowDown effect | ||
function startSD() { | ||
cheese.exist = false; | ||
sDCount = 1; | ||
ship.speed = currentSpeed * (sDCount / 10); | ||
sDInterval = setInterval(sDCountr, 1000); | ||
} | ||
|
||
function sDCountr() { | ||
if (sDCount > 4) { | ||
ship.speed = currentSpeed; | ||
cheeseCDFrame = 4; | ||
slowDown = false; | ||
cheese.exist = true; | ||
clearInterval(sDInterval); | ||
playDingSound(); | ||
} | ||
else if (sDCount <= 6) { | ||
cheese.exist = false; | ||
sDCount += 2; | ||
ship.speed = currentSpeed * (sDCount / 10); | ||
cheeseCDFrame += 1; | ||
if (cheeseCDFrame > 4) { | ||
cheeseCDFrame = 1; | ||
} | ||
} | ||
} | ||
|
||
function detectRange(player, obstacle) { | ||
if (playerControl) { | ||
if (player.x > obstacle.x + obstacle.width + tolerance || //same collision code, but adding extra distance to create a range | ||
player.x + player.width + tolerance < obstacle.x || | ||
player.y > obstacle.y + obstacle.height + tolerance || | ||
player.y + player.height + tolerance < obstacle.y) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
} | ||
|
||
function detectBorderCollision(obstacle) { | ||
if (obstacle.x < 1 - obstacle.width) { | ||
obstacle.x = canvas.width; | ||
} | ||
if (obstacle.x > canvas.width) { | ||
obstacle.x = 1 - obstacle.width; | ||
} | ||
if (obstacle.y < 1 - obstacle.height) { | ||
obstacle.y = canvas.height; | ||
} | ||
if (obstacle.y > canvas.height) { | ||
obstacle.y = 1 - obstacle.height; | ||
} | ||
} | ||
|
||
function detectCheeseBorderCol(obstacle) { | ||
if (obstacle.x < 0 || | ||
obstacle.x > canvas.width || | ||
obstacle.y < sBHeight || | ||
obstacle.y > canvas.height) { | ||
obstacle.x = 460; | ||
obstacle.y = 310; | ||
} | ||
} |
Oops, something went wrong.