git @ Cat's Eye Technologies Cosmos-Boulders / master src / 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 { concoctJaft as concoct } from '../contrib/jaft-0.2/jaftConcoctor.js';
import { newMap, get, setr } from './immutable.js';
import { error, countDownToMode, updatePosition } from './utils.js';


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


// modes are: MOVING | GONE
const makeMissile = concoct({ newMap, setr }, `(x, y, vx, vy) =>
    let m = newMap();
    let m = setr("x", x, m);
    let m = setr("y", y, m);
    let m = setr("vx", vx, m);
    let m = setr("vy", vy, m);
    let m = setr("mode", "MOVING", m);
    let m = setr("timer", 50, m);
    m
`);


// Reducer that takes a Missile and an Action and returns a new Missile.
// Action must be one of: STEP | EXPLODE
const updateMissile = concoct({ error, get, setr, countDownToMode, updatePosition }, `(missile, action) =>
    action.type === "STEP" ? (
        get(missile, "mode") === "MOVING" ? countDownToMode(updatePosition(missile), "GONE") : missile
    ) : (
        action.type === "EXPLODE" ? (
            get(missile, "mode") === "MOVING" ? setr("mode", "GONE", setr("timer", 50, missile)) : missile
        ) : error("Unhandled action", action.type)
    )`);


export { makeMissile, updateMissile };