extend("fiskheroes:hero_basic");
loadTextures({
    "layer1": "tgheroes:doctor_doom/doom2099/doctor_doom_2099_layer1",
    "layer2": "tgheroes:doctor_doom/doom2099/doctor_doom_2099_layer2",
    "cape": "tgheroes:doctor_doom/doom2099/doctor_doom_2099_cape"
});

var utils = implement("fiskheroes:external/utils");
var spell;
var telekinesis;
var capes = implement("fiskheroes:external/capes");
var cape;
var booster_boots;
var booster_back;
var boosters = implement("fiskheroes:external/bloom_booster");

function initEffects(renderer) {
    var physics = renderer.createResource("CAPE_PHYSICS", null);
    physics.maxFlare = 0.3;
    physics.flareDegree = 1.5;
    physics.flareFactor = 1.2;
    physics.flareElasticity = 5;
    cape = capes.createDefault(renderer, 24, "fiskheroes:cape_default.mesh.json", physics);
    cape.effect.texture.set("cape");
    cape.effect.width = 16;

    var color = 0x470718;
    var tao_mandala = renderer.createResource("SHAPE", "fiskheroes:tao_mandala");
    var beam = renderer.createResource("BEAM_RENDERER", "fiskheroes:line");
    spell = renderer.createEffect("fiskheroes:lines").setShape(tao_mandala).setRenderer(beam);
    spell.color.set(color);
    spell.setOffset(1.0, 8.0, 0.0).setScale(3.2);
    spell.anchor.set("rightArm");
    spell.mirror = true;

    var magic = renderer.bindProperty("fiskheroes:spellcasting");
    magic.colorGeneric.set(0xa6f0035);
    magic.colorEarthCrack.set(0xaa31037);
    magic.colorAtmosphere.set(0xa6f0035);
    magic.colorWhip.set(color);

	var forcefield = renderer.bindProperty("fiskheroes:forcefield");
    forcefield.color.set(0x470718);
    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;
    });
	
	var beam = renderer.createResource("BEAM_RENDERER", "tgheroes:doom_beam");
    utils.bindBeam(renderer, "fiskheroes:charged_beam", beam, "rightArm", color, [
        {"firstPerson": [-4.5, 3.75, -7.0], "offset": [0.0, 0.0, 0.0], "size": [4.0, 4.0] }
    ]).setParticles(renderer.createResource("PARTICLE_EMITTER", "fiskheroes:impact_charged_beam"));
	
	telekinesis=renderer.createEffect("fiskheroes:glowerlay");
	
	utils.bindCloud(renderer, "fiskheroes:telekinesis", "tgheroes:doom_2099_telekinesis");
    utils.bindCloud(renderer, "fiskheroes:teleportation", "tgheroes:doom_2099_teleport");
	
	var beam1 = renderer.createResource("BEAM_RENDERER", "tgheroes:doom_booster");
    var rp_fire = renderer.createResource("ICON", "tgheroes:pr_fire_layer_0");
    var color = 0x470736;
    booster_boots = boosters.create(renderer, "rightLeg", true, color, rp_fire, beam1).setOffset(0.0, 7.0, 2.0).setSize(1.5, 3.0);
    booster_back = boosters.create(renderer, "body", true, color, rp_fire, beam1).setOffset(1.5, 6.0, 1.75).setRotation(15.0, 0.0, 5.0);
}

function initAnimations(renderer) {
    parent.initAnimations(renderer);
    renderer.removeCustomAnimation("basic.BLOCKING");
    renderer.removeCustomAnimation("basic.CHARGED_BEAM");
    renderer.removeCustomAnimation("basic.BLOCKING");
    addAnimationWithData(renderer, "basic.BLOCKING", "fiskheroes:dual_aiming", "fiskheroes:shield_blocking_timer")
        .priority = -5;
    addAnimationWithData(renderer, "antimonitor.ANTIBLAST", "fiskheroes:aiming", "fiskheroes:beam_charge");
    
	var beam1 = renderer.createResource("BEAM_RENDERER", "tgheroes:doom_booster");
	utils.addHoverAnimation(renderer, "atom.HOVER", "fiskheroes:flight/idle/default_back");
    utils.addFlightAnimation(renderer, "atom.FLIGHT", "fiskheroes:flight/propelled.anim.json", (entity, data) => {
        data.load(0, entity.getInterpolatedData("fiskheroes:flight_timer"));
        data.load(1, entity.getInterpolatedData("fiskheroes:flight_boost_timer"));
        data.load(2, entity.getInterpolatedData("fiskheroes:scale"));
    });
}

function render(entity, renderLayer, isFirstPersonArm) {
    if (!isFirstPersonArm) {
  if (renderLayer == "CHESTPLATE") {
            var boost = entity.getInterpolatedData("fiskheroes:flight_boost_timer");
            booster_back.speedScale = 0.5 * boost;
            booster_back.flutter = 1 + boost;
    
            var f = Math.min(Math.max(boost * 3 - 1.25, 0), 1);
            f = entity.isSprinting() ? 0.5 - Math.cos(2 * f * Math.PI) / 2 : 0;
            booster_back.setSize(1.5 + f * 2, 3.5 - f * 2);
            booster_back.render(entity, entity.getInterpolatedData("fiskheroes:dyn/booster_timer"));
        }
        else if (renderLayer == "BOOTS") {
            var boost = entity.getInterpolatedData("fiskheroes:flight_boost_timer");
            booster_boots.speedScale = 0.5 * boost;
            booster_boots.flutter = 1 + boost;

            booster_boots.setRotation(15 - 10 * boost, 0, 0);
            booster_boots.render(entity, entity.getInterpolatedData("fiskheroes:dyn/booster_timer"));
        }
		        if (renderLayer == "CHESTPLATE") {
            cape.render(entity);
        }
    }
}