Skip to content

Commit

Permalink
Sound update: optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
blueSparrow2000 committed Jan 5, 2024
1 parent fa381b3 commit 8fcb2fe
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 27 deletions.
12 changes: 6 additions & 6 deletions backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const PLAYERRADIUS = 10 //16
const PLAYERSPEED = TICKRATE/5 // pixel
const PLAYERHEALTH = 3
const PLAYERHEALTHMAX = 6
const GUNHEARRANGE = 500
const GUNHEARRANGE = 700
const PLAYER_JOIN_DELAY = 3000

//to check if there exists any player left
Expand Down Expand Up @@ -42,10 +42,10 @@ const gunInfo = {
'railgun':{travelDistance:0, damage: 3, shake:0, num: 1, fireRate: 1000, projectileSpeed:0, magSize:2, reloadTime: 1800, ammotype:'battery', size: {length:50, width:5}}, // pierce walls and entities
'CrossBow':{travelDistance:650, damage: 15, shake:0, num: 1, fireRate: 100, projectileSpeed:10, magSize: 1, reloadTime: 1300, ammotype:'bolt', size: {length:21, width:2}},

'M1':{travelDistance:2400, damage: 6, shake:0, num: 1, fireRate: 1600, projectileSpeed:42, magSize: 5, reloadTime: 4000, ammotype:'7mm', size: {length:42, width:4}},
'mk14':{travelDistance:1600, damage: 3, shake:1, num: 1, fireRate: 600, projectileSpeed:32, magSize:14, reloadTime: 3300, ammotype:'7mm', size: {length:32, width:3} },
'SLR':{travelDistance:2000, damage: 3.5, shake:1, num: 1, fireRate: 350, projectileSpeed:36, magSize: 10, reloadTime: 2700, ammotype:'7mm', size: {length:38, width:3}},
'AWM':{travelDistance:3000, damage: 12, shake:0, num: 1, fireRate: 2000, projectileSpeed:30, magSize: 7, reloadTime: 4000, ammotype:'7mm', size: {length:50, width:5}},
'M1':{travelDistance:2000, damage: 6, shake:0, num: 1, fireRate: 1600, projectileSpeed:42, magSize: 5, reloadTime: 4000, ammotype:'7mm', size: {length:42, width:4}},
'mk14':{travelDistance:1000, damage: 3, shake:1, num: 1, fireRate: 600, projectileSpeed:32, magSize:14, reloadTime: 3300, ammotype:'7mm', size: {length:32, width:3} },
'SLR':{travelDistance:1200, damage: 3.5, shake:1, num: 1, fireRate: 350, projectileSpeed:36, magSize: 10, reloadTime: 2700, ammotype:'7mm', size: {length:38, width:3}},
'AWM':{travelDistance:2400, damage: 12, shake:0, num: 1, fireRate: 2000, projectileSpeed:30, magSize: 7, reloadTime: 4000, ammotype:'7mm', size: {length:50, width:5}},

'pistol':{travelDistance:400, damage: 1, shake:3, num: 1, fireRate: 300, projectileSpeed:20, magSize:15, reloadTime: 1100, ammotype:'5mm', size: {length:17, width:2}},
'M249':{travelDistance:800, damage: 1, shake:1, num: 1, fireRate: 75, projectileSpeed:28, magSize:150, reloadTime: 7400, ammotype:'5mm', size: {length:28, width:6}},
Expand Down Expand Up @@ -495,7 +495,7 @@ io.on('connection', (socket) => {
console.log('a user connected');

// give server info to a frontend
socket.emit('serverVars', {gunInfo, ammoInfo, PLAYERSPEED})
socket.emit('serverVars', {gunInfo, ammoInfo, consumableInfo, PLAYERSPEED})

// projectile spawn
socket.on('shoot',({x,y,angle, mousePos, currentGun,playerIdEXACT}) => {
Expand Down
Binary file added gun informations.xlsx
Binary file not shown.
2 changes: 1 addition & 1 deletion public/js/classes/Player.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class Player {
c.lineWidth = bodywidth
c.stroke()

} else if(thisguninfo.travelDistance >= 1200){ // snipters except VSS (can shoot all across the screen)
} else if(thisguninfo.projectileSpeed >= 30){ // snipters except VSS (can shoot all across the screen)
const tipsize = 3
const tipstart = itemlength- tipsize
const tipwidth = itemSize.width + thisguninfo.damage/3
Expand Down
10 changes: 5 additions & 5 deletions public/js/eventListeners.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ function shootProj(event){
if (!listen) {return} // not ready to eat
listen = false // block

let consumeSound = new Audio(`/consumeSound/${currentItemName}.mp3`)
consumeSound.play()
const consumeSound = frontEndConsumableSounds[currentItemName]// new Audio(`/consumeSound/${currentItemName}.mp3`)
consumeSound.cloneNode(true).play()

// decrease amount here (if needed in future)

Expand Down Expand Up @@ -181,8 +181,8 @@ function reloadGun(){
listen = false // block
//console.log("reloading!")

let gunSound = new Audio(`/reloadSound/${currentGunName}.mp3`)
gunSound.play()
let reloadSound = frontEndGunReloadSounds[currentGunName] //new Audio(`/reloadSound/${currentGunName}.mp3`)
reloadSound.cloneNode(true).play()
// reload ammo here!!!!!

frontEndPlayer.reloading = true
Expand Down Expand Up @@ -247,7 +247,7 @@ function interactItem(itemId,backEndItems){
listen = false
//console.log("interacting!")

let interactSound = new Audio("/sound/interact.mp3")

interactSound.play()

// interact here!
Expand Down
55 changes: 40 additions & 15 deletions public/js/frontend.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,17 @@ const TICKRATE = 15
const gunInfoFrontEnd = {}
let gunInfoKeysFrontEnd = []

let frontEndGunSounds = {}
let frontEndGunReloadSounds = {}

const ammoInfoFrontEnd = {}
let ammoInfoKeysFrontEnd = []
const interactSound = new Audio("/sound/interact.mp3")

let frontEndConsumableSounds = {}
let consumableInfoKeysFrontEnd = []



const SCREENWIDTH = 1024//1920//
const SCREENHEIGHT = 576//1080//
Expand All @@ -23,24 +32,43 @@ let listen = true // very important for event listener


// initialize server variables
socket.on('serverVars',( {gunInfo, ammoInfo, PLAYERSPEED})=>{
socket.on('serverVars',( {gunInfo, ammoInfo, consumableInfo, PLAYERSPEED})=>{
PLAYERSPEEDFRONTEND = PLAYERSPEED

// ammo infos
ammoInfoKeysFrontEnd = Object.keys(ammoInfo)
for (let i=0;i<ammoInfoKeysFrontEnd.length;i++){
const ammokey = ammoInfoKeysFrontEnd[i]
ammoInfoFrontEnd[ammokey] = ammoInfo[ammokey]
}
//console.log(ammoInfoFrontEnd)

// gun infos
gunInfoKeysFrontEnd = Object.keys(gunInfo)
//console.log(gunInfoKeysFrontEnd[0])
for (let i=0;i<gunInfoKeysFrontEnd.length;i++){
const gunkey = gunInfoKeysFrontEnd[i]
gunInfoFrontEnd[gunkey] = gunInfo[gunkey]
//console.log(gunInfoFrontEnd)

// load sounds
frontEndGunSounds[gunkey] = new Audio(`/sound/${gunkey}.mp3`)
if (gunkey !== 'fist' && gunkey !== 'knife' && gunkey !== 'bat'){ // these three dont have reload sounds
frontEndGunReloadSounds[gunkey] = new Audio(`/reloadSound/${gunkey}.mp3`)
}
}

// consumable infos
consumableInfoKeysFrontEnd = Object.keys(consumableInfo)
for (let i=0;i<consumableInfoKeysFrontEnd.length;i++){
const conskey = consumableInfoKeysFrontEnd[i]
gunInfoFrontEnd[conskey] = consumableInfo[conskey]

// load sounds
frontEndConsumableSounds[conskey] = new Audio(`/consumeSound/${conskey}.mp3`)
}




//console.log("front end got the variables from the server")
})

Expand Down Expand Up @@ -232,17 +260,14 @@ socket.on('updateFrontEnd',({backEndPlayers, backEndEnemies, backEndProjectiles,
const me = frontEndPlayers[myPlayerID]
if (me){

let gunSound = new Audio(`/sound/${gunName}.mp3`)
const DISTANCE = Math.hypot(backEndProjectile.x - me.x, backEndProjectile.y - me.y)
let soundhearrange = (gunInfoFrontEnd[backEndProjectile.gunName].travelDistance/4) * 3 + 100
//console.log(soundhearrange)
if (gunName==='VSS'){ // surpressed
soundhearrange = 400
}
if (DISTANCE < soundhearrange) {
if (gunName){
gunSound.play()
const thatGunSoundDistance = gunInfoFrontEnd[gunName].projectileSpeed * 20
if (gunName && (DISTANCE-100 < thatGunSoundDistance) ){
let gunSound = frontEndGunSounds[gunName] //new Audio(`/sound/${gunName}.mp3`)
if (DISTANCE > 100){
gunSound.volume = Math.round( 10*(thatGunSoundDistance - (DISTANCE-100))/thatGunSoundDistance ) / 10
}
gunSound.cloneNode(true).play()
}
}

Expand All @@ -251,7 +276,7 @@ socket.on('updateFrontEnd',({backEndPlayers, backEndEnemies, backEndProjectiles,
gsap.to(frontEndProjectiles[id], {
x: backEndProjectile.x + backEndProjectile.velocity.x,
y: backEndProjectile.y + backEndProjectile.velocity.y,
duration: 0.015,
duration: 0.015, // tick
ease: 'linear'
})
}
Expand All @@ -275,8 +300,8 @@ socket.on('updateFrontEnd',({backEndPlayers, backEndEnemies, backEndProjectiles,
if (me){
const DISTANCE = Math.hypot(backendDrawable.start.x - me.x, backendDrawable.start.y - me.y)
if (DISTANCE < GUNHEARRANGE) {
let gunSound = new Audio('/sound/railgun.mp3')
gunSound.play()
let gunSound = frontEndGunSounds['railgun']// new Audio('/sound/railgun.mp3')
gunSound.cloneNode(true).play()
}
}

Expand Down

0 comments on commit 8fcb2fe

Please sign in to comment.