UNPKG

isaacscript-common

Version:

Helper functions and features for IsaacScript mods.

97 lines (96 loc) 3.91 kB
"use strict"; /** * These functions have to do with the grid index inside of a room (i.e. the grid index that grid * entities use). * * For functions having to do with the room grid index of the level, see the "Level Grid" functions. * * @module */ Object.defineProperty(exports, "__esModule", { value: true }); exports.gridCoordinatesToWorldPosition = gridCoordinatesToWorldPosition; exports.gridIndexToGridPosition = gridIndexToGridPosition; exports.gridPositionToWorldPosition = gridPositionToWorldPosition; exports.isValidGridPosition = isValidGridPosition; exports.worldPositionToGridPosition = worldPositionToGridPosition; exports.worldPositionToGridPositionFast = worldPositionToGridPositionFast; const LRoomShapeToRectangles_1 = require("../objects/LRoomShapeToRectangles"); const math_1 = require("./math"); const roomShape_1 = require("./roomShape"); /** * Helper function to convert grid coordinates to a world position `Vector`. * * For example, the coordinates of (0, 0) are equal to `Vector(80, 160)`. */ function gridCoordinatesToWorldPosition(x, y) { const gridPosition = Vector(x, y); return gridPositionToWorldPosition(gridPosition); } /** * Helper function to convert a grid index to a grid position. * * For example, in a 1x1 room, grid index 0 is equal to "Vector(-1, -1) and grid index 16 is equal * to "Vector(0, 0)". */ function gridIndexToGridPosition(gridIndex, roomShape) { const gridWidth = (0, roomShape_1.getRoomShapeWidth)(roomShape); const x = (gridIndex % gridWidth) - 1; const y = Math.floor(gridIndex / gridWidth) - 1; return Vector(x, y); } /** * Helper function to convert a grid position `Vector` to a world position `Vector`. * * For example, the coordinates of (0, 0) are equal to `Vector(80, 160)`. */ function gridPositionToWorldPosition(gridPosition) { const x = (gridPosition.X + 2) * 40; const y = (gridPosition.Y + 4) * 40; return Vector(x, y); } /** * Test if a grid position is actually in the given `RoomShape`. * * In this context, the grid position of the top-left wall is "Vector(-1, -1)". */ function isValidGridPosition(gridPosition, roomShape) { return (0, roomShape_1.isLRoomShape)(roomShape) ? isValidGridPositionLRoom(gridPosition, roomShape) : isValidGridPositionNormal(gridPosition, roomShape); } function isValidGridPositionNormal(gridPosition, roomShape) { const topLeft = (0, roomShape_1.getRoomShapeTopLeftPosition)(roomShape); const bottomRight = (0, roomShape_1.getRoomShapeBottomRightPosition)(roomShape); return (0, math_1.inRectangle)(gridPosition, topLeft, bottomRight); } function isValidGridPositionLRoom(gridPosition, roomShape) { const rectangles = LRoomShapeToRectangles_1.L_ROOM_SHAPE_TO_RECTANGLES[roomShape]; if (rectangles === undefined) { return false; } const { verticalTopLeft, verticalBottomRight, horizontalTopLeft, horizontalBottomRight, } = rectangles; return ((0, math_1.inRectangle)(gridPosition, verticalTopLeft, verticalBottomRight) || (0, math_1.inRectangle)(gridPosition, horizontalTopLeft, horizontalBottomRight)); } /** * Helper function to convert a world position `Vector` to a grid position `Vector`. * * In this context, the grid position of the top-left wall is "Vector(-1, -1)". */ function worldPositionToGridPosition(worldPos) { const x = Math.round(worldPos.X / 40 - 2); const y = Math.round(worldPos.Y / 40 - 4); return Vector(x, y); } /** * Helper function to convert a world position `Vector` to a grid position `Vector`. * * In this context, the grid position of the top-left wall is "Vector(-1, -1)". * * This is similar to the `worldPositionToGridPosition` function, but the values are not rounded. */ function worldPositionToGridPositionFast(worldPos) { const x = worldPos.X / 40 - 2; const y = worldPos.Y / 40 - 4; return Vector(x, y); }