Skip to content

Commit

Permalink
fix: Continuous powerUps kill.
Browse files Browse the repository at this point in the history
  • Loading branch information
GoldenThrust committed Jan 22, 2025
1 parent 5929314 commit d26ad7b
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 46 deletions.
9 changes: 7 additions & 2 deletions config/websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,11 +246,16 @@ class WebSocket {
socket.to(gameId).emit('shootWeapon', username, weaponId, weapon);
});

socket.on('powerUp', (player)=> {
socket.to(gameId).emit('powerUp', player.username);
});


socket.on('weaponHit', (shooter, shootee, gunIndex) => {
socket.to(gameId).emit('weaponHit', shooter, shootee, gunIndex);
});

socket.on('destroy', async (shooter, shootee, powerUps = false) => {
socket.on('destroy', async (shooter, shootee, powerUp = false) => {
try {
const match = await Match.findById(gameId).populate('stats.player');

Expand Down Expand Up @@ -284,7 +289,7 @@ class WebSocket {
}

await match.save();
socket.to(gameId).emit('destroy', shooter.username, shootee.username, powerUps);
socket.to(gameId).emit('destroy', shooter.username, shootee.username, powerUp);
} catch (error) {
console.error('Error handling destroy event:', error);
}
Expand Down
21 changes: 11 additions & 10 deletions logic/game/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import socket from "./websocket.js";
import axios from "axios";
import { initaudioCtx, playPowerUp, playRocketMove, updateListenerPosition } from "./utils/audio.js";

const scaleFactor = 0.8;
const scaleFactor = 0.7;
const radius = 20;
let lastUse = 30000;
let enableSpeciality = false;
Expand All @@ -24,7 +24,7 @@ if (!gameStarted) {

addEventListener("contextmenu", () => { });

export let cp = new Map();
export const cp = new Map();


ctx.imageSmoothingEnabled = true;
Expand Down Expand Up @@ -75,14 +75,12 @@ function main(t) {
const gameData = JSON.parse(sessionStorage.getItem(`gameData-${gameid}`));
const kills = sessionStorage.getItem(`kill-${gameid}`);


if (Math.floor(t / 1000) < 10 && !gameStarted) {
ctx.fillStyle = 'white';
ctx.font = '20px Arial';
const text = ctx.measureText('Game started in ' + (10 - Math.floor(t / 1000)) + 'seconds');
ctx.fillText('Game started in ' + (10 - Math.floor(t / 1000)) + ' seconds', (canvas.width / 2 - text.width / 2), canvas.height / 2);
} else if (Math.floor(t / 100) > 8 && Math.floor(t / 60000) < 15) {
player.speciality?.updatePowersUp()
if (t - lastUse > 30000) {
lastUse = t;
enableSpeciality = true;
Expand All @@ -95,8 +93,8 @@ function main(t) {
ctx.translate(-canvas.width / 2, -canvas.height / 2);
ctx.translate(-player.x + canvas.width / 2, -player.y + canvas.height / 2);
updateListenerPosition(player.x, player.y);


if (map) {
ctx.drawImage(
map, (-maxDistance.w), (-maxDistance.h), (maxDistance.w * 2), (maxDistance.h * 2)
Expand All @@ -105,24 +103,27 @@ function main(t) {
if (!map)
renderParticles();
player.draw(t);

player.speciality?.drawPowersUp();

cp.forEach((cplayer) => {
cplayer.speciality?.drawPowersUp();
cplayer.draw(t);
})

ctx.restore();

ctx.strokeStyle = 'grey';
ctx.fillStyle = '#111';
ctx.strokeRect(mapAR.x, mapAR.y, mapAR.width, mapAR.height)
ctx.fillRect(mapAR.x, mapAR.y, mapAR.width, mapAR.height)
cp.forEach((cplayer) => {
cplayer.speciality?.updatePowersUp();
playRocketMove(cplayer.audiopanner);
drawMiniMapPosition(cplayer, cplayer.team === 'neutral' || cplayer.team !== player.team ? 'red' : 'blue');
})
drawMiniMapPosition(player);
console.log
player.update(t);
player.speciality?.updatePowersUp();
// drawLive(player)


Expand Down
19 changes: 10 additions & 9 deletions logic/game/player/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@ import { maxDistance } from "../utils/constant.js";
import { convertTitleToCamelCase, getGameId, getRandomInt } from "../utils/function.js";
import SpriteAnimation from "../utils/spriteAnimation.js";
import axios from "axios";
import Speciality from "./weapons/Speciality.js";
import { createSpatialAudio } from "../utils/audio.js";

import Speciality from "./weapons/Speciality.js"

export default class Player {
constructor(x, y, angle, ctx, username = null, rocket = null, burst = null, speed = null, range = null, durability = null, fireRate = null, speciality) {
constructor(x, y, angle, ctx, username = null, rocket = null, burst = null, speed = null, range = null, durability = null, fireRate = null, speciality = null) {
this.maxSpeed = 10;
this.range = 5000;
this.durability = 1;
this.live = 1;
this.fireRate = 1000;
const gameData = JSON.parse(sessionStorage.getItem(`gameData-${getGameId()}`));
this.nitro = new Image();
this.player = new Image();
Expand All @@ -24,7 +28,7 @@ export default class Player {
this.durability = selectedRocket.durability;
this.live = selectedRocket.durability;
this.fireRate = selectedRocket.fireRate;
this.speciality = new Speciality(convertTitleToCamelCase(selectedRocket.speciality));
this.speciality = new Speciality(convertTitleToCamelCase(selectedRocket.speciality), username);
}).catch((error) => {
console.log(error);
});
Expand All @@ -38,12 +42,8 @@ export default class Player {
this.durability = durability;
this.live = durability;
this.fireRate = fireRate;
this.speciality = new Speciality(convertTitleToCamelCase(speciality));
this.speciality = new Speciality(convertTitleToCamelCase(speciality), username);
}




this.x = x;
this.y = y;
this.w = 35;
Expand All @@ -67,6 +67,7 @@ export default class Player {
this.audiopanner = createSpatialAudio(this.x, this.y, this.angle);
}


draw(t) {
this.ctx.save();

Expand Down
77 changes: 57 additions & 20 deletions logic/game/player/weapons/Speciality.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { cp } from "../../main.js";
import { ctx } from "../../utils/constant.js";
import { killPlayer } from "../../utils/function.js";
import SpriteAnimation from "../../utils/spriteAnimation.js";
import socket from "../../websocket.js";
import { player } from "../currentPlayer.js";

class Speciality {
constructor(type = 'sprayGun') {
constructor(type = 'sonicDash', username) {
this.type = type;
this.ctx = ctx;
this.speciality = {
Expand All @@ -16,14 +15,21 @@ class Speciality {
trackGun: this.#trackGun.bind(this),
sonicDash: this.#sonicDash.bind(this),
};
this.username = username;
this.powerUp = false;
console.log(username);
}

usePowersUp() {
if (this.speciality[this.type]) {
this.speciality[this.type]().initiate();
}
}
drawPowersUp() {
if (this.speciality[this.type] && this.powerUp) {
this.speciality[this.type]().draw();
}
}

updatePowersUp() {
if (this.speciality[this.type] && this.powerUp) {
Expand All @@ -43,6 +49,7 @@ class Speciality {
// }

return {
draw: () => { },
initiate: () => { },
update: () => { },
}
Expand All @@ -58,24 +65,15 @@ class Speciality {
// }
// }
return {
draw: () => { },
initiate: () => { },
update: () => { },
}
}

#blackHole() {
// const radius = 2000;
// for (const [username, cplayer] of cp.entries()) {
// const distance = Math.hypot(player.x - cplayer.x, player.y - cplayer.y);
// if (distance <= radius) {
// const dx = player.x - cplayer.x;
// const dy = player.y - cplayer.y;
// cplayer.x += dx * 0.1;
// cplayer.y += dy * 0.1;
// console.log(`${username} pulled towards black hole!`);
// }
// }
return {
draw: () => { },
initiate: () => { },
update: () => { },
}
Expand All @@ -95,31 +93,70 @@ class Speciality {
// bullets--;
// });
return {
draw: () => { },
initiate: () => { },
update: () => { },
update: () => {
// this.powerUp = true;
// const radius = 2000;
// for (const [username, cplayer] of cp.entries()) {
// const distance = Math.hypot(player.x - cplayer.x, player.y - cplayer.y);
// if (distance <= radius) {
// const dx = player.x - cplayer.x;
// const dy = player.y - cplayer.y;
// cplayer.x += dx * 0.1;
// cplayer.y += dy * 0.1;
// console.log(`${username} pulled towards black hole!`);
// }
// }

// setTimeout(() => {
// this.powerUp = false;
// console.log(`Black Hole ended for ${player.username}`);
// }, 10000);
},
}
}

#sonicDash() {
const spPlayer = this.username === player.username ? player : cp.get(this.username);
const radius = 200;
return {
draw: () => {
ctx.globalAlpha = 0.1;
ctx.fillStyle = 'red';
ctx.beginPath();
ctx.arc(spPlayer.x, spPlayer.y, radius, 0, Math.PI * 2);
ctx.fill();
ctx.closePath();
ctx.globalAlpha = 1;
},
initiate: () => {
player.maxSpeed *= 10;
this.powerUp = true;
if (player.username === spPlayer.username) {
socket.emit('powerUp', player);
}
spPlayer.maxSpeed *= 10;

setTimeout(() => {
player.maxSpeed /= 10;
spPlayer.maxSpeed /= 10;
this.powerUp = false
console.log(`Sonic Dash ended for ${player.username}`);
}, 10000);
},
update: () => {
const radius = 1000;
cp.forEach((cplayer) => {
const distance = Math.hypot(player.x - cplayer.x, player.y - cplayer.y);
const distance = Math.hypot(spPlayer.x - cplayer.x, spPlayer.y - cplayer.y);
if (distance <= radius && !cplayer.dead) {
killPlayer(cplayer, player, socket, true, true);
if (player.username === spPlayer.username)
killPlayer(cplayer, spPlayer, socket, true, true);
else
killPlayer(cplayer, spPlayer, null, true, false);
}
})
if (player.username !== spPlayer.username) {
const distance = Math.hypot(spPlayer.x - player.x, spPlayer.y - player.y);
if (distance <= radius && !player.dead)
killPlayer(player, spPlayer, null, true, false);
}
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion logic/game/utils/function.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,9 @@ export function killPlayer(cplayer, player, socket = null, powerUps = false, kil
}
playExplosion(cplayer.audiopanner)
logKill(cplayer, player);
if (socket)
if (socket) {
socket.emit('destroy', player, cplayer, true);
}
if (kill) {
const kills = sessionStorage.getItem(`kill-${getGameId()}`) || 0;
sessionStorage.setItem(`kill-${getGameId()}`, Number(kills) + 1);
Expand Down
11 changes: 8 additions & 3 deletions logic/game/websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,14 @@ socket.on('weaponHit', (shooter, shootee, gunIndex) => {
damageAnimation(shooteePlayer)
})

socket.on('destroy', (shooter, shootee, powerUps) => {
socket.on('powerUp', (username) => {
const player = cp.get(username);
player?.speciality?.usePowersUp();
});

socket.on('destroy', (shooter, shootee, powerUp) => {
if (powerUp) player.live = 0;
if (player.username === shootee) {
if (powerUps) player.live = 0;
killPlayer(player, { username: shooter });
}

Expand All @@ -68,7 +73,7 @@ socket.on('destroy', (shooter, shootee, powerUps) => {
}
})

socket.on('gameEnd', ()=> {
socket.on('gameEnd', () => {
window.location.href = `/game-end/${getGameId()}`;
})

Expand Down
2 changes: 1 addition & 1 deletion webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default {
path: path.resolve(__dirname, 'logic/'),
},
mode: 'production',
watch: false,
watch: true,
module: {
rules: [
{
Expand Down

0 comments on commit d26ad7b

Please sign in to comment.