-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcharacter.js
74 lines (56 loc) · 2.4 KB
/
character.js
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
70
71
72
73
74
import {getDiceRollArray, getDicePlaceholderHtml, getPercentage} from "./utils.js"
class Character {
constructor(data) {
// everything in data object is assigned to new object
Object.assign(this, data)
// renders blank dice spots based on how many attacks character can make per turn
this.diceHtml = getDicePlaceholderHtml(this.diceCount)
// stores character's max health
this.maxHealth = this.health
// renders out the dice below character
this.setDiceHtml = diceCount => {
this.currentDiceScore = getDiceRollArray(this.diceCount)
this.diceHtml = this.currentDiceScore.map(num => {
return `<div class="dice">${num}</div>`
}).join('')
}
// renders characters
this.getCharacterHtml = data => {
// destructure "this" object so rest of function code is nice and clean
const { id, name, avatar, health, diceCount, currentDiceScore, diceHtml } = this
// declare diceHTML - later used to show multiple dice
let diceHTML = ""
const healthBar = this.getHealthBarHtml()
diceHTML = this.setDiceHtml(diceCount)
return `<div class="character-card">
<h4 class="name"> ${name} </h4>
<img class="avatar" src="${avatar}" />
<div class="health">health: <b> ${health} </b></div>
${healthBar}
<div class="dice-container">
${diceHtml}
</div>
</div>`
}
// function to calculate damage
this.takeDamage = attackScoreArray => {
const totalAttackScore = attackScoreArray.reduce((total, currentAttack) => total + currentAttack)
this.health -= totalAttackScore
// if health reaches zero, character declared dead
if (this.health <= 0) {
this.health = 0
this.dead = true
}
}
// render out health bar - red if 25%
this.getHealthBarHtml = function () {
const percent = getPercentage(this.health, this.maxHealth)
return `<div class="health-bar-outer">
<div class="health-bar-inner ${percent <= 25 ? `danger` : ``} "
style="width: ${percent}%;">
</div>
</div>`
}
}
}
export default Character