forked from mjzone/mk4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
92 lines (80 loc) · 1.85 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
87
88
89
90
91
92
_ = require("lodash");
var Scorpion;
var LiuKang;
class Warrior {
constructor(name, strength, skill, gear) {
this.name = name;
this.strength = strength;
this.skill = skill;
this.gear = gear;
this.calculateBonus();
this.armorBonus = 0;
}
static rollDice(times, type) {
var total = 0;
for (var i = 0; i < times; i++) {
total += Math.round(Math.random() * type);
}
return total;
}
addGear(item) {
this.gear.push(item);
if (item instanceof Armor) {
this.calculateBonus();
}
}
removeGear() {
let gearToRemove = _.first(this.gear);
if (gearToRemove) {
this.gear.splice(0, 1);
}
if (gearToRemove instanceof Armor) {
this.calculateBonus();
}
}
calculateBonus() {
this.armorBonus = 0;
_.each(this.gear, (item) => {
if (item instanceof Armor) {
this.armorBonus += item.bonus
}
});
}
attack(enemy) {
var roll = Warrior.rollDice(1, 20);
roll += this.strength;
var warriorPoints = _.clamp(roll, 1, 25);
var enemyPower = 10 + enemy.armorBonus + enemy.skill;
return warriorPoints >= enemyPower;
}
}
class Armor {
constructor(name, bonus) {
this.name = name;
this.bonus = bonus;
}
}
class Weapon {
constructor(name, damage) {
this.name = name;
this.damage = damage;
}
}
setUpWarriors = () => {
var spineArmor = new Armor("Spine Armor", 6);
var spineChain = new Weapon("Spine Chain", 5);
Scorpion = new Warrior("Scorpion", 7, 7, [spineArmor, spineChain]);
var leatherArmor = new Armor("Leather Armor", 3);
var sword = new Weapon("Chang Sword", 6);
LiuKang = new Warrior("Lui Kang", 6, 8, [leatherArmor, sword]);
}
fight = () => {
var wins = LiuKang.attack(Scorpion);
if (wins) {
console.log('Liu Kang Wins');
} else {
console.log('Scorpion Wins');
}
}
setUpWarriors();
fight();