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, |
4a01d9
|
12 |
}; |
CM |
13 |
}, |
8c672a
|
14 |
computed: { |
5529c7
|
15 |
monsterBarStyle() { |
08ed40
|
16 |
if (this.monsterHealth < 0) { |
CM |
17 |
return { width: "0%" }; |
|
18 |
} |
5529c7
|
19 |
return { width: this.monsterHealth + "%" }; |
8c672a
|
20 |
}, |
5529c7
|
21 |
playerBarStyle() { |
08ed40
|
22 |
if (this.playerHealth < 0) { |
CM |
23 |
return { width: "0%" }; |
|
24 |
} |
5529c7
|
25 |
return { width: this.playerHealth + "%" }; |
CM |
26 |
}, |
|
27 |
mayUseSpecialAttack() { |
|
28 |
return this.currentRound % 3 !== 0; |
8c672a
|
29 |
}, |
CM |
30 |
}, |
807972
|
31 |
watch: { |
08ed40
|
32 |
playerHealth(value) { |
807972
|
33 |
if (value <= 0 && this.monsterHealth <= 0) { |
CM |
34 |
this.winner = "draw"; |
|
35 |
} else if (value <= 0) { |
08ed40
|
36 |
this.winner = "monster"; |
807972
|
37 |
} |
CM |
38 |
}, |
|
39 |
monsterHealth(value) { |
08ed40
|
40 |
if (value <= 0 && this.playerHealth <= 0) { |
807972
|
41 |
this.winner = "draw"; |
CM |
42 |
} else if (value <= 0) { |
|
43 |
this.winner = "player"; |
|
44 |
} |
|
45 |
}, |
|
46 |
}, |
4a01d9
|
47 |
methods: { |
08ed40
|
48 |
startGame() { |
CM |
49 |
this.playerHealth = 100; |
|
50 |
this.monsterHealth = 100; |
|
51 |
this.currentRound = 0; |
|
52 |
this.winner = null; |
|
53 |
}, |
4a01d9
|
54 |
attackMonster() { |
5529c7
|
55 |
this.currentRound++; |
CM |
56 |
const attackValue = getRandomValue(5, 12); |
4a01d9
|
57 |
this.monsterHealth -= attackValue; |
CM |
58 |
this.attackPlayer(); |
14370b
|
59 |
}, |
5529c7
|
60 |
attackPlayer() { |
CM |
61 |
const attackValue = getRandomValue(8, 15); |
|
62 |
this.playerHealth -= attackValue; |
|
63 |
}, |
|
64 |
specialAttackMonster() { |
|
65 |
this.currentRound++; |
|
66 |
const attackValue = getRandomValue(10, 20); |
|
67 |
this.monsterHealth -= attackValue; |
|
68 |
this.attackPlayer(); |
4a01d9
|
69 |
}, |
02ffa3
|
70 |
healPlayer() { |
CM |
71 |
this.currentRound++; |
|
72 |
const healValue = getRandomValue(8, 20); |
|
73 |
if (this.playerHealth + healValue > 100) { |
|
74 |
this.playerHealth = 100; |
|
75 |
} else { |
|
76 |
this.playerHealth += healValue; |
|
77 |
} |
|
78 |
this.attackPlayer(); |
|
79 |
}, |
08ed40
|
80 |
surrender(){ |
CM |
81 |
this.winner = 'monster'; |
|
82 |
} |
4a01d9
|
83 |
}, |
CM |
84 |
}); |
|
85 |
|
|
86 |
app.mount("#game"); |