forked from mali3days/jshardtryingoop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
86 lines (75 loc) · 1.99 KB
/
game.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
const gamer = {};
let id = 0;
class Game {
constructor() {
this.id = id++;
gamer[this.id] = this;
this.score = 0;
this.score2 = 0;
}
render() {
document.querySelector(".score-player1").innerHTML = this.score;
document.querySelector(".score-player2").innerHTML = this.score2;
player1.renderGnome();
player2.renderHuman();
}
}
class Hero extends Game {
constructor(name) {
super();
this.hp = 50;
this.name = name;
}
reduceHp(attackUser, damage) {
this.hp = this.hp - (Math.floor(Math.random() * damage - 2) + 2);
this.render();
if (this.hp <= 0) {
gamer[this.id].hp = 50;
this.addScore(attackUser);
this.render();
}
}
attack() {
gamer[this.id].reduceHp(this.id, this.damage);
gamer[this.id].reduceHp(this.id, this.damage);
}
addScore(attackUser) {
if (attackUser === 0) {
gamer[attackUser].score2 += 1;
} else {
gamer[attackUser].score += 1;
}
}
}
class Gnome extends Hero {
constructor(name) {
super(name);
this.weapon = "Sward";
this.damage = 8;
}
renderGnome() {
document.querySelector(".hp-player1").innerHTML = this.hp;
document.querySelector(".weapon-player1").innerHTML = this.weapon;
document.querySelector(".name-player1").innerHTML = this.name;
}
}
class Human extends Hero {
constructor(name) {
super(name);
this.weapon = "Bow";
this.damage = 10;
}
renderHuman() {
document.querySelector(".hp-player2").innerHTML = this.hp;
document.querySelector(".weapon-player2").innerHTML = this.weapon;
document.querySelector(".name-player2").innerHTML = this.name;
}
}
let button1 = document.querySelector(".player1-attack");
let button2 = document.querySelector(".player2-attack");
let player1 = new Gnome("Perky");
button1.addEventListener("click", function(){player1.attack()});
let player2 = new Human("Oleja");
button2.addEventListener("click", function(){player2.attack()});
console.log(player1);
console.log(player2);