UNPKG

isaacscript-common

Version:

Helper functions and features for IsaacScript mods.

164 lines (163 loc) 6.84 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getCardDescription = getCardDescription; exports.getCardName = getCardName; exports.getItemConfigCardType = getItemConfigCardType; exports.hasCard = hasCard; exports.isCard = isCard; exports.isCardType = isCardType; exports.isModdedCardType = isModdedCardType; exports.isPocketItemObject = isPocketItemObject; exports.isReverseTarotCard = isReverseTarotCard; exports.isRune = isRune; exports.isSpecialCard = isSpecialCard; exports.isSuitCard = isSuitCard; exports.isTarotCard = isTarotCard; exports.isValidCardType = isValidCardType; exports.isVanillaCardType = isVanillaCardType; exports.useCardTemp = useCardTemp; const isaac_typescript_definitions_1 = require("isaac-typescript-definitions"); const cachedEnumValues_1 = require("../cachedEnumValues"); const cachedClasses_1 = require("../core/cachedClasses"); const constantsFirstLast_1 = require("../core/constantsFirstLast"); const cardDescriptions_1 = require("../objects/cardDescriptions"); const cardNames_1 = require("../objects/cardNames"); const itemConfigCardTypesForCards_1 = require("../sets/itemConfigCardTypesForCards"); const flag_1 = require("./flag"); const types_1 = require("./types"); /** * Helper function to get a card description from a `CardType` value. Returns "Unknown" if the * provided card type is not valid. * * This function works for both vanilla and modded trinkets. * * For example, `getCardDescription(CardType.FOOL)` would return "Where journey begins". */ function getCardDescription(cardType) { // "ItemConfigCard.Description" is bugged with vanilla cards on patch v1.7.6, so we use a // hard-coded map as a workaround. const cardDescription = cardDescriptions_1.CARD_DESCRIPTIONS[cardType]; // Handle modded cards. // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition if (cardDescription !== undefined) { return cardDescription; } const itemConfigCard = cachedClasses_1.itemConfig.GetCard(cardType); if (itemConfigCard !== undefined) { return itemConfigCard.Description; } return cardDescriptions_1.DEFAULT_CARD_DESCRIPTION; } /** * Helper function to get the name of a card. Returns "Unknown" if the provided card type is not * valid. * * This function works for both vanilla and modded trinkets. * * For example, `getCardName(Card.FOOL)` would return "0 - The Fool". */ function getCardName(cardType) { // "ItemConfigCard.Name" is bugged with vanilla cards on patch v1.7.6, so we use a hard-coded map // as a workaround. const cardName = cardNames_1.CARD_NAMES[cardType]; // Handle modded cards. // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition if (cardName !== undefined) { return cardName; } const itemConfigCard = cachedClasses_1.itemConfig.GetCard(cardType); if (itemConfigCard !== undefined) { return itemConfigCard.Name; } return cardNames_1.DEFAULT_CARD_NAME; } /** * Helper function to get the item config card type of a particular card, rune, or object. For * example, the item config card type of `CardType.FOOL` is equal to `ItemConfigCardType.TAROT`. * * Returns undefined if the provided card type was not valid. */ function getItemConfigCardType(cardType) { const itemConfigCard = cachedClasses_1.itemConfig.GetCard(cardType); if (itemConfigCard === undefined) { return undefined; } return itemConfigCard.CardType; } /** * Helper function to check if a player is holding a specific card in one of their pocket item * slots. * * This function is variadic, meaning that you can pass as many cards as you want to check for. The * function will return true if the player has any of the cards. */ function hasCard(player, ...cardTypes) { const cardTypesSet = new Set(cardTypes); return cachedEnumValues_1.POCKET_ITEM_SLOT_VALUES.some((pocketItemSlot) => { const cardType = player.GetCard(pocketItemSlot); return cardTypesSet.has(cardType); }); } /** * Returns true for card types that have the following item config card type: * * - `ItemConfigCardType.TAROT` (0) * - `ItemConfigCardType.SUIT` (1) * - `ItemConfigCardType.SPECIAL` (3) * - `ItemConfigCardType.TAROT_REVERSE` (5) */ function isCard(cardType) { const itemConfigCardType = getItemConfigCardType(cardType); if (itemConfigCardType === undefined) { return false; } return itemConfigCardTypesForCards_1.ITEM_CONFIG_CARD_TYPES_FOR_CARDS.has(itemConfigCardType); } /** Returns whether the given card type matches the specified item config card type. */ function isCardType(cardType, itemConfigCardType) { return itemConfigCardType === getItemConfigCardType(cardType); } /** Returns true for any card type added by a mod. */ function isModdedCardType(cardType) { return !isVanillaCardType(cardType); } /** Returns true for card types that have `ItemConfigCardType.SPECIAL_OBJECT`. */ function isPocketItemObject(cardType) { return isCardType(cardType, isaac_typescript_definitions_1.ItemConfigCardType.SPECIAL_OBJECT); } /** Returns true for card types that have `ItemConfigCardType.TAROT_REVERSE`. */ function isReverseTarotCard(cardType) { return isCardType(cardType, isaac_typescript_definitions_1.ItemConfigCardType.TAROT_REVERSE); } /** Returns true for card types that have `ItemConfigCardType.RUNE`. */ function isRune(cardType) { return isCardType(cardType, isaac_typescript_definitions_1.ItemConfigCardType.RUNE); } /** Returns true for card types that have `ItemConfigCardType.SPECIAL`. */ function isSpecialCard(cardType) { return isCardType(cardType, isaac_typescript_definitions_1.ItemConfigCardType.SPECIAL); } /** Returns true for card types that have `ItemConfigCardType.SUIT`. */ function isSuitCard(cardType) { return isCardType(cardType, isaac_typescript_definitions_1.ItemConfigCardType.SUIT); } /** Returns true for card types that have `ItemConfigCardType.TAROT`. */ function isTarotCard(cardType) { return isCardType(cardType, isaac_typescript_definitions_1.ItemConfigCardType.TAROT); } function isValidCardType(cardType) { const potentialCardType = (0, types_1.asCardType)(cardType); const itemConfigCard = cachedClasses_1.itemConfig.GetCard(potentialCardType); return itemConfigCard !== undefined; } function isVanillaCardType(cardType) { return cardType <= constantsFirstLast_1.LAST_VANILLA_CARD_TYPE; } /** * Helper function to use a card without showing an animation and without the announcer voice * playing. */ function useCardTemp(player, cardType) { const useFlags = (0, flag_1.addFlag)(isaac_typescript_definitions_1.UseFlag.NO_ANIMATION, isaac_typescript_definitions_1.UseFlag.NO_ANNOUNCER_VOICE); player.UseCard(cardType, useFlags); }