-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdestroy.js
More file actions
58 lines (50 loc) · 1.78 KB
/
destroy.js
File metadata and controls
58 lines (50 loc) · 1.78 KB
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
function SpaceThing(name, loc, speed, health) {
this.name = name;
this.loc = loc;
this.speed = speed;
this.health = health;
this.isAlive = true;
}
function DeathStar(name, loc, speed, health, energy, damage) {
SpaceThing.call(this, name, loc, speed, health);
this.energy = energy;
this.damage = damage;
}
SpaceThing.prototype.toString = function() {
console.log('Name: ' + this.name);
console.log('Location: ' + this.loc);
console.log('Speed: ' + this.speed);
console.log('Health: ' + this.health);
}
DeathStar.prototype = Object.create(SpaceThing.prototype);
DeathStar.prototype.constructor = DeathStar
DeathStar.prototype.attack = function(target) {
if (target.health) {
console.log(this.name + ' is attacking ' + target.name + ' for ' + this.damage + '!' );
this.energy -= (this.energy/2);
target.health -= this.damage;
if (target.health < 0) {
target.health = 0;
target.isAlive = false
}
console.log(target.name + ' is ' + (target.isAlive === true ? 'alive': 'dead')
+ ' with ' + target.health + ' health remaining ');
console.log(this.name + ' has ' + this.energy + ' energy remaining');
} else {
console.log('Relax, they\'re dead!');
}
}
DeathStar.prototype.toString = function () {
SpaceThing.prototype.toString.call(this);
console.log('Energy: ' + this.energy);
console.log('Damage: ' + this.damage);
}
var vaderStar = new DeathStar('Death Star', 'Alderaan', 434.1, 100, 100, 9001);
var alderaan = new SpaceThing('Alderaan', 'Alderaan System', 0 , 9000);
vaderStar.toString();
console.log('\n========================\n')
alderaan.toString();
console.log('\n========================\n')
vaderStar.attack(alderaan);
console.log('Luke, I am your father!');
console.log('I\'m not actually, my name is YOUR_NAME...')