In my code, I currently have an
engage(ability: number, opponent: Creature)
function that is responsible for executing one of three different attacks based on the ability chosen.
strike(opponent: Creature){}
claw(opponent: Creature){}
fireball(opponent: Creature){}
The issue I am facing is that the consequences of each attack method (such as the opponent dying) are repeated in my current implementation.
if(ability == 1){
claw(opponent);
*
*
lines detailing claw effects
*
*
*
}
else if(ability == 2){
*
*
*
}
else {
*
*
*
}
I am curious about the possibility of creating an array called attack_methods[3]
which would simplify the process to define and execute attacks like this:
attack_methods[1] = strike(opponent: Creature){};
attack_methods[2] = claw(opponent: Creature){};
attack_methods[3] = fireball(opponent: Creature){};
and then call them using the following format:
engage(ability: number, opponent: Creature, attack_methods[]: DivineKnowledge){
attack_methods[ability](Creature);
*
*
*
implications of the chosen ability
*
*
*
}
I am interested in understanding the correct way to implement this feature as I transition from programming in C to learning about TypeScript.