-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
105 lines (104 loc) · 2.5 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
function randomValue(min, max) {
return Math.floor(Math.random() * (max - min)) + min;
}
Vue.createApp({
data() {
return {
monsterHealth: 100,
playerHealth: 100,
counterA: 0,
counterH: 0,
winner: '',
loggedMessages: [],
};
},
watch: {
playerHealth(value) {
if (value <= 0 && this.monsterHealth <= 0) {
this.winner = 'draw';
} else if (value <= 0) {
this.winner = 'monster';
}
},
monsterHealth(value) {
if (value <= 0 && this.playerHealth <= 0) {
this.winner = 'draw';
} else if (value <= 0) {
this.winner = 'player';
}
},
},
computed: {
monsterBar() {
if (this.monsterHealth < 0) {
return { width: '0%' };
}
return { width: this.monsterHealth + '%' };
},
playerBar() {
if (this.playerHealth < 0) {
return { width: '0%' };
}
return { width: this.playerHealth + '%' };
},
enableSpecialAttack() {
return this.counterA < 3;
},
enableHealing() {
return this.counterH < 2;
},
},
methods: {
startGame() {
this.playerHealth = 100;
this.monsterHealth = 100;
this.counterH = 0;
this.counterA = 0;
this.winner = '';
this.loggedMessages = [];
},
attackMonster() {
this.counterH++;
this.counterA++;
const attackValue = randomValue(5, 12);
this.monsterHealth -= attackValue;
this.addLog('Player', 'attack', attackValue);
this.attackPlayer();
},
attackPlayer() {
const attackValue = randomValue(8, 16);
this.playerHealth -= attackValue;
this.addLog('Monster', 'attack', attackValue);
},
specialAttack() {
this.counterH++;
this.counterA = 0;
const attackValue = randomValue(13, 25);
this.monsterHealth -= attackValue;
this.addLog('Player', 'attack', attackValue);
this.attackPlayer();
},
healPlayer() {
this.counterA++;
this.counterH = 0;
const healValue = randomValue(10, 20);
if (this.playerHealth + healValue > 100) {
this.playerHealth = 100;
} else {
this.playerHealth += healValue;
}
this.addLog('Player', 'heal', healValue);
this.attackPlayer();
},
surrender() {
this.winner = 'monster';
},
addLog(creature, action, value) {
this.loggedMessages.unshift({
creature: creature,
action: action,
value: value,
});
},
},
}).mount('#game');