git @ Cat's Eye Technologies Cosmos-Boulders / master src / boulder.js
master

Tree @master (Download .tar.gz)

boulder.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, rep } from './immutable.js';
import {
    error, floor, random,
    screenWidth, screenHeight,
    countDownToMode, updatePosition
} from './utils.js';


/* --[ Boulder objects ]-------------------------------------------------------- */


// modes are: APPEARING | MOVING | EXPLODING | GONE
// FIXME: initial velocity should be from -1.0 to +1.0
const makeBoulder = concoct({ newMap, setr, floor, random, screenWidth, screenHeight }, `(i) =>
    let m = newMap();
    let m = setr("x", floor(random() * screenWidth()), m);
    let m = setr("y", floor(random() * screenHeight()), m);
    let m = setr("vx", random() - 1.0, m);
    let m = setr("vy", random() - 1.0, m);
    let m = setr("mode", "APPEARING", setr("timer", 60, m));
    m
`);


const makeBoulders = concoct({ rep, makeBoulder }, `() =>
    rep(8, makeBoulder)`);


const updateBoulder = concoct({ error, get, setr, updatePosition, countDownToMode }, `(boulder, action) =>
  action.type === "STEP" ? (
    get(boulder, "mode") === "APPEARING" ? countDownToMode(boulder, "MOVING") : (
      get(boulder, "mode") === "MOVING" ? updatePosition(boulder) : (
        get(boulder, "mode") === "EXPLODING" ? countDownToMode(boulder, "GONE") : (
          boulder
        )
      )
    )
  ) : (
    action.type === "EXPLODE" ? (
      get(boulder, "mode") === 'MOVING' ? (
        setr("mode", "EXPLODING", setr("timer", 50, boulder))
      ) : boulder
    ) : error("Unhandled action", action.type)
  )
`);


export { makeBoulders, updateBoulder };