diff --git a/index.html b/index.html
index cf27922..3f98198 100644
--- a/index.html
+++ b/index.html
@@ -46,7 +46,7 @@
-
+
Score:💰00
@@ -95,10 +95,11 @@
width: 20vw;
"
>
-
Fuel
+
+ Fuel
+
0) {
+ const cannonBall1 = new Bullet({
+ position: {
+ x: this.x + this.width * Math.cos(this.alpha) - this.offset.x,
+ y: this.y + this.width * Math.sin(this.alpha),
+ },
+ velocity: this.velocity,
+ theta: this.alpha,
+ collisionBlocks: this.blocks,
+ zombies,
+ damage: this.damage,
+ player: this.player,
+ radius: 12,
+ color: "gray",
+ });
+ const cannonBall2 = new Bullet({
+ position: {
+ x: this.x + this.width * Math.cos(this.alpha) - this.offset.x,
+ y: this.y + this.width * Math.sin(this.alpha),
+ },
+ velocity: this.velocity,
+ theta: this.alpha + 0.05,
+ collisionBlocks: this.blocks,
+ zombies,
+ damage: this.damage,
+ player: this.player,
+ radius: 12,
+ color: "gray",
+ });
+ const cannonBall3 = new Bullet({
+ position: {
+ x: this.x + this.width * Math.cos(this.alpha) - 20,
+ y: this.y + this.width * Math.sin(this.alpha),
+ },
+ velocity: this.velocity,
+ theta: this.alpha - 0.05,
+ collisionBlocks: this.blocks,
+ zombies,
+ damage: this.damage,
+ player: this.player,
+ radius: 12,
+ color: "gray",
+ });
+ cannonBall1.isCannon = true;
+ cannonBall2.isCannon = true;
+ cannonBall3.isCannon = true;
+ playSound("cannon", 0.5);
+ this.addBullet(cannonBall1);
+ this.addBullet(cannonBall2);
+ this.addBullet(cannonBall3);
+ this.ammo -= 3;
+ }
+ }
+
+ startFiring() {
+ const intervalTime = 60000 / this.gunrate;
+
+ this.interval = setInterval(() => {
+ this.fireBullet();
+ }, intervalTime);
+ }
+
+ stopFiring() {
+ clearInterval(this.interval);
+ }
+
+ draw() {
+ c.save();
+ c.translate(this.x, this.y);
+ c.rotate(this.angle);
+ c.fillStyle = this.color;
+ c.fillRect(0, -this.height / 2, this.width, this.height);
+ c.restore();
+ }
+}
diff --git a/src/Player.js b/src/Player.js
index 2201e48..2ddc064 100644
--- a/src/Player.js
+++ b/src/Player.js
@@ -19,11 +19,10 @@ class Player {
this.lastInjured = 0;
this.score = 0;
- this.fuel = 100;
this.jetpackActive = false;
this.maxFuel = 100;
this.fuelUsage = 0.05;
- this.refuelRate = 0.015;
+ this.refuelRate = 0.002;
this.jetpackGravity = 0.01;
}
@@ -35,8 +34,8 @@ class Player {
update() {
// Jetpack logic
// jumpSt = 13
- if (this.jetpackActive && this.fuel > 0) {
- this.fuel -= this.fuelUsage;
+ if (this.jetpackActive && fuel > 0) {
+ fuel -= this.fuelUsage;
this.gravity = this.jetpackGravity;
this.jumpStrength = 3;
} else {
@@ -46,9 +45,8 @@ class Player {
}
// refuel logic
- if (!this.jetpackActive && this.fuel < this.maxFuel)
- this.fuel += this.refuelRate;
- Fmeter.value = this.fuel / 100;
+ if (!this.jetpackActive && fuel < this.maxFuel) fuel += this.refuelRate;
+ Fmeter.value = fuel / 100;
this.draw();
this.position.x += this.velocity.x;
@@ -59,8 +57,16 @@ class Player {
}
toggleJetpack() {
- if (this.fuel > 20) {
- this.jetpackActive = !this.jetpackActive;
+ if (fuel > 0) {
+ if (this.jetpackActive) {
+ this.jetpackActive = false;
+ jetpackSoundOff();
+ } else {
+ if (fuel > 20) {
+ this.jetpackActive = true;
+ jetpackSoundOn();
+ }
+ }
}
}
@@ -162,6 +168,8 @@ class Player {
) {
// console.log("health rem: " + this.health);
this.health -= 10;
+ if (randomIntFromRange(1, 10) % 2 === 0) playSound("Hurt", 0.5);
+ else playSound("Hurt2", 0.5);
if (this instanceof Player) {
Hmeter.value = this.health / 100;
if (this.health <= 0)
@@ -224,7 +232,15 @@ class Zombie extends Player {
}
player.health -= this.damage + 0.25 * zombies.length;
if (player instanceof Player) Hmeter.value = player.health / 100;
- if (player.health <= 0) return true;
+ if (player.health <= 0) {
+ if (player instanceof Environment) {
+ cannonLeft.ammo -= 6;
+ cannonRight.ammo -= 6;
+ console.log("left cannon ammo: " + cannonLeft.ammo);
+ console.log("right cannon ammo: " + cannonRight.ammo);
+ }
+ return true;
+ }
this.lastAttackTime = currentTime;
}
return false;
diff --git a/src/canvas.js b/src/canvas.js
index ab64918..10ffced 100644
--- a/src/canvas.js
+++ b/src/canvas.js
@@ -27,7 +27,7 @@ const platform = new Platform({
// creating guns
const M4A1 = new Gun({
ammo: 60,
- gunrate: 360,
+ gunrate: 731.71,
blocks: blocks,
damage: 14,
mag: 30,
@@ -51,7 +51,7 @@ const AWM = new Gun({
});
const AKM = new Gun({
ammo: 60,
- gunrate: 246,
+ gunrate: 500,
blocks: blocks,
damage: 17.5,
mag: 30,
@@ -62,20 +62,20 @@ const AKM = new Gun({
spread: 1.5 * (Math.PI / 180),
});
const DEagle = new Gun({
- ammo: 28,
- gunrate: 82,
+ ammo: 36,
+ gunrate: 375,
blocks: blocks,
- damage: 32,
- mag: 7,
+ damage: 40.25,
+ mag: 9,
name: "DEagle",
velocity: 5,
player: player,
- reloadTime: 2,
- spread: 1.2 * (Math.PI / 180),
+ reloadTime: 2.3,
+ spread: 2.5 * (Math.PI / 180),
});
const UZI = new Gun({
ammo: 66,
- gunrate: 778,
+ gunrate: 1578.95,
blocks: blocks,
damage: 7.5,
mag: 33,
@@ -91,6 +91,46 @@ guns.push(AKM);
guns.push(DEagle);
guns.push(UZI);
+const cannonLeft = new Cannon({
+ x: 576,
+ y: 525,
+ width: 40,
+ height: 70,
+ ammo: 45,
+ gunrate: 3,
+ velocity: 8,
+ blocks: blocks,
+ damage: 50,
+ reloadTime: 3,
+ name: "left",
+ player: player,
+ spread: 0,
+ direction: "left",
+ angle: 2.93,
+ alpha: -1.75,
+ offset: { x: 20, y: 0 },
+ color: "rgb(108, 120, 140)",
+});
+const cannonRight = new Cannon({
+ x: 733,
+ y: 533,
+ width: 40,
+ height: 70,
+ ammo: 45,
+ gunrate: 3,
+ velocity: 8,
+ blocks: blocks,
+ damage: 50,
+ reloadTime: 3,
+ name: "right",
+ player: player,
+ spread: 0,
+ direction: "right",
+ alpha: -1.3575,
+ angle: 3.42,
+ offset: { x: 0, y: 0 },
+ color: "rgb(108, 120, 140)",
+});
const bulletTrack = new Bullet({
position: {
x: player.position.x + player.width / 2,
@@ -206,6 +246,10 @@ function animate() {
platform.draw();
theta = calculateAngle(player);
currentGun.draw();
+ cannonLeft.draw();
+ cannonLeft.shoot();
+ cannonRight.draw();
+ cannonRight.shoot();
const trajectoryPoints = bulletTrack.calculateTrajectoryPoints(
{
x:
@@ -250,7 +294,7 @@ function animate() {
}
// adding fire effect if jetpack is active
- if(player.jetpackActive){
+ if (player.jetpackActive) {
updateParticles();
}
}
@@ -281,9 +325,32 @@ addEventListener("keydown", ({ key }) => {
break;
case "j":
player.toggleJetpack();
+ break;
+ case "s":
+ if (gamePaused) {
+ usePowerUp();
+ // reset the cannons
+ cannonLeft.startFiring();
+ setTimeout(() => {
+ cannonRight.startFiring();
+ }, 10000);
+ } else {
+ gamePaused = true;
+ cannonLeft.stopFiring();
+ cannonRight.stopFiring();
+ powerUpScr.showModal();
+ powerUpScr.style.display = "flex";
+ }
+
break;
case "Escape":
if (gamePaused) {
+ // reset the cannons
+ cannonLeft.startFiring();
+ setTimeout(() => {
+ cannonRight.startFiring();
+ }, 10000);
+
playSound("pause");
gamePaused = false;
animate();
@@ -293,6 +360,8 @@ addEventListener("keydown", ({ key }) => {
pauseScr.close();
pauseScr.style.display = "none";
} else {
+ cannonLeft.stopFiring();
+ cannonRight.stopFiring();
playSound("pause");
msg.textContent = 'Press "Esc" to resume';
gamePaused = true;
@@ -360,17 +429,6 @@ addEventListener("mouseup", () => {
currentGun.stopFiring();
});
-shop.addEventListener("click", () => {
- if (gamePaused) return;
- gamePaused = true;
- powerUpScr.showModal();
- powerUpScr.style.display = "flex";
-});
-
-closeBtn.onclick = () => {
- usePowerUp();
-};
-
document.querySelectorAll(".powerUp").forEach((btn, i) => {
btn.addEventListener("mouseenter", (e) => {
PUdesc.textContent = btn.getAttribute("data-tooltip");
@@ -456,3 +514,14 @@ document.querySelectorAll(".powerUp").forEach((btn, i) => {
}
});
});
+
+window.onload = () => {
+ pauseScr.style.display = "none";
+ powerUpScr.style.display = "none";
+ cannonLeft.startFiring();
+ setTimeout(() => {
+ cannonRight.startFiring();
+ }, 10000);
+ // PUmsg.style.display = "none";
+ startGame();
+};
diff --git a/src/utils.js b/src/utils.js
index 0842373..43d122c 100644
--- a/src/utils.js
+++ b/src/utils.js
@@ -13,6 +13,7 @@ let collisionDetected,
gamePaused = false,
time,
time2,
+ fuel = 100,
zombieInterval = null,
color = "white",
currentGun,
@@ -88,7 +89,7 @@ function calculateAngle() {
// Function to generate random positions within canvas boundaries
function getRandomPosition() {
const x = randomIntFromRange(1, 5) % 2 == 0 ? 10 : 1140;
- const y = 400;
+ const y = 100;
return { x, y };
}
@@ -157,6 +158,8 @@ function formatTimer(seconds) {
}
function GameOver(text) {
+ cannonLeft.stopFiring();
+ cannonRight.stopFiring();
msg.textContent = text;
gamePaused = true;
pauseScr.style.display = "flex";
@@ -194,8 +197,8 @@ function decreaseTimer() {
function startGame() {
zombieInterval = setInterval(() => {
- spawnZombies(4500); // Spawn a zombie every 5 seconds
- }, 4500);
+ spawnZombies(3500); // Spawn a zombie every 5 seconds
+ }, 3500);
gamePaused = false;
decreaseTimer();
}
@@ -204,13 +207,6 @@ restartBtn.onclick = () => {
window.location.reload();
};
-window.onload = () => {
- pauseScr.style.display = "none";
- powerUpScr.style.display = "none";
- // PUmsg.style.display = "none";
- // startGame();
-};
-
function usePowerUp() {
// updating health meter,ammo meter, score, etc
Hmeter.value = player.health / 100;
@@ -276,6 +272,9 @@ const sounds = {
Hurt2: new Audio("./sounds/Hurt2.mp3"),
pause: new Audio("./sounds/pause.wav"),
shop: new Audio("./sounds/shop.wav"),
+ cannon: new Audio("./sounds/cannon.wav"),
+ cannonHit: new Audio("./sounds/cannonHit.wav"),
+ empty: new Audio("./sounds/empty.wav"),
};
function playSound(sound, vol = 1) {
@@ -302,3 +301,13 @@ function stopRunSound() {
isRunning = false;
}
}
+
+const jetpack = new Audio("./sounds/jetpack.mp3");
+jetpack.loop = true; // Make the sound loop
+function jetpackSoundOn() {
+ jetpack.play();
+}
+function jetpackSoundOff() {
+ jetpack.pause();
+ jetpack.currentTime = 0;
+}
diff --git a/style.css b/style.css
index 2bca6ea..b9fb4cb 100644
--- a/style.css
+++ b/style.css
@@ -114,6 +114,7 @@ meter::-webkit-meter-even-less-good-value {
#timer3 {
border: 3px solid white;
background-color: rgb(67, 73, 87);
+ background-color: rgb(108, 120, 140);
width: 80px;
height: 80px;
flex-shrink: 0;
@@ -144,8 +145,9 @@ meter::-webkit-meter-even-less-good-value {
margin: 0;
}
#score{
- margin-top: 15px;
+ margin-top: 6px;
}
+
#PUmsg {
transition: cubic-bezier(0.075, 0.82, 0.165, 1);
z-index: 1;
@@ -170,9 +172,7 @@ button {
transition: 0.3s ease;
box-shadow: 1px 1px 3px black;
}
-#shop {
- margin-bottom: -10px;
-}
+
button:hover {
cursor: pointer;
transform: scale(1.15);