// 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/>
/* --[ Constants ]---------------------------------------------------------------- */
const SCREEN_WIDTH = 640;
const SCREEN_HEIGHT = 400;
/* --[ Utility functions ]-------------------------------------------------------- */
function degreesToRadians(deg) {
return (deg / 360) * Math.PI * 2;
}
function sign(n) {
return n > 0 ? 1 : (n < 0 ? -1 : 0);
}
function cap(n, absMax) {
return (Math.abs(n) > absMax) ? absMax * sign(n) : n;
}
function limitWithin(low, high, v) {
if (v >= low && v < high) {
return v
} else if (v < 0) {
return limitWithin(low, high, v + high);
} else {
return limitWithin(low, high, v - high)
}
}
function updatePosition(obj) {
return obj.set("x", limitWithin(0, SCREEN_WIDTH, obj.get("x") + obj.get("vx")))
.set("y", limitWithin(0, SCREEN_HEIGHT, obj.get("y") + obj.get("vy")));
}
function decr(x) {
return x - 1;
}
function incr(x) {
return x + 1;
}
function countDown(obj, callback) {
const obj2 = obj.update("timer", decr);
return (obj2.get("timer") <= 0) ? callback(obj2) : obj2;
}
function countDownToMode(obj, toMode) {
return countDown(obj, function(obj2) {
return obj2.set("mode", toMode);
});
}
function fireWentDown(action) {
return (!action.prev.firePressed) && action.firePressed;
}
function fireWentUp(action) {
return action.prev.firePressed && (!action.firePressed);
}
export {
SCREEN_WIDTH, SCREEN_HEIGHT,
degreesToRadians, sign, cap, limitWithin,
updatePosition,
decr, incr, countDown, countDownToMode, fireWentDown, fireWentUp
}