view game/js/projectile.js @ 5:67bf3c505145

adjusted map0 mask
author ark
date 2026-10-18 22:50 +0200
parents 5492efd60e2c
children 910b26feca76
line wrap: on
line source

// 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 };
})();