extend("fiskheroes:hero_basic");
loadTextures({
 "layer1": "pack:warmachine_initiative_layer1",
    "layer2": "pack:warmachine_initiative_layer2",
    "lights": "pack:warmachine_initiative_glow",
    "suit": "fiskheroes:iron_man_mk45_suit.tx.json",
    "jetpack": "pack:warmachine_initiative_jetpack",
    "mask": "pack:warmachine_initiative_mask.tx.json",
		 "warpack": "pack:warpack"
});

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

var helmet;
var boosters;
var tubes;
var jetpack;
var metal_heat;

function init(renderer) {
    parent.init(renderer);
    renderer.setTexture((entity, renderLayer) => {
        if (renderLayer == "HELMET" && hasFoldingHelmet() && entity.getInterpolatedData("fiskheroes:mask_open_timer2") > 0) {
            return "layer2";
        } 
        return entity.getData("fiskheroes:suit_open_timer") > 0 ? "suit" : renderLayer == "LEGGINGS" ? "layer2" : "layer1";
    });
    renderer.setLights((entity, renderLayer) => {
        if (renderLayer == "HELMET" && entity.getInterpolatedData("fiskheroes:mask_open_timer2") > 0) {
            return null;
        }
        return entity.getData('fiskheroes:suit_open_timer') > 0 ? "lights_suit" : renderLayer == "LEGGINGS" ? "lights_layer2" : "lights_layer1";
    });
	 renderer.setLights((entity, renderLayer) => {
        if (renderLayer == "HELMET") {
            return entity.getData('fiskheroes:mask_open_timer') == 0 ? "lights" : null;
        }
        return "lights";
    });
	
	    var forcefield = renderer.bindProperty("fiskheroes:forcefield");
    forcefield.color.set(0xa3ff0f);
    forcefield.setShape(36, 18).setOffset(0.0, 6.0, 0.0).setScale(1.25);
    forcefield.setCondition(entity => {
        forcefield.opacity = entity.getInterpolatedData("fiskheroes:shield_blocking_timer") * 0.15;
        return true;
    });
	
	    utils.bindBeam(renderer, "fiskheroes:charged_beam", "fiskheroes:charged_beam", "head", 0xa3ff0f, [
 {    "offset": [6.0, -2.5, 0.0], "size": [2.0, 2.0]}
    ]).setParticles(renderer.createResource("PARTICLE_EMITTER", "fiskheroes:impact_charged_beam"));
}

function initEffects(renderer) {


 tubes = renderer.createEffect("fiskheroes:model");
    tubes.setModel(utils.createModel(renderer, "pack:war_jetpack", "warpack", null));
    tubes.anchor.set("head");

	
	
    jetpack = renderer.createEffect("fiskheroes:model");
    jetpack.setModel(utils.createModel(renderer, "fiskheroes:falcon_jetpack", "jetpack", null));
    jetpack.anchor.set("body");

    if (hasFoldingHelmet()) {
        helmet = iron_man_helmet.createFolding(renderer, "mask", "mask_lights", "fiskheroes:mask_open_timer2");
    }
    else {
        helmet = iron_man_helmet.createFaceplate(renderer, "mask", null);
    }

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

    metal_heat = renderer.createEffect("fiskheroes:metal_heat");
    metal_heat.includeEffects(helmet.effect);

    utils.addCameraShake(renderer, 0.015, 1.5, "fiskheroes:flight_boost_timer");
    var shake = renderer.bindProperty("fiskheroes:camera_shake").setCondition(entity => {
        shake.factor = entity.isSprinting() && entity.getData("fiskheroes:flying") ? 0.3 * Math.sin(Math.PI * entity.getInterpolatedData("fiskheroes:flight_boost_timer")) : 0;
        return true;
    });
    shake.intensity = 0.05;


    utils.bindParticles(renderer, "pack:ironman").setCondition(entity => entity.getData("fiskheroes:flying"));



    utils.bindBeam(renderer, "fiskheroes:repulsor_blast", "fiskheroes:repulsor_blast", "rightArm", 0xFFC462, [
 { "firstPerson": [-3.75, 3.0, -8.0], "offset":[-1.5, 9.0, 0.0], "size": [0.5, 0.5] },
        { "firstPerson": [3.75, 3.0, -8.0], "offset": [-1.5, 9.0, 0.0], "size": [0.5, 0.5], "anchor": "leftArm" }
    ]);
}

function initAnimations(renderer) {
    parent.initAnimations(renderer);
	
	
	    renderer.removeCustomAnimation("basic.AIMING");
    renderer.removeCustomAnimation("basic.ENERGY_PROJ");
    renderer.removeCustomAnimation("basic.CHARGED_BEAM");

    addAnimation(renderer, "basic.AIMING", "fiskheroes:dual_aiming").setData((entity, data) => {
        var charge = entity.getInterpolatedData("fiskheroes:beam_charge");
        data.load(Math.max(entity.getInterpolatedData("fiskheroes:aiming_timer")));
    });
	
	
	
    utils.addFlightAnimationWithLanding(renderer, "iron_man.FLIGHT", "fiskheroes:flight/iron_man.anim.json");
    utils.addHoverAnimation(renderer, "iron_man.HOVER", "fiskheroes:flight/idle/iron_man");
    utils.addAnimationEvent(renderer, "FLIGHT_DIVE", "fiskheroes:iron_man_dive");

    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 hasFoldingHelmet() {
    return false;
}

function hasBootLights() {
    return true;
}

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

    if (!isFirstPersonArm) {
		   jetpack.render();
		     tubes.render();
		
         if (renderLayer == "HELMET") {
            if (hasFoldingHelmet()) {
                helmet.render(entity);
            }
            else {
                helmet.render(entity.getInterpolatedData("fiskheroes:mask_open_timer2"));
            }
        }
    }

    metal_heat.opacity = entity.getInterpolatedData("fiskheroes:metal_heat");
    metal_heat.render();
}
