/
combat.js
69 lines (67 loc) · 2.09 KB
/
combat.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
window.onload = function() {
class Game {
constructor(player) {
this.playerName = player.dataset.name;
this.playerStrength = player.dataset.strength;
this.playerHealth = player.dataset.health;
}
updateGameState() {
if (this.monsterHealth < 1) {
this.consolePrint(`${this.monsterName} is DEAD!!`);
this.spawnMonster();
}
document
.querySelector("#playerName")
.setAttribute("value", this.playerName);
document
.querySelector("#playerStrength")
.setAttribute("value", this.playerStrength);
document
.querySelector("#playerHealth")
.setAttribute("value", this.playerHealth);
document
.querySelector("#monsterName")
.setAttribute("value", this.monsterName);
document
.querySelector("#monsterStrength")
.setAttribute("value", this.monsterStrength);
document
.querySelector("#monsterHealth")
.setAttribute("value", this.monsterHealth);
}
spawnMonster() {
let nextMonster = document.querySelector("#monsters input:nth-child(1)");
if (!nextMonster) {
alert("victory!");
}
this.monsterName = nextMonster.dataset.name;
this.monsterHealth = nextMonster.dataset.health;
this.monsterStrength = nextMonster.dataset.strength;
nextMonster.remove();
}
consolePrint(text) {
document.querySelector("#console").innerHTML += text;
}
performCombat() {
this.consolePrint(
`<p>${this.playerName} attacks ${this.monsterName} for ${
this.playerStrength
} damage!</p>`
);
this.consolePrint(
`<p>${this.monsterName} attacks ${this.playerName} for ${
this.monsterStrength
} damage!</p>`
);
this.playerHealth -= this.monsterStrength;
this.monsterHealth -= this.playerStrength;
}
}
var DOD = new Game(document.querySelector("#playerData"));
DOD.spawnMonster();
DOD.updateGameState();
document.querySelector("button").onclick = function() {
DOD.performCombat();
DOD.updateGameState();
};
};