Basically, I want to respond to multiple keys being pressed at the same time. I'm building a little game, where two fighters attack each other. When the user presses A on the keyboard, first fighter attacks, when the user presses J on the keyboard, the second fighter attacks. Then, I would like to have a combo attack, where when the user presses Q W E keys at the same time, It will deal extra damage, so how should I approach this problem ? What I have right now is this :
const getDamage = (attacker,defender) => {
document.addEventListener("keydown", (e)=>{
if(e.key == controls.playerOneAttack){
console.log("DEFENDER " + (defender.health = defender.health - ( getHitPower(attacker) - getBlockPower(defender))))
console.log(attacker.health);
}
else if(e.key == controls.playerTwoAttack){
console.log("ATTACKER " + (attacker.health = attacker.health - (getHitPower(defender) - getBlockPower(attacker))))
console.log(defender.health);
}
})
};
const getHitPower = (fighter) => {
const criticalHitChance = Math.floor(Math.random() * 2 + 1);
const power = fighter.attack * criticalHitChance;
return power
};
const getBlockPower = (fighter) => {
const dodgeChance = Math.floor(Math.random() * 2 + 1);
const power = fighter.defense * dodgeChance;
return power
};
I got controls coded in a different file and I'm just passing it in the if statement. Not asking to write a function for me, just asking how to build the if statement to respond to simultaneous key presses. Thank you in advance and sorry for the messy code, I'm a beginner :D