Skip to content

Commit

Permalink
Bug fixes: reload bug when player death (settimeout issue)
Browse files Browse the repository at this point in the history
  • Loading branch information
blueSparrow2000 committed Jan 3, 2024
1 parent 0f92011 commit 354d800
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 7 deletions.
11 changes: 7 additions & 4 deletions backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -718,10 +718,12 @@ io.on('connection', (socket) => {


const ENEMYSPAWNRATE = 1000

function spawnEnemies(){
enemyId++
const radius = 8 + Math.random() * 8
const speed = 1 + Math.random() * 2
const factor = 1 + Math.random() // 1~2
const radius = Math.round(factor*8) // 8~16
const speed = 3 - factor // 1~2
let x
let y

Expand All @@ -734,7 +736,8 @@ function spawnEnemies(){
}

// back ticks: ~ type this without shift!
const color = `hsl(${Math.random()*360},50%,50%)` // [0~360, saturation %, lightness %]
const colorfactor = 100 + Math.round(factor*60)
const color = `hsl(${colorfactor},50%,50%)` // [0~360, saturation %, lightness %]
const angle = Math.atan2(SCREENHEIGHT/2 - y, SCREENWIDTH/2 - x)
const velocity = {
x: Math.cos(angle)*speed,
Expand All @@ -743,7 +746,7 @@ function spawnEnemies(){

const damage = 1
const myID = enemyId
const health = 1 // default 1
const health = factor*2

// (new Enemy({ex, ey, eradius, ecolor, evelocity}))
backEndEnemies[enemyId] = {
Expand Down
2 changes: 2 additions & 0 deletions public/js/classes/Items.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ class Gun extends Item {
}

restock(playerId){
if (!frontEndPlayers[playerId]){return} // safe - player deleted white recharging may inccur an error

const ammoList = frontEndPlayers[playerId].ammoList
//console.log(ammoList)
const typetemp = this.ammotype
Expand Down
7 changes: 4 additions & 3 deletions public/js/eventListeners.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ function shootProj(event){
let currentHoldingItemId = frontEndPlayers[socket.id].inventory[inventoryPointer] // if it is 0, it is fist
let currentHoldingItem = frontEndItems[currentHoldingItemId]

if (!currentHoldingItem) {return} // undefined case

if ((currentHoldingItem.itemtype==='consumable')){ // eat
// dont need to check amount since we will delete item if eaten
Expand All @@ -40,14 +41,14 @@ function shootProj(event){

// decrease amount here (if needed in future)

fireTimeout = window.setTimeout(function(){socket.emit('consume',{
fireTimeout = window.setTimeout(function(){ if (frontEndPlayers[socket.id]){socket.emit('consume',{
itemName: currentHoldingItem.name,
playerId: socket.id,
healamount: currentHoldingItem.healamount,
deleteflag: true, // current version, delete right away
itemid: currentHoldingItemId,
currentSlot: frontEndPlayers[socket.id].currentSlot,
});
}) };
clearTimeout(fireTimeout);
listen = true},CONSUMERATE)

Expand Down Expand Up @@ -192,7 +193,7 @@ function reloadGun(){

reloadTimeout = window.setTimeout(function(){currentHoldingItem.restock(socket.id);
//console.log(`${currentGunName} ammo: ${currentHoldingItem.ammo}`);
clearTimeout(reloadTimeout);frontEndPlayers[socket.id].reloading = false;
clearTimeout(reloadTimeout); if (frontEndPlayers[socket.id]) {frontEndPlayers[socket.id].reloading = false};
listen = true}, GUNRELOADRATE)

}
Expand Down
1 change: 1 addition & 0 deletions public/js/frontend.js
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,7 @@ document.querySelector('#usernameForm').addEventListener('submit', (event) => {
// hide key info
//document.querySelector(`div[data-id="keyinfos"]`).style.display = 'none'
resetKeys()

socket.emit('initGame', {username: document.querySelector('#usernameInput').value, width: canvas.width, height: canvas.height})
})

Expand Down

0 comments on commit 354d800

Please sign in to comment.