wobblyhead hg > minor_monsters
comparison game/js/projectile.js @ 17:fe680946164b
reorganised repo
| author | ark |
|---|---|
| date | 2026-10-20 00:12 +0200 |
| parents | |
| children | 3f2811aebb3d |
comparison
equal
deleted
inserted
replaced
| 16:30bd10284fd8 | 17:fe680946164b |
|---|---|
| 1 // projectiles: fly towards their target and damage it on arrival, p2 also | |
| 2 // slows it | |
| 3 | |
| 4 window.MM = window.MM || {}; | |
| 5 | |
| 6 MM.projectile = (function () { | |
| 7 "use strict"; | |
| 8 | |
| 9 function create(tower, target, damage) { | |
| 10 var def = tower.def; | |
| 11 return { | |
| 12 kind: def.projectile, | |
| 13 speed: def.projectile_speed, | |
| 14 x: tower.x, | |
| 15 y: tower.y, | |
| 16 tx: target.x, | |
| 17 ty: target.y, | |
| 18 target: target, | |
| 19 damage: damage, | |
| 20 slow: def.slow ? def.slow[tower.level - 1] : 0, | |
| 21 slowTime: def.slow_time, | |
| 22 angle: Math.atan2(target.y - tower.y, target.x - tower.x), | |
| 23 done: false | |
| 24 }; | |
| 25 } | |
| 26 | |
| 27 // home in on the target. if it died first, fly on to its last position | |
| 28 // and disappear there without damage. | |
| 29 function update(p, game, dt) { | |
| 30 var t = p.target; | |
| 31 if (t && t.alive) { | |
| 32 p.tx = t.x; | |
| 33 p.ty = t.y; | |
| 34 } else { | |
| 35 t = null; | |
| 36 } | |
| 37 var dx = p.tx - p.x, dy = p.ty - p.y; | |
| 38 var d = Math.hypot(dx, dy); | |
| 39 var step = p.speed * dt; | |
| 40 var reach = t ? Math.max(step, t.hitRadius) : step; | |
| 41 if (d <= reach) { | |
| 42 if (t) { | |
| 43 if (p.slow) MM.monster.slow(t, p.slow, p.slowTime); | |
| 44 game.damage(t, p.damage); | |
| 45 } | |
| 46 p.done = true; | |
| 47 return; | |
| 48 } | |
| 49 p.angle = Math.atan2(dy, dx); | |
| 50 p.x += dx / d * step; | |
| 51 p.y += dy / d * step; | |
| 52 } | |
| 53 | |
| 54 return { create: create, update: update }; | |
| 55 })(); |
