-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
102 lines (101 loc) · 2.53 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
const getRandomValue = (min, max) => Math.floor(Math.random() * (max - min) + min);
const app = Vue.createApp({
data() {
return {
playerHealth: 100,
enemyHealth: 100,
currentRound: 0,
winner: null,
logMessages: [],
};
},
computed: {
enemyBarStyles() {
if (this.enemyHealth < 0) {
return { width: "0%" };
}
return { width: this.enemyHealth + "%" };
},
playerBarStyles() {
if (this.playerHealth < 0) {
return { width: "0%" };
}
return { width: this.playerHealth + "%" };
},
mayUseUltimate() {
return this.currentRound % 3 !== 0;
},
},
watch: {
playerHealth(value) {
if (value <= 0 && this.monsterHealth <= 0) {
// A draw
this.winner = "draw";
} else if (value <= 0) {
// Player lost
this.winner = "enemy";
}
},
enemyHealth(value) {
if (value <= 0 && this.playerHealth <= 0) {
// A draw
this.winner = "draw";
} else if (value <= 0) {
// Enemy lost
this.winner = "player";
}
},
},
methods: {
startNewGame() {
this.playerHealth = 100;
this.enemyHealth = 100;
this.winner = null;
this.currentRound = 0;
this.logMessages = [];
},
attackEnemy() {
this.currentRound++;
const attackValue = getRandomValue(5, 12);
this.enemyHealth -= attackValue;
this.addLog("player", "attack", attackValue);
this.receiveDamage();
this.determineAWinner();
},
receiveDamage() {
const attackValue = getRandomValue(5, 30);
this.playerHealth -= attackValue;
this.addLog("enemy", "attack", attackValue);
},
heal() {
this.currentRound++;
const restoredHealth = getRandomValue(10, 35);
if (this.playerHealth + restoredHealth > 100) {
this.playerHealth = 100;
} else {
this.playerHealth += restoredHealth;
}
this.addLog("player", "heal", restoredHealth);
this.receiveDamage();
this.determineAWinner();
},
useUltimate() {
this.currentRound++;
const attackValue = getRandomValue(25, 50);
this.enemyHealth -= attackValue;
this.addLog("player", "attack", attackValue);
this.receiveDamage();
this.determineAWinner();
},
surrender() {
this.winner = "enemy";
},
addLog(who, what, value) {
this.logMessages.unshift({
actionBy: who,
actionType: what,
actionValue: value,
});
},
},
}).mount("#game");