|
1
|
1 // projectiles: fly towards their target and damage it on arrival
|
|
|
2
|
|
|
3 window.MM = window.MM || {};
|
|
|
4
|
|
|
5 MM.projectile = (function () {
|
|
|
6 "use strict";
|
|
|
7
|
|
|
8 function create(tower, target, damage) {
|
|
|
9 return {
|
|
|
10 kind: tower.def.projectile,
|
|
|
11 speed: tower.def.projectile_speed,
|
|
|
12 x: tower.x,
|
|
|
13 y: tower.y,
|
|
|
14 tx: target.x,
|
|
|
15 ty: target.y,
|
|
|
16 target: target,
|
|
|
17 damage: damage,
|
|
|
18 angle: Math.atan2(target.y - tower.y, target.x - tower.x),
|
|
|
19 done: false
|
|
|
20 };
|
|
|
21 }
|
|
|
22
|
|
|
23 // home in on the target. if it died first, fly on to its last position
|
|
|
24 // and disappear there without damage.
|
|
|
25 function update(p, game, dt) {
|
|
|
26 var t = p.target;
|
|
|
27 if (t && t.alive) {
|
|
|
28 p.tx = t.x;
|
|
|
29 p.ty = t.y;
|
|
|
30 } else {
|
|
|
31 t = null;
|
|
|
32 }
|
|
|
33 var dx = p.tx - p.x, dy = p.ty - p.y;
|
|
|
34 var d = Math.hypot(dx, dy);
|
|
|
35 var step = p.speed * dt;
|
|
|
36 var reach = t ? Math.max(step, t.hitRadius) : step;
|
|
|
37 if (d <= reach) {
|
|
|
38 if (t) game.damage(t, p.damage);
|
|
|
39 p.done = true;
|
|
|
40 return;
|
|
|
41 }
|
|
|
42 p.angle = Math.atan2(dy, dx);
|
|
|
43 p.x += dx / d * step;
|
|
|
44 p.y += dy / d * step;
|
|
|
45 }
|
|
|
46
|
|
|
47 return { create: create, update: update };
|
|
|
48 })();
|