"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.setShadows = void 0;
const isaac_typescript_definitions_1 = require("isaac-typescript-definitions");
const cachedClasses_1 = require("../../../../core/cachedClasses");
const LadderSubTypeCustom_1 = require("../../../../enums/LadderSubTypeCustom");
const array_1 = require("../../../../functions/array");
const entitiesSpecific_1 = require("../../../../functions/entitiesSpecific");
const string_1 = require("../../../../functions/string");
const constants_1 = require("./constants");
/**
 * Normally, we would make a custom entity to represent a shadow effect, but we don't want to
 * interfere with the "entities2.xml" file in end-user mods. Thus, we must select a vanilla effect
 * to masquerade as a backdrop effect.
 *
 * We arbitrarily choose a ladder for this purpose because it will not automatically despawn after
 * time passes, like most other effects.
 */
const SHADOW_EFFECT_VARIANT = isaac_typescript_definitions_1.EffectVariant.LADDER;
const SHADOW_EFFECT_SUB_TYPE = LadderSubTypeCustom_1.LadderSubTypeCustom.CUSTOM_SHADOW;
/** The animation comes from StageAPI. */
const ROOM_SHAPE_TO_SHADOW_ANIMATION = {
    [isaac_typescript_definitions_1.RoomShape.SHAPE_1x1]: "1x1",
    [isaac_typescript_definitions_1.RoomShape.IH]: "1x1",
    [isaac_typescript_definitions_1.RoomShape.IV]: "1x1",
    [isaac_typescript_definitions_1.RoomShape.SHAPE_1x2]: "1x2",
    [isaac_typescript_definitions_1.RoomShape.IIV]: "1x2",
    [isaac_typescript_definitions_1.RoomShape.SHAPE_2x1]: "2x1",
    [isaac_typescript_definitions_1.RoomShape.IIH]: "2x1",
    [isaac_typescript_definitions_1.RoomShape.SHAPE_2x2]: "2x2",
    [isaac_typescript_definitions_1.RoomShape.LTL]: "2x2",
    [isaac_typescript_definitions_1.RoomShape.LTR]: "2x2",
    [isaac_typescript_definitions_1.RoomShape.LBL]: "2x2",
    [isaac_typescript_definitions_1.RoomShape.LBR]: "2x2", // 12
};
const FADED_BLACK = Color(0, 0, 0, 0.25);
function setShadows(customStage) {
    if (customStage.shadows === undefined) {
        return;
    }
    const room = cachedClasses_1.game.GetRoom();
    const roomShape = room.GetRoomShape();
    const centerPos = room.GetCenterPos();
    const animation = ROOM_SHAPE_TO_SHADOW_ANIMATION[roomShape];
    const shadows = customStage.shadows[animation];
    if (shadows === undefined) {
        return;
    }
    // We spawn an effect instead of simply rendering a static sprite so that the effect will properly
    // slide in during a room transition animation. (It looks stupid if the shadow stays statically
    // rendering throughout this animation.)
    const seed = 1;
    const shadowEffect = (0, entitiesSpecific_1.spawnEffectWithSeed)(SHADOW_EFFECT_VARIANT, SHADOW_EFFECT_SUB_TYPE, centerPos, seed);
    const sprite = shadowEffect.GetSprite();
    sprite.Load(`${constants_1.ISAACSCRIPT_CUSTOM_STAGE_GFX_PATH}/stage-shadow.anm2`, false);
    const decorationSeed = room.GetDecorationSeed();
    const shadow = (0, array_1.getRandomArrayElement)(shadows, decorationSeed);
    const pngPath = (0, string_1.removeCharactersBefore)(shadow.pngPath, "gfx/");
    sprite.ReplaceSpritesheet(0, pngPath);
    sprite.LoadGraphics();
    sprite.SetFrame(animation, 0);
    sprite.Color =
        shadow.color === undefined
            ? FADED_BLACK
            : Color(shadow.color.r, shadow.color.g, shadow.color.b, shadow.color.a);
}
exports.setShadows = setShadows;
