isaacscript-common
Version:
Helper functions and features for IsaacScript mods.
78 lines (77 loc) • 3.14 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.angleToDirection = angleToDirection;
exports.directionToDegrees = directionToDegrees;
exports.directionToMoveAction = directionToMoveAction;
exports.directionToShootAction = directionToShootAction;
exports.directionToVector = directionToVector;
exports.getDirectionName = getDirectionName;
const isaac_typescript_definitions_1 = require("isaac-typescript-definitions");
const directionNames_1 = require("../objects/directionNames");
const directionToDegrees_1 = require("../objects/directionToDegrees");
const directionToMoveAction_1 = require("../objects/directionToMoveAction");
const directionToShootAction_1 = require("../objects/directionToShootAction");
const directionToVector_1 = require("../objects/directionToVector");
/**
* Helper function to convert the degrees of an angle to the `Direction` enum.
*
* Note that this function considers 0 degrees to be pointing to the right, which is unusual because
* 0 normally corresponds to up. (This corresponds to how the `Vector.GetAngleDegrees` method
* works.)
*/
function angleToDirection(angleDegrees) {
let positiveDegrees = angleDegrees;
while (positiveDegrees < 0) {
positiveDegrees += 360;
}
const normalizedDegrees = positiveDegrees % 360;
if (normalizedDegrees < 45) {
return isaac_typescript_definitions_1.Direction.RIGHT;
}
if (normalizedDegrees < 135) {
return isaac_typescript_definitions_1.Direction.DOWN;
}
if (normalizedDegrees < 225) {
return isaac_typescript_definitions_1.Direction.LEFT;
}
if (normalizedDegrees < 315) {
return isaac_typescript_definitions_1.Direction.UP;
}
return isaac_typescript_definitions_1.Direction.RIGHT;
}
/**
* Helper function to convert a direction to degrees. For example, `Direction.LEFT` (0) would return
* 180 and `Direction.RIGHT` (2) would return 0. (This corresponds to how the
* `Vector.GetAngleDegrees` method works.)
*/
function directionToDegrees(direction) {
return directionToDegrees_1.DIRECTION_TO_DEGREES[direction];
}
/**
* Helper function to convert a direction to a shoot `ButtonAction`. For example, `Direction.LEFT`
* (0) would return `ButtonAction.LEFT` (0).
*/
function directionToMoveAction(direction) {
return directionToMoveAction_1.DIRECTION_TO_MOVE_ACTION[direction];
}
/**
* Helper function to convert a direction to a shoot `ButtonAction`. For example, `Direction.LEFT`
* (0) would return `ButtonAction.SHOOT_LEFT` (4).
*/
function directionToShootAction(direction) {
return directionToShootAction_1.DIRECTION_TO_SHOOT_ACTION[direction];
}
/**
* Helper function to convert a direction to a `Vector`. For example, `Direction.LEFT` (0) would
* convert to `Vector(-1, 0).
*/
function directionToVector(direction) {
return directionToVector_1.DIRECTION_TO_VECTOR[direction];
}
/**
* Helper function to get the lowercase name of a direction. For example, `Direction.LEFT` (0) would
* return "left".
*/
function getDirectionName(direction) {
return directionNames_1.DIRECTION_NAMES[direction];
}