-
Notifications
You must be signed in to change notification settings - Fork 0
/
missile.js
73 lines (47 loc) · 1.66 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
class Missile {
constructor(pos, target, speed) {
this.radius = 3;
this.speed = speed;
this.originX = pos.x;
this.originY = pos.y;
this.x = pos.x;
this.y = pos.y;
this.targetX = target.x;
this.targetY = target.y;
this.angle = -Math.atan2(this.originX -this.targetX, this.originY - this.targetY) - Math.PI/2;
this.live = Missile.LIVE;
}
step(dt) {
this.x += dt*this.speed * Math.cos(this.angle);
this.y += dt*this.speed * Math.sin(this.angle);
var dx = this.targetX - this.x;
var dy = this.targetY - this.y;
var distanceToTarget = Math.sqrt(dx * dx + dy * dy);
if ( this.distanceToTarget == null)
this.distanceToTarget = distanceToTarget;
if (distanceToTarget > this.distanceToTarget) {
this.x = this.targetX;
this.y = this.targetY;
this.live = 0;
}
this.distanceToTarget = distanceToTarget;
}
draw(ctx) {
ctx.beginPath();
ctx.strokeStyle = 'gray';
ctx.fillStyle = 'yellow';
ctx.arc(this.x,this.y,this.radius,2*Math.PI,false);
ctx.fill();
// ctx.stroke();
let crossSize = 5;
ctx.beginPath();
ctx.strokeStyle = 'lightgreen';
ctx.moveTo(this.targetX-crossSize, this.targetY-crossSize);
ctx.lineTo(this.targetX+crossSize, this.targetY+crossSize);
ctx.stroke();
ctx.moveTo(this.targetX+crossSize, this.targetY-crossSize);
ctx.lineTo(this.targetX-crossSize, this.targetY+crossSize);
ctx.stroke();
}
}
Missile.LIVE = 100;