-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCharacter.js
More file actions
69 lines (56 loc) · 1.78 KB
/
Copy pathCharacter.js
File metadata and controls
69 lines (56 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import { getDiceRollArray, getDicePlaceholderHtml, getPercentace } from "./utilities.js";
class Character{
constructor(data) {
Object.assign(this, data)
//store the max health of the character
this.maxHealth = this.health;
//get placeholder dice roll
this.rollHtml = getDicePlaceholderHtml(this.rollCount);
}
//get Dice Roll method
setDiceRollHtml () {
this.currentRollScore = getDiceRollArray(this.rollCount);
this.rollHtml = this.currentRollScore.map( num =>
`<div class="dice">${ num }</div>`).join('');
};
//take damage method
takeDamage = attackScoreArray => {
const totalAttackScore = attackScoreArray.reduce((total, currentEl) => {
return total + currentEl;
})
//reducing character health
this.health -= totalAttackScore;
//sopping character health from going below 0
if(this.health <= 0){
this.health = 0;
this.dead = true;
console.log(this.dead)
}
}
//healthbar method
getHealthBarHtml() {
const percentage = Math.floor(getPercentace(this.maxHealth, this.health));
return `<div class="health-bar-outer">
<div class="health-bar-inner ${ percentage < 26 ? 'danger' : '' } "
style="width: ${percentage}%;">
</div>
</div>`
}
//character html method
getCharacterHtml(){
const { name, avatar, health, rollHtml } = this;
const healthBar = this.getHealthBarHtml();
return `
<div class="character-card">
<h4 class="name">${name}</h4>
<img class="avatar" src=${avatar} />
<p class="health">health: <b> ${health} </b></p>
${healthBar}
<div class="dice-container">
${rollHtml}
</div>
</div>
`
}
}
export default Character;