Skip to content

Commit eadac48

Browse files
author
Gasca
committed
added methods for the game
1 parent 0380259 commit eadac48

File tree

3 files changed

+85
-92
lines changed

3 files changed

+85
-92
lines changed

src/App.vue

Lines changed: 85 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<template>
22
<div id="app">
33
<img alt="Vue logo" src="./assets/logo.png">
4-
<h3>game is runnig?: {{ gameIsRunnig }}</h3>
4+
<h3>game is runnig?: {{ isRunnig }}</h3>
55
<section class="row">
66
<div class="small-6 columns">
77
<h1 class="text-center">YOU</h1>
@@ -10,10 +10,7 @@
1010
class="healthbar text-center"
1111
style="background-color: green; margin: 0; color: white;"
1212
:style="{width: playerHealth + '%'}"
13-
>
14-
{{ playerHealth }}
15-
16-
</div>
13+
>{{ playerHealth }}</div>
1714
</div>
1815
</div>
1916
<div class="small-6 columns">
@@ -22,20 +19,17 @@
2219
<div
2320
class="healthbar text-center"
2421
style="background-color: green; margin: 0; color: white;"
25-
:style="{width: MonsterHealth + '%'}"
26-
>
27-
{{ MonsterHealth }}
28-
29-
</div>
22+
:style="{width: monsterHealth + '%'}"
23+
>{{ monsterHealth }}</div>
3024
</div>
3125
</div>
3226
</section>
33-
<section class="row controls">
27+
<section class="row controls" v-if="!isRunnig">
3428
<div class="small-12 columns">
3529
<button id="start-game" @click="startGame">START NEW GAME</button>
3630
</div>
3731
</section>
38-
<section class="row controls">
32+
<section class="row controls" v-else>
3933
<div class="small-12 columns">
4034
<button id="attack" @click="attack">ATTACK</button>
4135
<button id="special-attack" @click="specialAttack">SPECIAL ATTACK</button>
@@ -58,37 +52,90 @@ export default {
5852
name: "app",
5953
data: function() {
6054
return {
61-
playerHealth: 30,
62-
MonsterHealth: 45,
63-
gameIsRunnig: false
55+
playerHealth: 10,
56+
monsterHealth: 10,
57+
isRunnig: false,
58+
turns: []
6459
};
6560
},
6661
methods: {
67-
monsterIsAttacking(interval){
68-
setInterval(function(){
69-
this.playerHealth = this.playerHealth - 2;
70-
var result = (typeof this.playerHealth === 'number');
71-
console.log(this.playerHealth + " is number?:" + result);
72-
//NaN is number?:true
73-
}, interval);
74-
75-
},
76-
startGame() {
77-
this.gameIsRunnig = true;
62+
startGame: function() {
7863
this.playerHealth = 100;
79-
this.MonsterHealth = 100;
80-
console.log("game status set to: " + this.gameIsRunnig);
81-
this.monsterIsAttacking(1000);
82-
},
83-
giveUp() {
84-
this.gameIsRunnig = false;
85-
this.playerHealth = 0;
86-
this.MonsterHealth = 0;
87-
console.log("game status set to: " + this.gameIsRunnig);
64+
this.monsterHealth = 100;
65+
this.isRunnig = true;
8866
},
89-
attack(){},
90-
specialAttack(){},
91-
heal(){}
67+
attack: function() {
68+
var damage = this.calculateDamage(3, 10);
69+
this.monsterHealth -= damage;
70+
this.turns.unshift({
71+
isPlayer: true,
72+
text: "Player hits Monster for " + damage
73+
});
74+
if (this.checkWin()) {
75+
return;
76+
}
77+
78+
this.monsterAttacks();
79+
},
80+
specialAttack: function() {
81+
var damage = this.calculateDamage(10, 20);
82+
this.monsterHealth -= damage;
83+
this.turns.unshift({
84+
isPlayer: true,
85+
text: "Player hits Monster hard for " + damage
86+
});
87+
if (this.checkWin()) {
88+
return;
89+
}
90+
this.monsterAttacks();
91+
},
92+
heal: function() {
93+
if (this.playerHealth <= 90) {
94+
this.playerHealth += 10;
95+
} else {
96+
this.playerHealth = 100;
97+
}
98+
this.turns.unshift({
99+
isPlayer: true,
100+
text: "Player heals for 10"
101+
});
102+
this.monsterAttacks();
103+
},
104+
giveUp: function() {
105+
this.playerHealth = 100;
106+
this.monsterHealth = 100;
107+
this.isRunnig = false;
108+
},
109+
monsterAttacks: function() {
110+
var damage = this.calculateDamage(5, 12);
111+
this.playerHealth -= damage;
112+
this.checkWin();
113+
this.turns.unshift({
114+
isPlayer: false,
115+
text: "Monster hits Player for " + damage
116+
});
117+
},
118+
calculateDamage: function(min, max) {
119+
return Math.max(Math.floor(Math.random() * max) + 1, min);
120+
},
121+
checkWin: function() {
122+
if (this.monsterHealth <= 0) {
123+
if (confirm("You won! New Game?")) {
124+
this.startGame();
125+
} else {
126+
this.isRunnig = false;
127+
}
128+
return true;
129+
} else if (this.playerHealth <= 0) {
130+
if (confirm("You lost! New Game?")) {
131+
this.startGame();
132+
} else {
133+
this.isRunnig = false;
134+
}
135+
return true;
136+
}
137+
return false;
138+
}
92139
}
93140
};
94141
</script>
@@ -107,7 +154,6 @@ export default {
107154
}
108155
109156
.healthbar {
110-
111157
height: 40px;
112158
background-color: #eee;
113159
margin: auto;

src/components/character.vue

Lines changed: 0 additions & 29 deletions
This file was deleted.

src/components/healthBar.vue

Lines changed: 0 additions & 24 deletions
This file was deleted.

0 commit comments

Comments
 (0)