-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
143 lines (126 loc) · 3.68 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
function getRandomValue(min, max) {
return Math.floor(Math.random() * (max - min)) + min;
};
const app = Vue.createApp({
data() {
return {
playerHealth: 100,
monsterHealth: 100,
currentRound: 0,
winner: null,
logMessages: [],
}
},
watch: {
playerHealth: function(newValue) {
if (newValue <= 0 && this.monsterHealth <= 0) {
this.winner = 'draw';
}
else if (newValue <= 0) {
this.winner = 'monster';
}
else if (this.monsterHealth <= 0) {
this.winner = 'player';
}
}
},
computed: {
monsterHealthBar() {
if (this.monsterHealth <= 0) {
return {
width: 0,
}
}
else{
return { width: this.monsterHealth + '%',
}
}
},
playerHealthBar() {
if(this.playerHealth <= 0) {
return {
width: 0,
}
}
else{
return{ width: this.playerHealth + '%',
}
}
},
mayUseSpecialAttack() {
return this.currentRound % 3 !== 0;
},
},
methods: {
attackMonster() {
this.currentRound++;
const damage = getRandomValue(5, 12);
if (this.monsterHealth - damage < 0) {
this.monsterHealth = 0;
this.attackPlayer();
} else {
this.monsterHealth -= damage;
this.attackPlayer();
}
this.addLogMesssages('player', 'hit the monster for ', damage + ' damage.');
},
attackPlayer() {
const damage = getRandomValue(8, 15);
if (this.playerHealth - damage < 0) {
this.playerHealth = 0;
} else {
this.playerHealth -= damage;
}
this.addLogMesssages('monster', 'hit the player for ', damage + ' damage.');
},
specialAttack() {
this.currentRound++;
const damage = getRandomValue(10, 25);
this.monsterHealth -= damage;
this.attackPlayer();
this.addLogMesssages('player', 'use Special attack of ', damage + ' damage.');
},
healPlayer() {
this.currentRound++;
const heal = getRandomValue(10, 20);
if (this.playerHealth + heal > 100) {
this.playerHealth = 100;
this.attackPlayer() ;
}
else{
this.playerHealth += heal;
this.attackPlayer() ;
}
this.addLogMesssages('player', 'heal for ', heal);
},
healMonster() {
const heal = getRandomValue(10, 20);
if (this.monsterHealth + heal > 100) {
this.monsterHealth = 100;
}
else{
this.monsterHealth += heal;
}
this.addLogMesssages('monster', 'heal for ', heal);
},
giveUp() {
this.playerHealth = 0;
this.winner = 'monster';
},
startnew() {
this.playerHealth = 100;
this.monsterHealth = 100;
this.winner = null;
this.currentRound= 0;
this.logMessages= [];
},
addLogMesssages(who , what , value) {
this.logMessages.push({
actionBy: who,
actionType: what,
actionValue: value,
});
},
}
});
app.mount('#game');