|
6
|
1 // towers: targeting, attacks, upgrade, sell
|
|
1
|
2
|
|
|
3 window.MM = window.MM || {};
|
|
|
4
|
|
|
5 MM.tower = (function () {
|
|
|
6 "use strict";
|
|
|
7
|
|
|
8 var cfg = MM.config;
|
|
|
9 // how long a melee attack shows its spikes pushed out
|
|
|
10 var BURST_TIME = 0.3;
|
|
|
11
|
|
6
|
12 // sides: for t0, which sides draw spikes (map.facingSides); all four
|
|
|
13 // when left out
|
|
|
14 function create(type, x, y, sides) {
|
|
1
|
15 return {
|
|
|
16 type: type,
|
|
|
17 def: cfg.tower[type],
|
|
|
18 level: 1,
|
|
|
19 x: x,
|
|
|
20 y: y,
|
|
6
|
21 sides: sides || [true, true, true, true],
|
|
|
22 // purchases, {cost, pause}: pause is the number of the pause
|
|
|
23 // the purchase was made in, 0 while the game ran
|
|
|
24 paid: [],
|
|
1
|
25 angle: -Math.PI / 2,
|
|
|
26 cooldown: 0,
|
|
|
27 burst: 0
|
|
|
28 };
|
|
|
29 }
|
|
|
30
|
|
6
|
31 // cost of the next level, null at the top level. level n costs the
|
|
|
32 // base cost x n.
|
|
|
33 function upgradeCost(t) {
|
|
|
34 return t.level < t.def.damage.length ? t.def.cost * (t.level + 1) : null;
|
|
|
35 }
|
|
|
36
|
|
|
37 // coins back for selling the tower: half of everything paid for it,
|
|
|
38 // rounded down, except purchases made during the current pause, which
|
|
|
39 // come back in full. pause is the number of the current pause, 0 while
|
|
|
40 // the game runs.
|
|
|
41 function refund(t, pause) {
|
|
|
42 var rest = 0, full = 0;
|
|
|
43 t.paid.forEach(function (p) {
|
|
|
44 if (pause && p.pause === pause) full += p.cost;
|
|
|
45 else rest += p.cost;
|
|
|
46 });
|
|
|
47 return Math.floor(rest * cfg.tower_rules.sell_refund) + full;
|
|
|
48 }
|
|
|
49
|
|
1
|
50 function sprite(t) {
|
|
|
51 return t.level === 1 ? t.type : t.type + "_" + t.level;
|
|
|
52 }
|
|
|
53
|
|
|
54 function range(t) {
|
|
|
55 return t.def.range[t.level - 1];
|
|
|
56 }
|
|
|
57
|
|
|
58 function damage(t) {
|
|
|
59 return t.def.damage[t.level - 1];
|
|
|
60 }
|
|
|
61
|
|
|
62 function inRange(t, m) {
|
|
|
63 var r = range(t);
|
|
|
64 var dx = m.x - t.x, dy = m.y - t.y;
|
|
|
65 return dx * dx + dy * dy <= r * r;
|
|
|
66 }
|
|
|
67
|
|
|
68 // the monster in range that is furthest along, that is with the least
|
|
|
69 // distance left to its exit
|
|
|
70 function target(t, monsters) {
|
|
|
71 var best = null, left = Infinity;
|
|
|
72 for (var i = 0; i < monsters.length; i++) {
|
|
|
73 var m = monsters[i];
|
|
|
74 if (!m.alive || !inRange(t, m)) continue;
|
|
|
75 var l = MM.monster.remaining(m);
|
|
|
76 if (l < left) {
|
|
|
77 left = l;
|
|
|
78 best = m;
|
|
|
79 }
|
|
|
80 }
|
|
|
81 return best;
|
|
|
82 }
|
|
|
83
|
|
|
84 function update(t, game, dt) {
|
|
|
85 if (t.cooldown > 0) t.cooldown -= dt;
|
|
|
86 if (t.burst > 0) t.burst -= dt;
|
|
|
87 var melee = t.def.projectile_speed === 0;
|
|
|
88 var m = target(t, game.monsters);
|
|
|
89 if (!m) return;
|
|
|
90 if (!melee) t.angle = Math.atan2(m.y - t.y, m.x - t.x);
|
|
|
91 if (t.cooldown > 0) return;
|
|
|
92 t.cooldown += t.def.attack_time;
|
|
4
|
93 // sound events t0_attack for the melee tower, t1_shot and so on
|
|
|
94 MM.audio.play(t.type + (melee ? "_attack" : "_shot"));
|
|
1
|
95 if (melee) {
|
|
|
96 // every monster in range at once
|
|
|
97 var hit = game.monsters.filter(function (o) { return o.alive && inRange(t, o); });
|
|
|
98 hit.forEach(function (o) { game.damage(o, damage(t)); });
|
|
|
99 t.burst = BURST_TIME;
|
|
|
100 } else {
|
|
|
101 game.projectiles.push(MM.projectile.create(t, m, damage(t)));
|
|
|
102 }
|
|
|
103 }
|
|
|
104
|
|
|
105 return { create: create, update: update, sprite: sprite, range: range,
|
|
6
|
106 upgradeCost: upgradeCost, refund: refund, BURST_TIME: BURST_TIME };
|
|
1
|
107 })();
|