"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.worldPositionToGridPositionFast = exports.worldPositionToGridPosition = exports.isValidGridPosition = exports.gridPositionToWorldPosition = exports.gridIndexToGridPosition = exports.gridCoordinatesToWorldPosition = void 0;
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);
}
exports.gridCoordinatesToWorldPosition = gridCoordinatesToWorldPosition;
/**
 * 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);
}
exports.gridIndexToGridPosition = gridIndexToGridPosition;
/**
 * 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);
}
exports.gridPositionToWorldPosition = gridPositionToWorldPosition;
/**
 * 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);
}
exports.isValidGridPosition = isValidGridPosition;
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);
}
exports.worldPositionToGridPosition = worldPositionToGridPosition;
/**
 * 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);
}
exports.worldPositionToGridPositionFast = worldPositionToGridPositionFast;
