-
Notifications
You must be signed in to change notification settings - Fork 25
/
battle.ts
64 lines (55 loc) · 1.33 KB
/
battle.ts
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
class Warrior {
private wearingUnderwear: boolean;
public weapon: Weapon;
public health: number;
private fightId: number;
publics fullName: string;
constructor(fullName: number) {
this.fullName = fullName;
this.health = 100;
this.wearingUnderwear = true;
}
fight(target: Warrior) {
this.fightId = setTimeout(this.weapon.secondsBetweenSwings, () => {
this.weapon.swing(target);
});
}
}
class Sword {
public shiny: boolean;
public sharp: boolean;
public damage: number;
public secondsBetweenSwings: number;
constructor() {
this.shiny = true;
this.sharp = true;
this.damage = 10;
this.secondsBetweenSwings = 5;
}
swing(target: Warrior) {
target.health -= this.damage;
console.log(`${target.fullName} is down to ${target.health} health.`);
}
}
class Mace {
public pointy: boolean;
public numberOfPoints: number;
public damage: number;
constructor() {
this.pointy = true;
this.numberOfPoints = 12;
this.damage = 30;
}
}
interface Weapon {
damage: number;
secondsBetweenSwings: number;
swing(target: Warrior): void;
}
let leeroy = new Warrior('Leeroy Jenkins');
let leeroy.weapon = new Sword();
let lebron = new Warrior('Lebron James');
let lebron.weapon = new Mace();
leeroy.wearingUnderwear = false;
lebron.fight(leeroy);
leeroy.fight(lebron);