-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmissile.js
73 lines (65 loc) · 1.63 KB
/
missile.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
/* exported missile */
/* global angleRectOverlap,ressources,angleDiff,drawnEntity,mergeObject */
"use strict";
var missile = function(settings,target) {
var ini = {
type:'missile',
speed: 500,
strength: 2,
hotspot:new Point(1.0,0.5),
rotSpeed: Math.PI*4,
duration:0.5,
radius:4,
frames:ressources.missile
};
// inherits from drawnEntity
var missile = drawnEntity(mergeObject(settings,ini));
missile.target = target;
log("missile created");
missile.update = function(dt) {
this.duration -= dt;
if (this.duration <= 0) {
this.destroy();
} else {
if(this.target !== null) {
if(!this.target.destroyed) {
var target = this.target.position;
var targetAngle = angle(this.position,target);
var angleTo = angleDiff(this.angle,targetAngle);
var angleStep = this.rotSpeed * dt;
if (angleTo > angleStep) {
this.angle += angleStep;
}else if (angleTo < -angleStep) {
this.angle -= angleStep;
} else {
this.angle = targetAngle;
}
} else if (this.target.destroyed) {
// release reference
this.target = null;
}
}
var step = this.speed * dt;
this.position.x += step * Math.cos(this.angle);
this.position.y += step * Math.sin(this.angle);
}
};
missile.hit = function() {
/*
smallExplosion({
layerName : this.layerName,
position:this.position
});
//*/
playSoundInstance(sounds['missile']);
this.destroy();
};
var superDestroy = missile.destroy;
missile.destroy = function() {
engine.killable.erase(this);
superDestroy.call(this);
};
engine.killable.push(missile);
missile.overlap = angleRectOverlap;
return missile;
};