-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
95 lines (92 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
const getRandomVal = ( min, max ) => {
return Math.floor(Math.random() * (max - min)) + min;
}
const app = Vue.createApp({
data () {
return {
playerHP: 200,
monsterHP: 200,
currentRound: 0,
winner: null,
battleLog: []
}
},
methods: {
startGame () {
this.playerHP = 200;
this.monsterHP = 200;
this.currentRound = 0;
this.winner = null;
},
attackMonster () {
this.currentRound++
const attackPoints = getRandomVal(5, 12);
this.monsterHP -= attackPoints;
this.addBattleLog("player", "attack", attackPoints);
this.attackPlayer();
},
attackPlayer () {
const attackPoints = getRandomVal(8, 15);
this.playerHP -= attackPoints;
this.addBattleLog("monster", "attack", attackPoints);
},
specialAttackMonster () {
this.currentRound++
const attackPoints = getRandomVal(10, 25);
this.monsterHP -= attackPoints;
this.addBattleLog("player", "special-attack", attackPoints);
this.attackPlayer();
},
healPlayer () {
this.currentRound++;
const healValue = getRandomVal(8, 20)
if (this.playerHP + healValue > 200) {
this.playerHP = 200;
} else {
this.playerHP += healValue;
}
this.addBattleLog("player", "heal", healValue);
this.attackPlayer();
},
playerSurrender () {
this.winner = "monster";
},
addBattleLog ( agent, action, actionPoints ) {
this.battleLog.unshift({ agent, action, actionPoints });
}
},
computed: {
monsterHealthBarStyle () {
if (this.monsterHP < 0) {
return { width: "0%" }
}
return { width: (this.monsterHP / 2) + "%" };
},
playerHealthBarStyle () {
if (this.playerHP < 0) {
return { width: "0%" };
}
return { width: (this.playerHP / 2) + "%" };
},
specialAttackAvailable () {
return this.currentRound % 3 !== 0 || this.winner
},
},
watch: {
playerHP (value) {
if (value <= 0 && this.monsterHP <= 0) {
this.winner = "draw"
} else if (value <= 0 ) {
this.winner = "monster"
}
},
monsterHP (value) {
if (value <= 0 && this.playerHP <= 0) {
this.winner = "draw"
} else if (value <= 0) {
this.winner = "player"
}
}
}
})
app.mount("#game")