isaacscript-common
Version:
Helper functions and features for IsaacScript mods.
148 lines (147 loc) • 5.61 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getHUDOffsetVector = getHUDOffsetVector;
exports.getHeartRowLength = getHeartRowLength;
exports.getHeartsUIWidth = getHeartsUIWidth;
exports.getScreenBottomCenterPos = getScreenBottomCenterPos;
exports.getScreenBottomLeftPos = getScreenBottomLeftPos;
exports.getScreenBottomRightPos = getScreenBottomRightPos;
exports.getScreenBottomY = getScreenBottomY;
exports.getScreenCenterPos = getScreenCenterPos;
exports.getScreenRightX = getScreenRightX;
exports.getScreenTopCenterPos = getScreenTopCenterPos;
exports.getScreenTopLeftPos = getScreenTopLeftPos;
exports.getScreenTopRightPos = getScreenTopRightPos;
exports.getVisibleHearts = getVisibleHearts;
const isaac_typescript_definitions_1 = require("isaac-typescript-definitions");
const cachedClasses_1 = require("../core/cachedClasses");
const constants_1 = require("../core/constants");
const vector_1 = require("./vector");
/**
* In the options menu, players have the ability to set a HUD offset (which gets written to the
* `HudOffset` attribute in the "options.ini" file). This function uses the current HUD offset to
* generate a vector that should be added to the corresponding position that you want to draw a UI
* element at.
*
* For example:
* - If the user does not have a HUD offset configured, this function will return `Vector(0, 0)`.
* - If the user has a HUD offset of 1.0 configured, this function will return `Vector(20, 12)`.
*/
function getHUDOffsetVector() {
// Convert e.g. 0.4 to 4.
const hudOffset = Math.floor(Options.HUDOffset * 10);
// Expected values are integers between 1 and 10.
if (hudOffset < 1 || hudOffset > 10) {
return (0, vector_1.copyVector)(constants_1.VectorZero);
}
const x = hudOffset * 2;
let y = hudOffset;
if (y >= 4) {
y++;
}
if (y >= 9) {
y++;
}
return Vector(x, y);
}
/**
* Returns how many hearts are in the heart UI row. If the player has more than 6 hearts, this
* function will return 6.
*/
function getHeartRowLength(player) {
const maxHearts = player.GetMaxHearts();
const soulHearts = player.GetSoulHearts();
const boneHearts = player.GetBoneHearts();
const brokenHearts = player.GetBrokenHearts();
// There are no half bone hearts or half broken hearts.
const combinedHearts = maxHearts + soulHearts + boneHearts * 2 + brokenHearts * 2;
const heartRowLength = combinedHearts / 2;
// After 6 hearts, the hearts wrap to a second row.
return Math.min(heartRowLength, 6);
}
/**
* Helper function to get the width of the first player's hearts on the UI. This is useful for
* drawing UI elements to the right of where the player's hearts are. Make sure to use this in
* combination with the `getHUDOffsetVector` helper function.
*/
function getHeartsUIWidth() {
const level = cachedClasses_1.game.GetLevel();
const curses = level.GetCurses();
const player = Isaac.GetPlayer();
const extraLives = player.GetExtraLives();
const effects = player.GetEffects();
const hasHolyMantleEffect = effects.HasCollectibleEffect(isaac_typescript_definitions_1.CollectibleType.HOLY_MANTLE);
let heartRowLength = getHeartRowLength(player);
if (hasHolyMantleEffect) {
heartRowLength++;
}
if (curses === isaac_typescript_definitions_1.LevelCurse.UNKNOWN) {
heartRowLength = 1;
}
let width = heartRowLength * constants_1.UI_HEART_WIDTH;
if (extraLives > 9) {
width += 20;
if (player.HasCollectible(isaac_typescript_definitions_1.CollectibleType.GUPPYS_COLLAR)) {
width += 6;
}
}
else if (extraLives > 0) {
width += 16;
if (player.HasCollectible(isaac_typescript_definitions_1.CollectibleType.GUPPYS_COLLAR)) {
width += 4;
}
}
return width;
}
function getScreenBottomCenterPos() {
const bottomRightPos = getScreenBottomRightPos();
return Vector(bottomRightPos.X / 2, bottomRightPos.Y);
}
function getScreenBottomLeftPos() {
const bottomRightPos = getScreenBottomRightPos();
return Vector(0, bottomRightPos.Y);
}
function getScreenBottomRightPos() {
const screenWidth = Isaac.GetScreenWidth();
const screenHeight = Isaac.GetScreenHeight();
return Vector(screenWidth, screenHeight);
}
function getScreenBottomY() {
const bottomRightPos = getScreenBottomRightPos();
return bottomRightPos.Y;
}
function getScreenCenterPos() {
const bottomRightPos = getScreenBottomRightPos();
return bottomRightPos.div(2);
}
function getScreenRightX() {
const bottomRightPos = getScreenBottomRightPos();
return bottomRightPos.X;
}
function getScreenTopCenterPos() {
const bottomRightPos = getScreenBottomRightPos();
return Vector(bottomRightPos.X / 2, 0);
}
function getScreenTopLeftPos() {
return (0, vector_1.copyVector)(constants_1.VectorZero);
}
function getScreenTopRightPos() {
const bottomRightPos = getScreenBottomRightPos();
return Vector(bottomRightPos.X, 0);
}
/**
* Get how many hearts are currently being shown on the hearts UI.
*
* This function is originally from piber20 Helper.
*/
function getVisibleHearts(player) {
const effectiveMaxHearts = player.GetEffectiveMaxHearts();
const soulHearts = player.GetSoulHearts();
const boneHearts = player.GetBoneHearts();
const maxHearts = Math.max(effectiveMaxHearts, boneHearts * 2);
let visibleHearts = Math.ceil((maxHearts + soulHearts) / 2);
if (visibleHearts < 1) {
visibleHearts = 1;
}
return visibleHearts;
}