extend("fiskheroes:hero_basic");
loadTextures({
	"punch": "skarredheroes:gwenpool/soundbubbles/punch.tx.json",
	"sword": "skarredheroes:gwenpool/soundbubbles/sword.tx.json",
    "pizza": "skarredheroes:gwenpool/soundbubbles/other/pizza",
    "die": "skarredheroes:gwenpool/soundbubbles/die.tx.json",
	"water": "skarredheroes:gwenpool/soundbubbles/water.tx.json",
    "sleep1": "skarredheroes:gwenpool/soundbubbles/sleep/zzz",
    "sleep2": "skarredheroes:gwenpool/soundbubbles/sleep/zzz2",
    "sleep3": "skarredheroes:gwenpool/soundbubbles/sleep/zzz3",
    "sleep4": "skarredheroes:gwenpool/soundbubbles/sleep/zzz4",
    "sleep5": "skarredheroes:gwenpool/soundbubbles/sleep/zzz5",
	"gun": "skarredheroes:gwenpool/soundbubbles/gun.tx.json"
});
var utils = implement("fiskheroes:external/utils");

var pos1;
var pos2;
var pos3;
var pos4;
var pos5;
var pos6;
var pos7;
var pos8;
var pos9;

function init(renderer) {
    parent.init(renderer);
}

function initEffects(renderer) {
    pos1 = renderer.createEffect("fiskheroes:shield");
    pos1.anchor.set("body");
    pos1.setCurve(0.0, 0.0);
    pos1.setOffset(14.0, -14.0, 8.0);
	pos1.setRotation(0.0, -90.0, 0.0);
    pos1.large = true;
	pos1.setScale(2);
	
    pos2 = renderer.createEffect("fiskheroes:shield");
    pos2.anchor.set("body");
    pos2.setCurve(0.0, 0.0);
    pos2.setOffset(-14.0, -14.0, 8.0);
	pos2.setRotation(0.0, -90.0, 0.0);
    pos2.large = true;
	pos2.setScale(2);
	
    pos3 = renderer.createEffect("fiskheroes:shield");
    pos3.anchor.set("body");
    pos3.setCurve(0.0, 0.0);
    pos3.setOffset(14.0, 14.0, 8.0);
	pos3.setRotation(0.0, -90.0, 0.0);
    pos3.large = true;
	pos3.setScale(2);
	
    pos4 = renderer.createEffect("fiskheroes:shield");
    pos4.anchor.set("body");
    pos4.setCurve(0.0, 0.0);
    pos4.setOffset(-14.0, 14.0, 8.0);
	pos4.setRotation(0.0, -90.0, 0.0);
    pos4.large = true;
	pos4.setScale(2);
	
    pos5 = renderer.createEffect("fiskheroes:shield");
    pos5.anchor.set("body");
    pos5.setCurve(0.0, 0.0);
    pos5.setOffset(16.0, 5.0, 8.0);
	pos5.setRotation(0.0, -90.0, 0.0);
    pos5.large = true;
	pos5.setScale(2);

    pos6 = renderer.createEffect("fiskheroes:shield");
    pos6.anchor.set("body");
    pos6.setCurve(0.0, 0.0);
    pos6.setOffset(-16.0, 5.0, 8.0);
	pos6.setRotation(0.0, -90.0, 0.0);
    pos6.large = true;
	pos6.setScale(2);
	
    pos7 = renderer.createEffect("fiskheroes:shield");
    pos7.anchor.set("body");
    pos7.setCurve(0.0, 0.0);
    pos7.setOffset(0.0, -18.0, 8.0);
	pos7.setRotation(0.0, -90.0, 0.0);
    pos7.large = true;
	pos7.setScale(2);
	
    pos8 = renderer.createEffect("fiskheroes:shield");
    pos8.anchor.set("body");
    pos8.setCurve(0.0, 0.0);
    pos8.setOffset(16.0, -5.0, 8.0);
	pos8.setRotation(0.0, -90.0, 0.0);
    pos8.large = true;
	pos8.setScale(2);
	
    pos9 = renderer.createEffect("fiskheroes:shield");
    pos9.anchor.set("body");
    pos9.setCurve(0.0, 0.0);
    pos9.setOffset(-16.0, -5.0, 8.0);
	pos9.setRotation(0.0, -90.0, 0.0);
    pos9.large = true;
	pos9.setScale(2);
}

function render(entity, renderLayer, isFirstPersonArm) {
    if (renderLayer == "CHESTPLATE" && !isFirstPersonArm) {
		var t = entity.getData("fiskheroes:ticks_since_gunshot");
		var firing = (t & 255) < 5 || ((t >> 8) & 255) < 5;
		
		var item = entity.getHeldItem();
		var nbt = item.nbt();
		var ammoType = nbt.getShort('AmmoType');
		
        var propellent = ammoType & 31;
		var casing = (ammoType >> 5) & 31;
		var tip = (ammoType >> 10) & 31;
		
		var texture;
		var position;
		
		texture = null;
		
		if (!firing && entity.getHeldItem().name() == 'fiskheroes:boxed_pizza' && entity.isPunching()) {
			texture = "pizza";
			position = 'position_punch';
		} else if ((entity.getHeldItem().isEmpty() || entity.getHeldItem().isWeapon()) && !firing && entity.isPunching() && entity.getHeldItem().name() != 'fiskheroes:katana') {
			texture = "punch";
			position = 'position_punch';
        } else if ((entity.isPunching() || entity.getData('fiskheroes:is_swing_in_progress')) && entity.getHeldItem().name() == 'fiskheroes:katana') {
			texture = "sword";
			position = 'position_punch';
		} else if (((Math.abs(entity.motionX()) > 0) || (Math.abs(entity.motionY()) > 0.05) || (Math.abs(entity.motionZ()) > 0)) && !firing && entity.isInWater() && entity.world().getBlock(entity.pos().add(0, -0.463, 0)) != 'minecraft:bed' && entity.isAlive() && !entity.isPunching() && !entity.getData('fiskheroes:is_swing_in_progress')) {
			texture = "water";
			position = 'position_water';
		} else if (!entity.isAlive()) {
			texture = "die";
			position = 'position_die';
		} else if (firing) {
			texture = "gun";
			position = 'position_gun';
		} else {
			texture = null;
		}
		
        if (!firing && !entity.isPunching() && entity.world().getBlock(entity.pos().add(0, -0.463, 0)) == 'minecraft:bed' && entity.isAlive()) {
            if (entity.ticksExisted() % 120 >= 0 && entity.ticksExisted() % 120 < 20) {
				pos2.texture.set(null, "sleep1");
				pos2.render();
			} else if (entity.ticksExisted() % 120 >= 20 && entity.ticksExisted() % 120 < 40) {
				pos2.texture.set(null, "sleep2");
				pos2.render();
			} else if (entity.ticksExisted() % 120 >= 40 && entity.ticksExisted() % 120 < 60) {
				pos2.texture.set(null, "sleep3");
				pos2.render();
			} else if (entity.ticksExisted() % 120 >= 60 && entity.ticksExisted() % 120 < 80) {
				pos2.texture.set(null, "sleep4");
				pos2.render();
			} else if (entity.ticksExisted() % 120 >= 80 && entity.ticksExisted() % 120 < 100) {
				pos2.texture.set(null, "sleep5");
				pos2.render();
			}
        }
		
		if (!entity.isDisplayStand() && !entity.isBookPlayer() && typeof position !== "undefined" ) {
			if (entity.getData('skarredheroes:dyn/' + position) == 0) {
				pos1.texture.set(null, texture);
				pos1.render();
			} else if (entity.getData('skarredheroes:dyn/' + position) == 1) {
				pos2.texture.set(null, texture);
				pos2.render();
			} else if (entity.getData('skarredheroes:dyn/' + position) == 2) {
				pos3.texture.set(null, texture);
				pos3.render();
			} else if (entity.getData('skarredheroes:dyn/' + position) == 3) {
				pos4.texture.set(null, texture);
				pos4.render();
			} else if (entity.getData('skarredheroes:dyn/' + position) == 4) {
				pos5.texture.set(null, texture);
				pos5.render();
			} else if (entity.getData('skarredheroes:dyn/' + position) == 5) {
				pos6.texture.set(null, texture);
				pos6.render();
			} else if (entity.getData('skarredheroes:dyn/' + position) == 6) {
				pos7.texture.set(null, texture);
				pos7.render();
			} else if (entity.getData('skarredheroes:dyn/' + position) == 7) {
				pos8.texture.set(null, texture);
				pos8.render();
			} else if (entity.getData('skarredheroes:dyn/' + position) == 8) {
				pos9.texture.set(null, texture);
				pos9.render();
			}
		}
	}
}