commit | author | age
|
5529c7
|
1 |
function getRandomValue(min, max) { |
CM |
2 |
return Math.floor(Math.random() * (max - min)) + min; |
4a01d9
|
3 |
} |
CM |
4 |
|
14370b
|
5 |
const app = Vue.createApp({ |
4a01d9
|
6 |
data() { |
CM |
7 |
return { |
|
8 |
playerHealth: 100, |
|
9 |
monsterHealth: 100, |
5529c7
|
10 |
currentRound: 0, |
807972
|
11 |
winner: null, |
efa2e3
|
12 |
logMessages: [], |
4a01d9
|
13 |
}; |
CM |
14 |
}, |
8c672a
|
15 |
computed: { |
5529c7
|
16 |
monsterBarStyle() { |
08ed40
|
17 |
if (this.monsterHealth < 0) { |
CM |
18 |
return { width: "0%" }; |
|
19 |
} |
5529c7
|
20 |
return { width: this.monsterHealth + "%" }; |
8c672a
|
21 |
}, |
5529c7
|
22 |
playerBarStyle() { |
08ed40
|
23 |
if (this.playerHealth < 0) { |
CM |
24 |
return { width: "0%" }; |
|
25 |
} |
5529c7
|
26 |
return { width: this.playerHealth + "%" }; |
CM |
27 |
}, |
|
28 |
mayUseSpecialAttack() { |
|
29 |
return this.currentRound % 3 !== 0; |
8c672a
|
30 |
}, |
CM |
31 |
}, |
807972
|
32 |
watch: { |
08ed40
|
33 |
playerHealth(value) { |
807972
|
34 |
if (value <= 0 && this.monsterHealth <= 0) { |
CM |
35 |
this.winner = "draw"; |
|
36 |
} else if (value <= 0) { |
08ed40
|
37 |
this.winner = "monster"; |
807972
|
38 |
} |
CM |
39 |
}, |
|
40 |
monsterHealth(value) { |
08ed40
|
41 |
if (value <= 0 && this.playerHealth <= 0) { |
807972
|
42 |
this.winner = "draw"; |
CM |
43 |
} else if (value <= 0) { |
|
44 |
this.winner = "player"; |
|
45 |
} |
|
46 |
}, |
|
47 |
}, |
4a01d9
|
48 |
methods: { |
08ed40
|
49 |
startGame() { |
CM |
50 |
this.playerHealth = 100; |
|
51 |
this.monsterHealth = 100; |
|
52 |
this.currentRound = 0; |
|
53 |
this.winner = null; |
efa2e3
|
54 |
this.logMessages = []; |
08ed40
|
55 |
}, |
4a01d9
|
56 |
attackMonster() { |
5529c7
|
57 |
this.currentRound++; |
CM |
58 |
const attackValue = getRandomValue(5, 12); |
4a01d9
|
59 |
this.monsterHealth -= attackValue; |
efa2e3
|
60 |
this.addLogMessage('player', 'attack', attackValue); |
4a01d9
|
61 |
this.attackPlayer(); |
14370b
|
62 |
}, |
5529c7
|
63 |
attackPlayer() { |
CM |
64 |
const attackValue = getRandomValue(8, 15); |
|
65 |
this.playerHealth -= attackValue; |
efa2e3
|
66 |
this.addLogMessage('monster', 'attack', attackValue); |
5529c7
|
67 |
}, |
CM |
68 |
specialAttackMonster() { |
|
69 |
this.currentRound++; |
|
70 |
const attackValue = getRandomValue(10, 20); |
|
71 |
this.monsterHealth -= attackValue; |
efa2e3
|
72 |
this.addLogMessage('player', 'attack Sp', attackValue); |
5529c7
|
73 |
this.attackPlayer(); |
4a01d9
|
74 |
}, |
02ffa3
|
75 |
healPlayer() { |
CM |
76 |
this.currentRound++; |
|
77 |
const healValue = getRandomValue(8, 20); |
|
78 |
if (this.playerHealth + healValue > 100) { |
|
79 |
this.playerHealth = 100; |
|
80 |
} else { |
|
81 |
this.playerHealth += healValue; |
|
82 |
} |
efa2e3
|
83 |
this.addLogMessage('player', 'heal', healValue); |
02ffa3
|
84 |
this.attackPlayer(); |
CM |
85 |
}, |
efa2e3
|
86 |
surrender() { |
CM |
87 |
this.winner = "monster"; |
|
88 |
}, |
|
89 |
addLogMessage(who, whath, value) { |
|
90 |
this.logMessages.unshift({ |
|
91 |
actionBy: who, |
|
92 |
actionType: whath, |
|
93 |
actionValue: value |
|
94 |
}); |
|
95 |
}, |
4a01d9
|
96 |
}, |
CM |
97 |
}); |
|
98 |
|
|
99 |
app.mount("#game"); |