extend("fiskheroes:hero_basic");
loadTextures({
    "base": "pkg:lex_luthor_suit",
    "layer2": "pkg:lex_luthor_suit",
    "suit": "pkg:lex_luthor_suit.tx.json",
    "lights": "pkg:lex_luthor_lights",
    "shield": "pkg:superior_iron_man_shield",
    "shield_handle": "pkg:superior_iron_man_shield_handle",
    "shield_handle_lights": "pkg:superior_iron_man_shield_handle_lights",
    "null": "pkg:null"
});

var utils = implement("fiskheroes:external/utils");
var iron_man_boosters = implement("fiskheroes:external/iron_man_boosters");

var boosters;

var color = 0x00750E;
var shield;
var shield_handle;
var repulsor;

function init(renderer) {
    parent.init(renderer);
    renderer.setLights((entity, renderLayer) => renderLayer == "CHESTPLATE" ? "lights" : null);
    renderer.setTexture((entity, renderLayer) => renderLayer == "CHESTPLATE" ? "base" : null);
    renderer.showModel("CHESTPLATE", "head", "headwear", "body", "rightArm", "leftArm", "rightLeg", "leftLeg");
    renderer.fixHatLayer("CHESTPLATE");
}


function initEffects(renderer) {
    repulsor = renderer.createEffect("fiskheroes:overlay");

    boosters = iron_man_boosters.create(renderer, "pkg:repulsor_layer_%s", true);

    utils.addCameraShake(renderer, 0.015, 1.5, "fiskheroes:flight_boost_timer");

    shield = renderer.createEffect("fiskheroes:shield");
    shield.texture.set(null, "shield");
    shield.anchor.set("rightArm");
    shield.setRotation(55.0, -20.0, -10.0).setCurve(15.0, 50.0);
    shield.large = true;
    shield_handle = renderer.createEffect("fiskheroes:shield");
    shield_handle.texture.set("shield_handle", "shield_handle_lights");
    shield_handle.anchor.set("rightArm");
    shield_handle.setOffset(3.5, 7.0, -3.0).setRotation(55.0, -20.0, 20.0).setCurve(100.0, 45.0);

    // Aim
    utils.bindBeam(renderer, "fiskheroes:repulsor_blast", "pkg:low_beam", "rightArm", 0x00750E, [
        { "firstPerson": [-4.5, 3.75, -7.0], "offset": [-0.5, 9.0, 0.0], "size": [1.5, 1.5] }
    ]);

    var beam = renderer.createResource("BEAM_RENDERER", "pkg:low_beam");
    utils.bindBeam(renderer, "fiskheroes:energy_projection", beam, "body", color, [
        { "firstPerson": [0.0, 6.0, 0.0], "offset": [0.0, 5.0, -4.0], "size": [6.0, 6.0] }
    ]).setParticles(renderer.createResource("PARTICLE_EMITTER", "fiskheroes:impact_energy_projection"));
}

function initAnimations(renderer) {
    parent.initAnimations(renderer);
    utils.addFlightAnimationWithLanding(renderer, "iron_man.FLIGHT", "fiskheroes:flight/iron_man.anim.json");
    utils.addHoverAnimation(renderer, "iron_man.HOVER", "fiskheroes:flight/idle/iron_man");
    addAnimationWithData(renderer, "iron_man.LAND", "fiskheroes:superhero_landing", "fiskheroes:dyn/superhero_landing_timer")
        .priority = -8;

    addAnimationWithData(renderer, "iron_man.ROLL", "fiskheroes:flight/barrel_roll", "fiskheroes:barrel_roll_timer")
        .priority = 10;

}

function render(entity, renderLayer, isFirstPersonArm) {
    boosters.render(entity, renderLayer, isFirstPersonArm, false);

    if (renderLayer == "CHESTPLATE") {
        repulsor.opacity = entity.getInterpolatedData("fiskheroes:dyn/booster_r_timer");
        repulsor.texture.set(null, "repulsor");
        repulsor.render();
        repulsor.opacity = entity.getInterpolatedData("fiskheroes:dyn/booster_l_timer");
        repulsor.texture.set(null, "repulsor_left");
        repulsor.render();
    }
    else if (renderLayer == "BOOTS") {
        repulsor.opacity = entity.getInterpolatedData("fiskheroes:dyn/booster_timer");
        repulsor.texture.set(null, "repulsor_boots");
        repulsor.render();
    }
    shield.unfold = entity.getInterpolatedData("fiskheroes:shield_blocking_timer");
    shield.opacity = 0.25 + 0.25 * shield.unfold * shield.unfold;
    shield.setOffset(3.75 + 2.25 * shield.unfold, 8.75 + 1.5 * shield.unfold, -0.75 * shield.unfold);
    shield.render();

    var shield_timer = entity.getInterpolatedData("fiskheroes:shield_timer");
    shield_handle.unfold = entity.getData("fiskheroes:shield") ? Math.min(shield_timer * 2, 1) : Math.max(shield_timer * 2 - 1, 0);
    shield_handle.render();
}