-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
119 lines (113 loc) · 2.77 KB
/
app.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
new Vue({
el: '#app',
data: {
playerHealth: 100,
monsterHealth: 100,
gameIsRunning: false,
damageByMonster: -1,
damageByPlayer: -1,
logs: [],
},
methods: {
attack() {
this.generateMstTurn();
this.damageByPlayer = this.calculateBasicDamage();
this.monsterHealth -= this.damageByPlayer;
this.playerHealth -= this.damageByMonster;
this.enterLog('attack');
},
specialAttack() {
this.generateMstTurn();
this.damageByPlayer = this.calculateSpecialDamage();
this.monsterHealth -= this.damageByPlayer;
this.playerHealth -= this.damageByMonster;
this.enterLog('attack');
},
heal() {
this.generateMstTurn();
let healPoints = this.calculateHealing();
this.playerHealth += healPoints;
this.playerHealth -= this.damageByMonster;
this.enterLog('heal', healPoints);
},
calculateBasicDamage() {
return Math.floor(Math.random() * (8 - 4)) + 3;
},
calculateHealing() {
return Math.floor(Math.random() * (5 - 2)) + 1;
},
calculateSpecialDamage() {
return Math.floor(Math.random() * (8 - 2)) + 3;
},
reset() {
this.playerHealth = 100;
this.monsterHealth = 100;
this.gameIsRunning = false;
this.logs = [];
},
generateMstTurn() {
switch (Math.round(Math.random())) {
case 0:
this.damageByMonster = this.calculateBasicDamage();
break;
case 1:
this.damageByMonster = this.calculateSpecialDamage();
break;
}
},
enterLog(action, healPoints) {
switch (action) {
case 'attack':
this.logs.push(
{
text: `PLAYER HITS MONSTER BY ${this.damageByPlayer}`,
class: 'player-turn',
},
{
text: `MONSTER HITS PLAYER BY ${this.damageByMonster}`,
class: 'monster-turn',
}
);
break;
case 'heal':
this.logs.push(
{
text: `PLAYER HEALED BY ${healPoints}`,
class: 'player-turn',
},
{
text: `MONSTER HITS PLAYER BY ${this.damageByMonster}`,
class: 'monster-turn',
}
);
break;
}
},
},
computed: {
playerHealthSize() {
return {
width: this.playerHealth * 3.76 + 'px',
};
},
monsterHealthSize() {
return {
width: this.monsterHealth * 3.76 + 'px',
};
},
},
watch: {
playerHealth() {
if (this.playerHealth <= 0) {
alert('You lose!');
this.reset();
}
},
monsterHealth() {
if (this.monsterHealth <= 0) {
alert('You win!!');
this.reset();
}
},
},
});