git @ Cat's Eye Technologies Cosmos-Boulders / master src / cosmos-boulders / missile.js
master

Tree @master (Download .tar.gz)

missile.js @masterraw · history · blame

// SPDX-FileCopyrightText: In 2019, Chris Pressey, the original author of this work, placed it into the public domain.
// SPDX-License-Identifier: Unlicense
// For more information, please refer to <https://unlicense.org/>


import { countDownToMode, updatePosition } from './utils.js';


/* --[ Missile objects ]-------------------------------------------------------- */


function makeMissile(x, y, vx, vy) {
    return Immutable.Map({
        x: x,
        y: y,
        vx: vx,
        vy: vy,
        mode: 'MOVING',  // MOVING | GONE
        timer: 50
    });
}


// Reducer that takes a Missile and an Action and returns a new Missile.
// Action must be one of: STEP | EXPLODE
function updateMissile(missile, action) {
    if (action.type === 'STEP') {
        if (missile.get("mode") === 'MOVING') {
            return countDownToMode(updatePosition(missile), 'GONE');
        } else {
            return missile;
        }
    } else if (action.type === 'EXPLODE') {
        if (missile.get("mode") === 'MOVING') {
            return missile.set("mode", 'GONE').set("timer", 50);
        } else {
            return missile;
        }
    }
    throw new Error("Unhandled action: " + action.type);
}

export { makeMissile, updateMissile };