wobblyhead hg > minor_monsters
view game/js/tower.js @ 12:dc314bfbb1cf
minor map adjustment
| author | ark |
|---|---|
| date | 2026-10-19 20:41 +0200 |
| parents | 910b26feca76 |
| children | 8df95050fc8b |
line wrap: on
line source
// towers: targeting, attacks, upgrade, sell window.MM = window.MM || {}; MM.tower = (function () { "use strict"; var cfg = MM.config; // how long a melee attack shows its spikes pushed out var BURST_TIME = 0.3; // sides: for t0, which sides draw spikes (map.facingSides); all four // when left out function create(type, x, y, sides) { return { type: type, def: cfg.tower[type], level: 1, x: x, y: y, sides: sides || [true, true, true, true], // purchases, {cost, pause}: pause is the number of the pause // the purchase was made in, 0 while the game ran paid: [], angle: -Math.PI / 2, cooldown: 0, burst: 0 }; } // cost of the next level, null at the top level. level n costs the // base cost x n. function upgradeCost(t) { return t.level < t.def.damage.length ? t.def.cost * (t.level + 1) : null; } // coins back for selling the tower: half of everything paid for it, // rounded down, except purchases made during the current pause, which // come back in full. pause is the number of the current pause, 0 while // the game runs. function refund(t, pause) { var rest = 0, full = 0; t.paid.forEach(function (p) { if (pause && p.pause === pause) full += p.cost; else rest += p.cost; }); return Math.floor(rest * cfg.tower_rules.sell_refund) + full; } function sprite(t) { return t.level === 1 ? t.type : t.type + "_" + t.level; } function range(t) { return t.def.range[t.level - 1]; } function damage(t) { return t.def.damage[t.level - 1]; } function inRange(t, m) { var r = range(t); var dx = m.x - t.x, dy = m.y - t.y; return dx * dx + dy * dy <= r * r; } // the monster in range that is furthest along, that is with the least // distance left to its exit function target(t, monsters) { var best = null, left = Infinity; for (var i = 0; i < monsters.length; i++) { var m = monsters[i]; if (!m.alive || !inRange(t, m)) continue; var l = MM.monster.remaining(m); if (l < left) { left = l; best = m; } } return best; } function update(t, game, dt) { if (t.cooldown > 0) t.cooldown -= dt; if (t.burst > 0) t.burst -= dt; var melee = t.def.projectile_speed === 0; var m = target(t, game.monsters); if (!m) return; if (!melee) t.angle = Math.atan2(m.y - t.y, m.x - t.x); if (t.cooldown > 0) return; t.cooldown += t.def.attack_time; // sound events t0_attack for the melee tower, t1_shot and so on MM.audio.play(t.type + (melee ? "_attack" : "_shot")); if (melee) { // every monster in range at once var hit = game.monsters.filter(function (o) { return o.alive && inRange(t, o); }); hit.forEach(function (o) { game.damage(o, damage(t)); }); t.burst = BURST_TIME; } else { game.projectiles.push(MM.projectile.create(t, m, damage(t))); } } return { create: create, update: update, sprite: sprite, range: range, upgradeCost: upgradeCost, refund: refund, BURST_TIME: BURST_TIME }; })();
