UNPKG

isaacscript-common

Version:

Helper functions and features for IsaacScript mods.

186 lines (185 loc) • 9.01 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SHOOTING_BUTTON_ACTIONS_SET = exports.SHOOTING_BUTTON_ACTIONS = exports.MOVEMENT_BUTTON_ACTIONS_SET = exports.MOVEMENT_BUTTON_ACTIONS = exports.MODIFIER_KEYS = void 0; exports.controllerToString = controllerToString; exports.getMoveButtonActions = getMoveButtonActions; exports.getShootButtonActions = getShootButtonActions; exports.isActionPressed = isActionPressed; exports.isActionPressedOnAnyInput = isActionPressedOnAnyInput; exports.isActionTriggered = isActionTriggered; exports.isActionTriggeredOnAnyInput = isActionTriggeredOnAnyInput; exports.isKeyboardPressed = isKeyboardPressed; exports.isModifierKeyPressed = isModifierKeyPressed; exports.isMoveAction = isMoveAction; exports.isMoveActionPressed = isMoveActionPressed; exports.isMoveActionPressedOnAnyInput = isMoveActionPressedOnAnyInput; exports.isMoveActionTriggered = isMoveActionTriggered; exports.isMoveActionTriggeredOnAnyInput = isMoveActionTriggeredOnAnyInput; exports.isShootAction = isShootAction; exports.isShootActionPressed = isShootActionPressed; exports.isShootActionPressedOnAnyInput = isShootActionPressedOnAnyInput; exports.isShootActionTriggered = isShootActionTriggered; exports.isShootActionTriggeredOnAnyInput = isShootActionTriggeredOnAnyInput; exports.keyboardToString = keyboardToString; const isaac_typescript_definitions_1 = require("isaac-typescript-definitions"); const cachedEnumValues_1 = require("../cachedEnumValues"); const keyboardToStringMap_1 = require("../maps/keyboardToStringMap"); const ReadonlySet_1 = require("../types/ReadonlySet"); const string_1 = require("./string"); exports.MODIFIER_KEYS = [ isaac_typescript_definitions_1.Keyboard.LEFT_SHIFT, // 340 isaac_typescript_definitions_1.Keyboard.LEFT_CONTROL, // 341 isaac_typescript_definitions_1.Keyboard.LEFT_ALT, // 342 isaac_typescript_definitions_1.Keyboard.LEFT_SUPER, // 343 isaac_typescript_definitions_1.Keyboard.RIGHT_SHIFT, // 344 isaac_typescript_definitions_1.Keyboard.RIGHT_CONTROL, // 345 isaac_typescript_definitions_1.Keyboard.RIGHT_ALT, // 346 isaac_typescript_definitions_1.Keyboard.RIGHT_SUPER, // 347 ]; exports.MOVEMENT_BUTTON_ACTIONS = [ isaac_typescript_definitions_1.ButtonAction.LEFT, // 0 isaac_typescript_definitions_1.ButtonAction.RIGHT, // 1 isaac_typescript_definitions_1.ButtonAction.UP, // 2 isaac_typescript_definitions_1.ButtonAction.DOWN, // 3 ]; exports.MOVEMENT_BUTTON_ACTIONS_SET = new ReadonlySet_1.ReadonlySet(exports.MOVEMENT_BUTTON_ACTIONS); exports.SHOOTING_BUTTON_ACTIONS = [ isaac_typescript_definitions_1.ButtonAction.SHOOT_LEFT, // 4 isaac_typescript_definitions_1.ButtonAction.SHOOT_RIGHT, // 5 isaac_typescript_definitions_1.ButtonAction.SHOOT_UP, // 6 isaac_typescript_definitions_1.ButtonAction.SHOOT_DOWN, // 7 ]; exports.SHOOTING_BUTTON_ACTIONS_SET = new ReadonlySet_1.ReadonlySet(exports.SHOOTING_BUTTON_ACTIONS); /** * Helper function to get the enum name for the specified `Controller` value. Note that this will * trim off the "BUTTON_" prefix. * * Returns undefined if the submitted controller value was not valid. */ function controllerToString(controller) { const key = isaac_typescript_definitions_1.Controller[controller]; // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition if (key === undefined) { return undefined; } return (0, string_1.trimPrefix)(key, "BUTTON_"); } /** * Helper function to get the movement actions that the specified `ControllerIndex` is currently * pressing down. This returns an array because a player can be holding down more than one movement * key at a time. */ function getMoveButtonActions(controllerIndex) { return exports.MOVEMENT_BUTTON_ACTIONS.filter((buttonAction) => Input.IsActionPressed(buttonAction, controllerIndex)); } /** * Helper function to get the shooting actions that the specified `ControllerIndex` is currently * pressing down. This returns an array because a player can be holding down more than one shooting * key at a time. */ function getShootButtonActions(controllerIndex) { return exports.SHOOTING_BUTTON_ACTIONS.filter((buttonAction) => Input.IsActionPressed(buttonAction, controllerIndex)); } /** * Helper function to check if a player is pressing a specific button (i.e. holding it down). * * This is a variadic version of `Input.IsActionPressed`, meaning that you can pass as many buttons * as you want to check for. This function will return true if any of the buttons are pressed. */ function isActionPressed(controllerIndex, ...buttonActions) { return buttonActions.some((buttonAction) => Input.IsActionPressed(buttonAction, controllerIndex)); } /** * Helper function to iterate over all inputs to determine if a specific button is pressed (i.e. * being held down). * * This function is variadic, meaning you can pass as many buttons as you want to check for. This * function will return true if any of the buttons are pressed. */ function isActionPressedOnAnyInput(...buttonActions) { return cachedEnumValues_1.CONTROLLER_INDEX_VALUES.some((controllerIndex) => isActionPressed(controllerIndex, ...buttonActions)); } /** * Helper function to check if a player is triggering a specific button (i.e. pressing and releasing * it). * * This is a variadic version of `Input.IsActionTriggered`, meaning that you can pass as many * buttons as you want to check for. This function will return true if any of the buttons are * triggered. */ function isActionTriggered(controllerIndex, ...buttonActions) { return buttonActions.some((buttonAction) => Input.IsActionTriggered(buttonAction, controllerIndex)); } /** * Iterates over all inputs to determine if a specific button is triggered (i.e. held down and then * released). * * This function is variadic, meaning you can pass as many buttons as you want to check for. This * function will return true if any of the buttons are pressed. */ function isActionTriggeredOnAnyInput(...buttonActions) { return cachedEnumValues_1.CONTROLLER_INDEX_VALUES.some((controllerIndex) => isActionTriggered(controllerIndex, ...buttonActions)); } /** * Helper function to see if a specific keyboard key is being held down by the player. * * This function is variadic, meaning you can pass as many keyboard values as you want to check for. * This function will return true if any of the values are pressed. */ function isKeyboardPressed(...keys) { return keys.some((key) => Input.IsButtonPressed(key, isaac_typescript_definitions_1.ControllerIndex.KEYBOARD)); } /** * Helper function to check if one or more modifier keys are being pressed down on the keyboard. * * A modifier key is defined as shift, control, alt, or Windows. */ function isModifierKeyPressed() { return isKeyboardPressed(...exports.MODIFIER_KEYS); } function isMoveAction(buttonAction) { return exports.MOVEMENT_BUTTON_ACTIONS_SET.has(buttonAction); } function isMoveActionPressed(controllerIndex) { return isActionPressed(controllerIndex, ...exports.MOVEMENT_BUTTON_ACTIONS); } function isMoveActionPressedOnAnyInput() { return exports.MOVEMENT_BUTTON_ACTIONS.some((moveAction) => isActionPressedOnAnyInput(moveAction)); } function isMoveActionTriggered(controllerIndex) { return isActionTriggered(controllerIndex, ...exports.MOVEMENT_BUTTON_ACTIONS); } function isMoveActionTriggeredOnAnyInput() { return exports.MOVEMENT_BUTTON_ACTIONS.some((moveAction) => isActionTriggeredOnAnyInput(moveAction)); } function isShootAction(buttonAction) { return exports.SHOOTING_BUTTON_ACTIONS_SET.has(buttonAction); } function isShootActionPressed(controllerIndex) { return isActionPressed(controllerIndex, ...exports.SHOOTING_BUTTON_ACTIONS); } function isShootActionPressedOnAnyInput() { return exports.SHOOTING_BUTTON_ACTIONS.some((shootAction) => isActionPressedOnAnyInput(shootAction)); } function isShootActionTriggered(controllerIndex) { return isActionTriggered(controllerIndex, ...exports.SHOOTING_BUTTON_ACTIONS); } function isShootActionTriggeredOnAnyInput() { return exports.SHOOTING_BUTTON_ACTIONS.some((shootAction) => isActionTriggeredOnAnyInput(shootAction)); } /** * Helper function to get the string that would be typed if someone pressed the corresponding key. * This is useful for creating in-game chat. * * Note that this function will only work for the keyboard values that are printable. Thus, it will * return undefined for e.g. `Keyboard.LEFT_SHIFT` (340). If all you want is the corresponding name * of the key, then simply use the enum reverse mapping (e.g. `Keyboard[keyboard]`). */ function keyboardToString(keyboard, uppercase) { const tuple = keyboardToStringMap_1.KEYBOARD_TO_STRING_MAP.get(keyboard); if (tuple === undefined) { return undefined; } const [lowercaseCharacter, uppercaseCharacter] = tuple; return uppercase ? uppercaseCharacter : lowercaseCharacter; }