-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
134 lines (112 loc) · 3.18 KB
/
Copy pathscript.js
File metadata and controls
134 lines (112 loc) · 3.18 KB
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
130
131
132
133
134
let playButton = document.getElementById("play");
let resultDiv = document.getElementById("result");
let p1NameDiv = document.getElementById("p1Name");
let p2NameDiv = document.getElementById("p2Name");
let p1HealthDiv = document.getElementById("p1Health");
let p2HealthDiv = document.getElementById("p2Health");
const updateGame = (p1, p2) => {
p1NameDiv.innerHTML = p1.name;
p2NameDiv.innerHTML = p2.name;
p1HealthDiv.innerHTML = p1.health;
p2HealthDiv.innerHTML = p2.health;
if (p1.health <= 0 || p2.health <= 0) {
game.isOver = true;
resultDiv.innerHTML = game.declareWinner(p1, p2);
}
};
class Player {
constructor(name, health, attackDamage) {
this.name = name;
this.health = health;
this.attackDmg = attackDamage;
}
strike(player, enemy, attackDmg) {
let damageAmount = Math.floor(Math.random() * attackDmg) + 1;
enemy.health -= attackDmg;
updateGame(p1, p2);
console.log(`${player.name} attacks ${enemy.name} for ${damageAmount}`);
}
heal(player) {
const hpAmount = Math.floor(Math.random() * 5) + 1;
player.health += hpAmount;
updateGame(p1, p2);
console.log(`${this.name} heals for ${hpAmount} HP`);
}
}
class Game {
constructor() {
this.isOver = false;
}
declareWinner(p1, p2) {
let message;
if (game.isOver) {
if (p1.health <= 0) {
message = `${p2.name} WINS`;
} else if (p2.health <= 0) {
message = `${p1.name} WINS`;
}
document.getElementById("victory").play();
}
return message;
}
reset(p1, p2) {
p1.health = 100;
p2.health = 100;
game.isOver = false;
resultDiv.innerHTML = "";
updateGame(p1, p2);
}
play(p1, p2) {
this.reset(p1, p2);
while (!this.isOver) {
console.log("oc");
p1.heal(p1);
p2.heal(p2);
p1.strike(p1, p2, p1.attackDmg);
p2.strike(p2, p1, p2.attackDmg);
this.checkDraw(p1, p2);
}
}
checkDraw(p1, p2) {
if (p1.health <= 0 && p2.health <= 0) {
resultDiv.innerHTML = "DRAW!!!";
}
}
}
let p1 = new Player("Sub-Zero", 100, 7);
let p2 = new Player("Liu Kang", 100, 7);
let game = new Game();
updateGame(p1, p2);
playButton.addEventListener("click", () => {
game.play(p1, p2);
});
// ** Player 1 Controls **
document.addEventListener("keydown", function (e) {
if (e.key == "q" && game.isOver == false) {
if (p2.health > 0) {
p1.strike(p1, p2, p1.attackDmg);
document.getElementById("p1attack").play();
}
}
});
document.addEventListener("keydown", function (e) {
if (e.key == "a" && game.isOver == false) {
p1.heal(p1, p2);
document.getElementById("p1heal").play();
}
});
// ** Player 2 Controls **
document.addEventListener("keydown", function (e) {
if (e.key == "p" && game.isOver == false) {
if (p1.health > 0) {
p2.strike(p2, p1, p2.attackDmg);
document.getElementById("p2attack").play();
}
}
});
document.addEventListener("keydown", function (e) {
if (e.key == "l" && game.isOver == false) {
p2.heal(p2, p1);
document.getElementById("p2heal").play();
}
});