wobblyhead hg > minor_monsters
diff game/js/projectile.js @ 17:fe680946164b
reorganised repo
| author | ark |
|---|---|
| date | 2026-10-20 00:12 +0200 |
| parents | |
| children | 3f2811aebb3d |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/game/js/projectile.js Tue Oct 20 00:12:36 2026 +0200 @@ -0,0 +1,55 @@ +// projectiles: fly towards their target and damage it on arrival, p2 also +// slows it + +window.MM = window.MM || {}; + +MM.projectile = (function () { + "use strict"; + + function create(tower, target, damage) { + var def = tower.def; + return { + kind: def.projectile, + speed: def.projectile_speed, + x: tower.x, + y: tower.y, + tx: target.x, + ty: target.y, + target: target, + damage: damage, + slow: def.slow ? def.slow[tower.level - 1] : 0, + slowTime: def.slow_time, + angle: Math.atan2(target.y - tower.y, target.x - tower.x), + done: false + }; + } + + // home in on the target. if it died first, fly on to its last position + // and disappear there without damage. + function update(p, game, dt) { + var t = p.target; + if (t && t.alive) { + p.tx = t.x; + p.ty = t.y; + } else { + t = null; + } + var dx = p.tx - p.x, dy = p.ty - p.y; + var d = Math.hypot(dx, dy); + var step = p.speed * dt; + var reach = t ? Math.max(step, t.hitRadius) : step; + if (d <= reach) { + if (t) { + if (p.slow) MM.monster.slow(t, p.slow, p.slowTime); + game.damage(t, p.damage); + } + p.done = true; + return; + } + p.angle = Math.atan2(dy, dx); + p.x += dx / d * step; + p.y += dy / d * step; + } + + return { create: create, update: update }; +})();
