wobblyhead hg > minor_monsters
annotate game/js/tower.js @ 14:7fda634d15ad
adjustments to wave logic, tower pivot points
| author | ark |
|---|---|
| date | 2026-10-19 22:50 +0200 |
| parents | 8df95050fc8b |
| children | c1c7e4f54bf6 |
| rev | line source |
|---|---|
| 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], |
|
13
8df95050fc8b
no more build during pause; tower popup closes on any click outside it
ark
parents:
6
diff
changeset
|
22 paid: 0, // coins paid for building and upgrades |
| 1 | 23 angle: -Math.PI / 2, |
| 24 cooldown: 0, | |
| 25 burst: 0 | |
| 26 }; | |
| 27 } | |
| 28 | |
| 6 | 29 // cost of the next level, null at the top level. level n costs the |
| 30 // base cost x n. | |
| 31 function upgradeCost(t) { | |
| 32 return t.level < t.def.damage.length ? t.def.cost * (t.level + 1) : null; | |
| 33 } | |
| 34 | |
| 35 // coins back for selling the tower: half of everything paid for it, | |
|
13
8df95050fc8b
no more build during pause; tower popup closes on any click outside it
ark
parents:
6
diff
changeset
|
36 // rounded down |
|
8df95050fc8b
no more build during pause; tower popup closes on any click outside it
ark
parents:
6
diff
changeset
|
37 function refund(t) { |
|
8df95050fc8b
no more build during pause; tower popup closes on any click outside it
ark
parents:
6
diff
changeset
|
38 return Math.floor(t.paid * cfg.tower_rules.sell_refund); |
| 6 | 39 } |
| 40 | |
| 1 | 41 function sprite(t) { |
| 42 return t.level === 1 ? t.type : t.type + "_" + t.level; | |
| 43 } | |
| 44 | |
| 45 function range(t) { | |
| 46 return t.def.range[t.level - 1]; | |
| 47 } | |
| 48 | |
| 49 function damage(t) { | |
| 50 return t.def.damage[t.level - 1]; | |
| 51 } | |
| 52 | |
| 53 function inRange(t, m) { | |
| 54 var r = range(t); | |
| 55 var dx = m.x - t.x, dy = m.y - t.y; | |
| 56 return dx * dx + dy * dy <= r * r; | |
| 57 } | |
| 58 | |
| 59 // the monster in range that is furthest along, that is with the least | |
| 60 // distance left to its exit | |
| 61 function target(t, monsters) { | |
| 62 var best = null, left = Infinity; | |
| 63 for (var i = 0; i < monsters.length; i++) { | |
| 64 var m = monsters[i]; | |
| 65 if (!m.alive || !inRange(t, m)) continue; | |
| 66 var l = MM.monster.remaining(m); | |
| 67 if (l < left) { | |
| 68 left = l; | |
| 69 best = m; | |
| 70 } | |
| 71 } | |
| 72 return best; | |
| 73 } | |
| 74 | |
| 75 function update(t, game, dt) { | |
| 76 if (t.cooldown > 0) t.cooldown -= dt; | |
| 77 if (t.burst > 0) t.burst -= dt; | |
| 78 var melee = t.def.projectile_speed === 0; | |
| 79 var m = target(t, game.monsters); | |
| 80 if (!m) return; | |
| 81 if (!melee) t.angle = Math.atan2(m.y - t.y, m.x - t.x); | |
| 82 if (t.cooldown > 0) return; | |
| 83 t.cooldown += t.def.attack_time; | |
| 4 | 84 // sound events t0_attack for the melee tower, t1_shot and so on |
| 85 MM.audio.play(t.type + (melee ? "_attack" : "_shot")); | |
| 1 | 86 if (melee) { |
| 87 // every monster in range at once | |
| 88 var hit = game.monsters.filter(function (o) { return o.alive && inRange(t, o); }); | |
| 89 hit.forEach(function (o) { game.damage(o, damage(t)); }); | |
| 90 t.burst = BURST_TIME; | |
| 91 } else { | |
| 92 game.projectiles.push(MM.projectile.create(t, m, damage(t))); | |
| 93 } | |
| 94 } | |
| 95 | |
| 96 return { create: create, update: update, sprite: sprite, range: range, | |
| 6 | 97 upgradeCost: upgradeCost, refund: refund, BURST_TIME: BURST_TIME }; |
| 1 | 98 })(); |
