diff game/js/projectile.js @ 1:5492efd60e2c

first prototype + updated art sheet
author ark
date 2026-10-18 14:50 +0200
parents
children 910b26feca76
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/game/js/projectile.js	Sun Oct 18 14:50:20 2026 +0200
@@ -0,0 +1,48 @@
+// projectiles: fly towards their target and damage it on arrival
+
+window.MM = window.MM || {};
+
+MM.projectile = (function () {
+    "use strict";
+
+    function create(tower, target, damage) {
+        return {
+            kind: tower.def.projectile,
+            speed: tower.def.projectile_speed,
+            x: tower.x,
+            y: tower.y,
+            tx: target.x,
+            ty: target.y,
+            target: target,
+            damage: damage,
+            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) 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 };
+})();