-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhero.ts
More file actions
31 lines (29 loc) · 881 Bytes
/
hero.ts
File metadata and controls
31 lines (29 loc) · 881 Bytes
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
export class Hero implements IProduct {
private name: string;
private health: number;
private maxHealth: number = 100;
constructor(name: string, health: number = 100) {
this.name = name;
if (health < this.maxHealth) {
this.health = health;
} else {
this.health = this.maxHealth;
}
}
attacked(attackValue) {
if (attackValue > this.health) {
console.log(`${this.name} is no more.`);
} else {
this.health -= attackValue;
console.log(`Hero attacked: ${attackValue} -- ${this.health}`);
}
}
heal(healValue) {
if (this.health + healValue > this.maxHealth) {
console.log(`${this.name} has max health of ${this.maxHealth}`);
} else {
this.health += healValue;
console.log(`${this.name} healed to ${this.health}`);
}
}
}