-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEnemy.js
74 lines (57 loc) · 1.79 KB
/
Enemy.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
(function() {
function Enemy(props) {
this.init(props);
this.direction = 0;
this.health = this.health || Math.floor(Game.Math.random(25, 150));
this.weapon = new Game.Weapon({
damage: Math.floor(Game.Math.random(15, 25)),
projectileSpeed: Math.floor(Game.Math.random(10, 15))
});
this.isFiring = false;
this.lastFire = 0;
this.fireInt = Game.Math.random(500, 1500);
this.detectionRadius = this.detectionRadius || Math.floor(Game.Math.random(300, 500));
this.movementSpeed = this.movementSpeed || Math.floor(Game.Math.random(1, 4));
}
Enemy.prototype = Object.create(kontra.sprite.prototype);
Enemy.prototype.constructor = Enemy;
Enemy.prototype.shootAt = function(object) {
var angle = Game.Math.angleFrom(this, object) + 90;
return this.weapon.fire(angle);
};
Enemy.prototype.attack = function(object) {
var distance = Game.Math.distance(this, object);
var angle = Game.Math.angleFrom(this, object)
var dx = distance*Game.Math.cos(angle+90);
var dy = distance*Game.Math.sin(angle+90);
if(distance < this.detectionRadius) {
if(distance > 200) {
this.x -= dx/this.movementSpeed;
this.y -= dy/this.movementSpeed;
}
}
}
Enemy.prototype.moveTo = function(x, y, duration) {
Game.Animator.animateTo(this, x, y, duration);
};
Enemy.prototype.takeDamage = function(damage) {
this.health -= damage;
};
Enemy.prototype.isDead = function() {
if(this.health <= 0) {
return true;
}
return false;
};
Enemy.prototype.dropItem = function() {
var item = Game.Item.random();
item.x = this.x;
item.y = this.y;
if(Game.Math.random() > Game.Math.random()) {
return item;
} else {
return false;
}
}
Game.Enemy = Enemy;
})();