wobblyhead hg > minor_monsters
view game/js/projectile.js @ 21:fa04481563b7
doc update
| author | ark |
|---|---|
| date | 2026-10-20 00:44 +0200 |
| parents | fe680946164b |
| children | 3f2811aebb3d |
line wrap: on
line source
// 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 }; })();
