Reworked the Mana Spreader's rotation to be vector based. I want to punch a loli after this...

This commit is contained in:
Vasco Lavos committed 2014-01-26 16:03:09 +00:00
1 parent 9fd53fdae8
commit 5c90c9814b
4 files changed
+388 -10

No files matched your search

@@ -0,0 +1,334 @@
/**
* This class was created by <ChickenBones>. It's distributed as
* part of the Botania Mod. Get the Source Code in github:
* https://github.com/Vazkii/Botania
*
* Botania is Open Source and distributed under a
* Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License
* (http://creativecommons.org/licenses/by-nc-sa/3.0/deed.en_GB)
*
* File Created @ [? (GMT)]
*/
package vazkii.botania.client.core.helper;
import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import org.lwjgl.opengl.GL11;
import org.lwjgl.util.vector.Vector3f;
import org.lwjgl.util.vector.Vector4f;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.entity.Entity;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.Vec3;
public class Vector3
{
public static Vector3 zero = new Vector3();
public static Vector3 one = new Vector3(1, 1, 1);
public static Vector3 center = new Vector3(0.5, 0.5, 0.5);
public double x;
public double y;
public double z;
public Vector3()
{
}
public Vector3(double d, double d1, double d2)
{
x = d;
y = d1;
z = d2;
}
public Vector3(Vector3 vec)
{
x = vec.x;
y = vec.y;
z = vec.z;
}
public Vector3(Vec3 vec)
{
x = vec.xCoord;
y = vec.yCoord;
z = vec.zCoord;
}
public Vector3 copy()
{
return new Vector3(this);
}
public static Vector3 fromEntity(Entity e)
{
return new Vector3(e.posX, e.posY, e.posZ);
}
public static Vector3 fromEntityCenter(Entity e)
{
return new Vector3(e.posX, e.posY - e.yOffset + e.height/2, e.posZ);
}
public static Vector3 fromTileEntity(TileEntity e)
{
return new Vector3(e.xCoord, e.yCoord, e.zCoord);
}
public static Vector3 fromTileEntityCenter(TileEntity e)
{
return new Vector3(e.xCoord + 0.5, e.yCoord + 0.5, e.zCoord + 0.5);
}
public Vector3 set(double d, double d1, double d2)
{
x = d;
y = d1;
z = d2;
return this;
}
public Vector3 set(Vector3 vec)
{
x = vec.x;
y = vec.y;
z = vec.z;
return this;
}
public double dotProduct(Vector3 vec)
{
double d = vec.x * x + vec.y * y + vec.z * z;
if(d > 1 && d < 1.00001)
d = 1;
else if(d < -1 && d > -1.00001)
d = -1;
return d;
}
public double dotProduct(double d, double d1, double d2)
{
return d * x + d1 * y + d2 * z;
}
public Vector3 crossProduct(Vector3 vec)
{
double d = y * vec.z - z * vec.y;
double d1 = z * vec.x - x * vec.z;
double d2 = x * vec.y - y * vec.x;
x = d;
y = d1;
z = d2;
return this;
}
public Vector3 add(double d, double d1, double d2)
{
x += d;
y += d1;
z += d2;
return this;
}
public Vector3 add(Vector3 vec)
{
x += vec.x;
y += vec.y;
z += vec.z;
return this;
}
public Vector3 add(double d)
{
return add(d, d, d);
}
public Vector3 sub(Vector3 vec)
{
return subtract(vec);
}
public Vector3 subtract(Vector3 vec)
{
x -= vec.x;
y -= vec.y;
z -= vec.z;
return this;
}
public Vector3 negate(Vector3 vec)
{
x = -x;
y = -y;
z = -z;
return this;
}
public Vector3 multiply(double d)
{
x *= d;
y *= d;
z *= d;
return this;
}
public Vector3 multiply(Vector3 f)
{
x *= f.x;
y *= f.y;
z *= f.z;
return this;
}
public Vector3 multiply(double fx, double fy, double fz)
{
x *= fx;
y *= fy;
z *= fz;
return this;
}
public double mag()
{
return Math.sqrt(x * x + y * y + z * z);
}
public double magSquared()
{
return x * x + y * y + z * z;
}
public Vector3 normalize()
{
double d = mag();
if(d != 0)
{
multiply(1 / d);
}
return this;
}
public String toString()
{
MathContext cont = new MathContext(4, RoundingMode.HALF_UP);
return "Vector3("+new BigDecimal(x, cont)+", "+new BigDecimal(y, cont)+", "+new BigDecimal(z, cont)+")";
}
public Vector3 perpendicular()
{
if(z == 0)
return zCrossProduct();
return xCrossProduct();
}
public Vector3 xCrossProduct()
{
double d = z;
double d1 = -y;
x = 0;
y = d;
z = d1;
return this;
}
public Vector3 zCrossProduct()
{
double d = y;
double d1 = -x;
x = d;
y = d1;
z = 0;
return this;
}
public Vector3 yCrossProduct()
{
double d = -z;
double d1 = x;
x = d;
y = 0;
z = d1;
return this;
}
public Vec3 toVec3D()
{
return Vec3.createVectorHelper(x, y, z);
}
public double angle(Vector3 vec)
{
return Math.acos(copy().normalize().dotProduct(vec.copy().normalize()));
}
public boolean isZero()
{
return x == 0 && y == 0 && z == 0;
}
public boolean isAxial()
{
return x == 0 ? (y == 0 || z == 0) : (y == 0 && z == 0);
}
@SideOnly(Side.CLIENT)
public Vector3f vector3f()
{
return new Vector3f((float)x, (float)y, (float)z);
}
@SideOnly(Side.CLIENT)
public Vector4f vector4f()
{
return new Vector4f((float)x, (float)y, (float)z, 1);
}
@SideOnly(Side.CLIENT)
public void glVertex()
{
GL11.glVertex3d(x, y, z);
}
public Vector3 negate()
{
x = -x;
y = -y;
z = -z;
return this;
}
public double scalarProject(Vector3 b)
{
double l = b.mag();
return l == 0 ? 0 : dotProduct(b)/l;
}
public Vector3 project(Vector3 b)
{
double l = b.magSquared();
if(l == 0)
{
set(0, 0, 0);
return this;
}
double m = dotProduct(b)/l;
set(b).multiply(m);
return this;
}
@Override
public boolean equals(Object o)
{
if(!(o instanceof Vector3))
return false;
Vector3 v = (Vector3)o;
return x == v.x && y == v.y && z == v.z;
}
}
@@ -38,7 +38,7 @@ public class RenderTileSpreader extends TileEntitySpecialRenderer {
Minecraft.getMinecraft().renderEngine.bindTexture(texture);
GL11.glTranslatef(0.5F, 1.5F, 0.5F);
GL11.glRotatef(spreader.rotationX, 0F, 1F, 0F);
GL11.glRotatef(spreader.rotationX + 90F, 0F, 1F, 0F);
GL11.glTranslatef(0F, -1F, 0F);
GL11.glRotatef(spreader.rotationY, 1F, 0F, 0F);
GL11.glTranslatef(0F, 1F, 0F);
@@ -132,7 +132,7 @@ public class TilePool extends TileSimpleInventory implements IManaPool {
@Override
public int getCurrentMana() {
return 10000000; //mana;
return MAX_MANA;
}
@Override
@@ -149,7 +149,4 @@ public class TilePool extends TileSimpleInventory implements IManaPool {
public String getInvName() {
return LibBlockNames.POOL;
}
// TODO
}
@@ -13,19 +13,24 @@ package vazkii.botania.common.block.tile;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.ScaledResolution;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.packet.Packet132TileEntityData;
import net.minecraft.util.ChunkCoordinates;
import net.minecraft.util.MathHelper;
import net.minecraft.util.MovingObjectPosition;
import net.minecraft.util.StatCollector;
import net.minecraft.util.Vec3;
import net.minecraft.world.World;
import org.lwjgl.opengl.GL11;
import vazkii.botania.api.internal.ManaNetworkEvent;
import vazkii.botania.api.mana.IManaCollector;
import vazkii.botania.client.core.helper.Vector3;
import vazkii.botania.common.block.ModBlocks;
import vazkii.botania.common.item.ItemTwigWand;
import cpw.mods.fml.common.network.PacketDispatcher;
import cpw.mods.fml.common.network.Player;
@@ -109,11 +114,53 @@ public class TileSpreader extends TileMod implements IManaCollector {
}
worldObj.playSoundAtEntity(player, "random.orb", 1F, 1F);
} else {
rotationX = -player.rotationYaw;
rotationY = player.rotationPitch;
PacketDispatcher.sendPacketToAllInDimension(getDescriptionPacket(), worldObj.provider.dimensionId);
MovingObjectPosition pos = raytraceFromEntity(worldObj, player, true, 5);
if(pos != null && pos.hitVec != null && !worldObj.isRemote) {
double x = pos.hitVec.xCoord - xCoord - 0.5;
double y = pos.hitVec.yCoord - yCoord - 0.5;
double z = pos.hitVec.zCoord - zCoord - 0.5;
if(pos.sideHit != 0 && pos.sideHit != 1) {
Vector3 clickVector = new Vector3(x, 0, z);
Vector3 relative = new Vector3(-0.5, 0, 0);
double angle = Math.acos(clickVector.dotProduct(relative) / (relative.mag() * clickVector.mag())) * 180D / Math.PI;
rotationX = (float) angle;
if(clickVector.z < 0)
rotationX = 360 - rotationX;
}
double angle = y * 180;
rotationY = (float) angle;
PacketDispatcher.sendPacketToAllInDimension(getDescriptionPacket(), worldObj.provider.dimensionId);
}
}
}
public static MovingObjectPosition raytraceFromEntity(World world, Entity player, boolean par3, double range) {
float f = 1.0F;
float f1 = player.prevRotationPitch + (player.rotationPitch - player.prevRotationPitch) * f;
float f2 = player.prevRotationYaw + (player.rotationYaw - player.prevRotationYaw) * f;
double d0 = player.prevPosX + (player.posX - player.prevPosX) * f;
double d1 = player.prevPosY + (player.posY - player.prevPosY) * f;
if (!world.isRemote && player instanceof EntityPlayer)
d1 += 1.62D;
double d2 = player.prevPosZ + (player.posZ - player.prevPosZ) * f;
Vec3 vec3 = world.getWorldVec3Pool().getVecFromPool(d0, d1, d2);
float f3 = MathHelper.cos(-f2 * 0.017453292F - (float) Math.PI);
float f4 = MathHelper.sin(-f2 * 0.017453292F - (float) Math.PI);
float f5 = -MathHelper.cos(-f1 * 0.017453292F);
float f6 = MathHelper.sin(-f1 * 0.017453292F);
float f7 = f4 * f5;
float f8 = f3 * f5;
double d3 = range;
if (player instanceof EntityPlayerMP)
d3 = ((EntityPlayerMP) player).theItemInWorldManager.getBlockReachDistance();
Vec3 vec31 = vec3.addVector(f7 * d3, f6 * d3, f8 * d3);
return world.rayTraceBlocks_do_do(vec3, vec31, par3, !par3);
}
public void renderHUD(Minecraft mc, ScaledResolution res) {
String name = ModBlocks.spreader.getLocalizedName();