I am making progress with the Dungeon crawler challenge by writing a combat text app to start with. I figured out how to use modules properly to import game objects thus helping with code separation. My question is mainly around how to prevent player damage from doing more damage than intended when updating the each message state in a tick() function at 1 second intervals? For more info check out my Github repo:
AFAIK I should be getting the same damage value returned because both functions are identical except for the player and enemy objects (which do the same damage by the way) I’m passing in to the functions:
// Player.js
const player = {
classname: 'dragonknight',
first: 'Jorvund',
last: 'Greymane',
skill: 'flame',
health: 100,
xp: 0,
str: 2,
dex: 1,
mag: 1,
armour: {
rating: 0
},
weapon: {
name: 'greatsword',
damage: 10
},
power: {
name: 'searing strike',
damage: 20
}
};
export default player;
// App.js
playerDealsDamage(pc, npc) {
let amount = pc.weapon.damage;
if(this.player.health <= 0) {
return this.getFullName(pc) + ' is dead';
} else if (pc.health > 0){
this.enemy.health -= amount;
return 'total damage : ' + amount.toString();
} else {
return this.kill(npc);
}
}
enemyDealsDamage(pc, npc){
let amount = npc.weapon.damage;
if(this.enemy.health <= 0) {
return this.getFullName(npc) + ' is dead';
} else if (pc.health > 0){
this.player.health -= amount;
return 'total damage: ' + amount.toString();
} else {
return this.kill(pc);
}
}