package net.minecraft.src;

import net.lax1dude.eaglercraft.TextureLocation;
import net.lax1dude.eaglercraft.EaglerAdapter;

public class RenderCreeper extends RenderLiving
{
    private static final TextureLocation armoredCreeperTextures = new TextureLocation("textures/entity/creeper/creeper_armor.png");
    private static final TextureLocation creeperTextures = new TextureLocation("textures/entity/creeper/creeper.png");

    /** The creeper model. */
    private ModelBase creeperModel = new ModelCreeper(2.0F);

    public RenderCreeper()
    {
        super(new ModelCreeper(), 0.5F);
    }

    /**
     * Updates creeper scale in prerender callback
     */
    protected void updateCreeperScale(EntityCreeper par1EntityCreeper, float par2)
    {
        float var3 = par1EntityCreeper.getCreeperFlashIntensity(par2);
        float var4 = 1.0F + MathHelper.sin(var3 * 100.0F) * var3 * 0.01F;

        if (var3 < 0.0F)
        {
            var3 = 0.0F;
        }

        if (var3 > 1.0F)
        {
            var3 = 1.0F;
        }

        var3 *= var3;
        var3 *= var3;
        float var5 = (1.0F + var3 * 0.4F) * var4;
        float var6 = (1.0F + var3 * 0.1F) / var4;
        EaglerAdapter.glScalef(var5, var6, var5);
    }

    /**
     * Updates color multiplier based on creeper state called by getColorMultiplier
     */
    protected int updateCreeperColorMultiplier(EntityCreeper par1EntityCreeper, float par2, float par3)
    {
        float var4 = par1EntityCreeper.getCreeperFlashIntensity(par3);

        if ((int)(var4 * 10.0F) % 2 == 0)
        {
            return 0;
        }
        else
        {
            int var5 = (int)(var4 * 0.2F * 255.0F);

            if (var5 < 0)
            {
                var5 = 0;
            }

            if (var5 > 255)
            {
                var5 = 255;
            }

            short var6 = 255;
            short var7 = 255;
            short var8 = 255;
            return var5 << 24 | var6 << 16 | var7 << 8 | var8;
        }
    }

    /**
     * A method used to render a creeper's powered form as a pass model.
     */
    protected int renderCreeperPassModel(EntityCreeper par1EntityCreeper, int par2, float par3)
    {
        if (par1EntityCreeper.getPowered())
        {
            if (par1EntityCreeper.isInvisible())
            {
                EaglerAdapter.glDepthMask(false);
            }
            else
            {
                EaglerAdapter.glDepthMask(true);
            }

            if (par2 == 1)
            {
                float var4 = (float)par1EntityCreeper.ticksExisted + par3;
                this.bindTexture(armoredCreeperTextures);
                EaglerAdapter.glMatrixMode(EaglerAdapter.GL_TEXTURE);
                EaglerAdapter.glLoadIdentity();
                float var5 = var4 * 0.01F;
                float var6 = var4 * 0.01F;
                EaglerAdapter.glTranslatef(var5, var6, 0.0F);
                this.setRenderPassModel(this.creeperModel);
                EaglerAdapter.glMatrixMode(EaglerAdapter.GL_MODELVIEW);
                EaglerAdapter.glEnable(EaglerAdapter.GL_BLEND);
                float var7 = 0.5F;
                EaglerAdapter.glColor4f(var7, var7, var7, 1.0F);
                EaglerAdapter.glDisable(EaglerAdapter.GL_LIGHTING);
                EaglerAdapter.glBlendFunc(EaglerAdapter.GL_ONE, EaglerAdapter.GL_ONE);
                return 1;
            }

            if (par2 == 2)
            {
                EaglerAdapter.glMatrixMode(EaglerAdapter.GL_TEXTURE);
                EaglerAdapter.glLoadIdentity();
                EaglerAdapter.glMatrixMode(EaglerAdapter.GL_MODELVIEW);
                EaglerAdapter.glEnable(EaglerAdapter.GL_LIGHTING);
                EaglerAdapter.glDisable(EaglerAdapter.GL_BLEND);
            }
        }

        return -1;
    }

    protected int func_77061_b(EntityCreeper par1EntityCreeper, int par2, float par3)
    {
        return -1;
    }

    protected TextureLocation getCreeperTextures(EntityCreeper par1EntityCreeper)
    {
        return creeperTextures;
    }

    /**
     * Allows the render to do any OpenGL state modifications necessary before the model is rendered. Args:
     * entityLiving, partialTickTime
     */
    protected void preRenderCallback(EntityLivingBase par1EntityLivingBase, float par2)
    {
        this.updateCreeperScale((EntityCreeper)par1EntityLivingBase, par2);
    }

    /**
     * Returns an ARGB int color back. Args: entityLiving, lightBrightness, partialTickTime
     */
    protected int getColorMultiplier(EntityLivingBase par1EntityLivingBase, float par2, float par3)
    {
        return this.updateCreeperColorMultiplier((EntityCreeper)par1EntityLivingBase, par2, par3);
    }

    /**
     * Queries whether should render the specified pass or not.
     */
    protected int shouldRenderPass(EntityLivingBase par1EntityLivingBase, int par2, float par3)
    {
        return this.renderCreeperPassModel((EntityCreeper)par1EntityLivingBase, par2, par3);
    }

    @Override
    protected void bindTexture(EntityLivingBase par1EntityLiving) {
        creeperTextures.bindTexture();
    }

    protected int inheritRenderPass(EntityLivingBase par1EntityLivingBase, int par2, float par3)
    {
        return this.func_77061_b((EntityCreeper)par1EntityLivingBase, par2, par3);
    }

    /**
     * Returns the location of an entity's texture. Doesn't seem to be called unless you call Render.bindEntityTexture.
     */
    protected TextureLocation getEntityTexture(Entity par1Entity)
    {
        return this.getCreeperTextures((EntityCreeper)par1Entity);
    }
}
