-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
119 lines (98 loc) · 3.14 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
new Vue({
el: '#app',
data: {
playerHealth: 100,
monsterHealth: 100,
gameIsRunning: false,
turns:[]
},
methods: {
//this fucntion executes when we click on start game button
startGame: function () {
this.gameIsRunning = true;
this.playerHealth = 100;
this.monsterHealth = 100;
this.turns=[];
},
//function to attack normally
attack: function () {
// console.log("attacked");
var damage = this.calculateDamage(3, 10)
this.monsterHealth -= damage;
this.turns.unshift({
isPlayer:true,
text: "Player hits Monster for "+damage
});
if (this.checkWin()) {
return;
}
this.monsterAttack();
},
//function to use some special power to use some special attack
specialAttack: function () {
var damage= this.calculateDamage(10,20);
this.monsterHealth-=damage;
this.turns.unshift({
isPlayer:true,
text: "Player hits Monster hard for "+damage
});
if(this.checkWin()){
return;
}
this.monsterAttack();
},
//function to heal your self
heal: function () {
if(this.playerHealth<=90)
{
this.playerHealth+=10;
}else{
this.playerHealth=100;
}
this.turns.unshift({
isPlayer:true,
text: "Player heals for "+10
});
this.monsterAttack();
},
//function for giving up on game
giveUp: function () {
this.gameIsRunning = false;
this.monsterHealth = 100;
this.playerHealth = 100;
alert("You Have Lost! Try again");
},
monsterAttack:function () {
damage=this.calculateDamage(5,12);
this.playerHealth-=damage;
this.turns.unshift({
isPlayer:false,
text: "Monster hits Player for "+damage
});
this.checkWin();
},
//this fucntion calculates damage and returns it to attack fucntion
calculateDamage: function (min, max) {
return Math.max(Math.floor(Math.random() * max) + 1, min);
},
// this fucntion checks if we won the game or lost
checkWin: function () {
if (this.monsterHealth <= 0) {
if (confirm("You won! Start new Game?")) {
this.startGame();
} else {
this.gameIsRunning = false;
}
return true;
} else if (this.playerHealth <= 0) {
if (confirm("You lost! Start new Game?")) {
this.startGame();
} else {
this.gameIsRunning = false;
}
return true;
}
return false;
}
}
})