-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
129 lines (125 loc) · 3.73 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
function getRandomNumber(min, max) {
return Math.floor(Math.random() * (max, min) + min)
}
const app = Vue.createApp({
data() {
return {
playerHealth: 100,
monsterHealth: 100,
logMessages: [],
winner: null,
currentRound: 0,
}
},
watch: {
playerHealth(value) {
if(value <= 0 && this.monsterHealth <= 0) {
// a draw
this.winner = 'Draw'
} else if (value <= 0) {
// monster winner
this.winner = 'Monster'
}
},
monsterHealth(value) {
if(value <= 0 && this.playerHealth <= 0) {
// a draw
this.winner = 'Draw'
} else if (value <= 0) {
// player winner
this.winner = 'Player'
}
}
},
methods: {
monsterAttack() {
const attackValue = getRandomNumber(9, 16)
this.playerHealth -= attackValue
this.addLogBattles('Monster', 'attack and deals', attackValue)
},
playerAttack() {
this.currentRound++
const attackValue = getRandomNumber(7, 12)
this.monsterHealth -= attackValue
this.addLogBattles('Player', 'attack and deals', attackValue)
this.monsterAttack()
},
specialAttack() {
this.currentRound++
const attackValue = getRandomNumber(15, 20)
this.monsterHealth -= attackValue
this.addLogBattles('Player', 'special-attack', attackValue)
this.monsterAttack()
},
addLogBattles(who, what, value) {
this.logMessages.unshift({
actionBy: who,
actionType: what,
actionValue: value
})
},
healPlayer() {
this.currentRound++
const healValue = getRandomNumber(9, 18)
if (this.playerHealth + healValue > 100) {
this.playerHealth = 100
} else {
this.playerHealth += healValue
}
this.addLogBattles('Player', 'heals himself for', healValue)
this.monsterAttack()
},
newGame() {
this.playerHealth = 100
this.monsterHealth = 100
this.logMessages = []
this.winner = null
this.currentRound = 1
},
styleAction(action) {
if (action === 'Player') {
return { color: 'blueviolet' }
} else {
return { color: 'goldenrod' }
}
},
styleValue(type) {
if (type.includes("heals")) {
return { color: 'forestgreen' }
} else {
return { color: 'brown' }
}
},
surrender() {
this.winner = 'Monster'
this.playerHealth = 0
}
},
computed: {
updateMonsterBar() {
if (this.monsterHealth < 0) {
return { width: '0%' }
}
return { width: this.monsterHealth + '%' }
},
updatePlayerBar() {
if (this.playerHealth < 0) {
return { width: '0%' }
}
return { width: this.playerHealth + '%' }
},
availableSpecial() {
return this.currentRound % 3 !== 0
},
battleResult() {
if (this.winner === 'Player') {
return 'You won!'
} else if (this.winner === 'Monster') {
return 'You lost!'
} else {
return "It's a draw!"
}
},
}
})
app.mount('#game')