zeldaswordskills.client.render.entity.RenderCustomArrow.java Source code

Java tutorial

Introduction

Here is the source code for zeldaswordskills.client.render.entity.RenderCustomArrow.java

Source

/**
Copyright (C) <2015> <coolAlias>
    
This file is part of coolAlias' Zelda Sword Skills Minecraft Mod; as such,
you can redistribute it and/or modify it under the terms of the GNU
General Public License as published by the Free Software Foundation,
either version 3 of the License, or (at your option) any later version.
    
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
    
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

package zeldaswordskills.client.render.entity;

import net.minecraft.client.renderer.Tessellator;
import net.minecraft.client.renderer.entity.Render;
import net.minecraft.entity.Entity;
import net.minecraft.util.MathHelper;
import net.minecraft.util.ResourceLocation;

import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL12;

import zeldaswordskills.entity.projectile.EntityArrowBomb;
import zeldaswordskills.entity.projectile.EntityArrowCustom;
import zeldaswordskills.entity.projectile.EntityArrowElemental;
import zeldaswordskills.ref.ModInfo;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;

@SideOnly(Side.CLIENT)
public class RenderCustomArrow extends Render {
    private static final ResourceLocation vanillaArrow = new ResourceLocation(ModInfo.ID,
            "textures/entity/arrow.png");
    private static final ResourceLocation bombArrow = new ResourceLocation(ModInfo.ID,
            "textures/entity/arrow_bomb.png");
    private static final ResourceLocation fireArrow = new ResourceLocation(ModInfo.ID,
            "textures/entity/arrow_fire.png");
    private static final ResourceLocation iceArrow = new ResourceLocation(ModInfo.ID,
            "textures/entity/arrow_ice.png");
    private static final ResourceLocation lightArrow = new ResourceLocation(ModInfo.ID,
            "textures/entity/arrow_light.png");

    public RenderCustomArrow() {
    }

    public void renderArrow(EntityArrowCustom arrow, double dx, double dy, double dz, float yaw,
            float partialTick) {
        bindEntityTexture(arrow);
        GL11.glPushMatrix();
        GL11.glTranslated(dx, dy, dz);
        GL11.glRotatef(arrow.prevRotationYaw + (arrow.rotationYaw - arrow.prevRotationYaw) * partialTick - 90.0F,
                0.0F, 1.0F, 0.0F);
        GL11.glRotatef(arrow.prevRotationPitch + (arrow.rotationPitch - arrow.prevRotationPitch) * partialTick,
                0.0F, 0.0F, 1.0F);
        Tessellator tessellator = Tessellator.instance;
        byte b0 = 0;
        float f2 = 0.0F;
        float f3 = 0.5F;
        float f4 = (float) (0 + b0 * 10) / 32.0F;
        float f5 = (float) (5 + b0 * 10) / 32.0F;
        float f6 = 0.0F;
        float f7 = 0.15625F;
        float f8 = (float) (5 + b0 * 10) / 32.0F;
        float f9 = (float) (10 + b0 * 10) / 32.0F;
        float f10 = 0.05625F;
        GL11.glEnable(GL12.GL_RESCALE_NORMAL);
        float f11 = (float) arrow.arrowShake - partialTick;

        if (f11 > 0.0F) {
            float f12 = -MathHelper.sin(f11 * 3.0F) * f11;
            GL11.glRotatef(f12, 0.0F, 0.0F, 1.0F);
        }

        GL11.glRotatef(45.0F, 1.0F, 0.0F, 0.0F);
        GL11.glScalef(f10, f10, f10);
        GL11.glTranslatef(-4.0F, 0.0F, 0.0F);
        GL11.glNormal3f(f10, 0.0F, 0.0F);
        tessellator.startDrawingQuads();
        tessellator.addVertexWithUV(-7.0D, -2.0D, -2.0D, (double) f6, (double) f8);
        tessellator.addVertexWithUV(-7.0D, -2.0D, 2.0D, (double) f7, (double) f8);
        tessellator.addVertexWithUV(-7.0D, 2.0D, 2.0D, (double) f7, (double) f9);
        tessellator.addVertexWithUV(-7.0D, 2.0D, -2.0D, (double) f6, (double) f9);
        tessellator.draw();
        GL11.glNormal3f(-f10, 0.0F, 0.0F);
        tessellator.startDrawingQuads();
        tessellator.addVertexWithUV(-7.0D, 2.0D, -2.0D, (double) f6, (double) f8);
        tessellator.addVertexWithUV(-7.0D, 2.0D, 2.0D, (double) f7, (double) f8);
        tessellator.addVertexWithUV(-7.0D, -2.0D, 2.0D, (double) f7, (double) f9);
        tessellator.addVertexWithUV(-7.0D, -2.0D, -2.0D, (double) f6, (double) f9);
        tessellator.draw();

        for (int i = 0; i < 4; ++i) {
            GL11.glRotatef(90.0F, 1.0F, 0.0F, 0.0F);
            GL11.glNormal3f(0.0F, 0.0F, f10);
            tessellator.startDrawingQuads();
            tessellator.addVertexWithUV(-8.0D, -2.0D, 0.0D, (double) f2, (double) f4);
            tessellator.addVertexWithUV(8.0D, -2.0D, 0.0D, (double) f3, (double) f4);
            tessellator.addVertexWithUV(8.0D, 2.0D, 0.0D, (double) f3, (double) f5);
            tessellator.addVertexWithUV(-8.0D, 2.0D, 0.0D, (double) f2, (double) f5);
            tessellator.draw();
        }

        GL11.glDisable(GL12.GL_RESCALE_NORMAL);
        GL11.glPopMatrix();
    }

    @Override
    public void doRender(Entity entity, double dx, double dy, double dz, float yaw, float partialTick) {
        renderArrow((EntityArrowCustom) entity, dx, dy, dz, yaw, partialTick);
    }

    @Override
    protected ResourceLocation getEntityTexture(Entity entity) {
        return getArrowTexture((EntityArrowCustom) entity);
    }

    protected ResourceLocation getArrowTexture(EntityArrowCustom arrow) {
        if (arrow instanceof EntityArrowBomb) {
            return getBombArrowTexture((EntityArrowBomb) arrow);
        } else if (arrow instanceof EntityArrowElemental) {
            return getElementalArrowTexture((EntityArrowElemental) arrow);
        }
        return vanillaArrow;
    }

    protected ResourceLocation getBombArrowTexture(EntityArrowBomb arrow) {
        switch (arrow.getType()) {
        case BOMB_FIRE:
        case BOMB_WATER:
        default:
            return bombArrow;
        }
    }

    protected ResourceLocation getElementalArrowTexture(EntityArrowElemental arrow) {
        switch (arrow.getType()) {
        case FIRE:
            return fireArrow;
        case ICE:
            return iceArrow;
        case LIGHT:
            return lightArrow;
        default:
            return vanillaArrow;
        }
    }
}