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