From aa42aedecd2d2842351088085e8fd9d69ec79565 Mon Sep 17 00:00:00 2001 From: Foghrye4 Date: Fri, 14 Apr 2017 07:58:16 +0300 Subject: Colourful lights --- ihl/enviroment/SpotlightTileEntity.java | 451 ++++++++++++-------------------- 1 file changed, 165 insertions(+), 286 deletions(-) (limited to 'ihl/enviroment/SpotlightTileEntity.java') diff --git a/ihl/enviroment/SpotlightTileEntity.java b/ihl/enviroment/SpotlightTileEntity.java index ab18ebe..d8e55df 100644 --- a/ihl/enviroment/SpotlightTileEntity.java +++ b/ihl/enviroment/SpotlightTileEntity.java @@ -1,6 +1,9 @@ package ihl.enviroment; + import ic2.api.network.INetworkTileEntityEventListener; import ic2.core.IC2; +import ihl.ClientProxy; +import ihl.IHLMod; import ihl.IHLModInfo; import ihl.handpump.XYZ; import ihl.utils.IHLUtils; @@ -9,6 +12,9 @@ import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Set; + +import cpw.mods.fml.relauncher.Side; +import cpw.mods.fml.relauncher.SideOnly; import net.minecraft.block.Block; import net.minecraft.block.material.Material; import net.minecraft.entity.EntityLivingBase; @@ -19,315 +25,188 @@ import net.minecraft.nbt.NBTTagCompound; import net.minecraft.util.Vec3; import net.minecraftforge.common.util.ForgeDirection; -public class SpotlightTileEntity extends LightBulbTileEntity implements INetworkTileEntityEventListener -{ - Vec3 directionVector=Vec3.createVectorHelper(1, 0, 0); - private static final int maxLightRange=128; - private final Set xyzGlowningAir = new HashSet(); - private static final Set lightSphereVectors = new HashSet(); - public float rotationPitch=0f; - public float rotationYaw=0f; - public float prevRotationPitch=0f; - public float prevRotationYaw=0f; - boolean needLightTargetUpdate=false; - - public static void createLightSphereVectors() - { - for(double y=-1d;y<=1d;y+=0.2d) - { - double sqr = 1-y*y; - double x,z; - for(double alpha=0;alpha getNetworkedFields() - { - List list = super.getNetworkedFields(); - list.add("rotationPitch"); - list.add("rotationYaw"); - return list; - } - - public void setLightOn() - { - Iterator gai = xyzGlowningAir.iterator(); - while(gai.hasNext()) - { - XYZ xyz = gai.next(); - Block block = worldObj.getBlock(xyz.x,xyz.y,xyz.z); - if(block==Blocks.air || block.isAir(this.worldObj, xyz.x,xyz.y,xyz.z)) - { - worldObj.setBlock(xyz.x, xyz.y, xyz.z, LightBulbBlock.glowningAir); - } - } + @Override + public void writeToNBT(NBTTagCompound nbttagcompound) { + super.writeToNBT(nbttagcompound); + nbttagcompound.setDouble("directionX", directionX); + nbttagcompound.setDouble("directionY", directionY); + nbttagcompound.setDouble("directionZ", directionZ); } - - public void setLightOff() - { - Iterator gai = xyzGlowningAir.iterator(); - while(gai.hasNext()) - { - XYZ xyz = gai.next(); - Block block = worldObj.getBlock(xyz.x,xyz.y,xyz.z); - if(block == LightBulbBlock.glowningAir) - { - worldObj.setBlockToAir(xyz.x, xyz.y, xyz.z); - } - } + + @Override + public List getNetworkedFields() { + List list = super.getNetworkedFields(); + list.add("needLightTargetUpdate"); + list.add("rotationPitch"); + list.add("rotationYaw"); + list.add("directionX"); + list.add("directionY"); + list.add("directionZ"); + return list; } - + @Override - public void updateEntity() - { + public void updateEntity() { super.updateEntity(); - if(needLightTargetUpdate) - { - this.generateGlowningAirList(); + if (this.worldObj.isRemote) { + if(this.directionX!=this.prevDirectionX || + this.directionY!=this.prevDirectionY || + this.directionZ!=this.prevDirectionZ){ + updateLightState(); + this.prevDirectionX=this.directionX; + this.prevDirectionY=this.directionY; + this.prevDirectionZ=this.directionZ; + } + } else if (needLightTargetUpdate) { this.updateLightState(); - needLightTargetUpdate=false; - } - if(this.prevRotationPitch!=this.rotationPitch) - { - IC2.network.get().updateTileEntityField(this, "rotationPitch"); - this.prevRotationPitch=this.rotationPitch; + needLightTargetUpdate = false; } - if(this.prevRotationYaw!=this.rotationYaw) - { - IC2.network.get().updateTileEntityField(this, "rotationYaw"); - this.prevRotationYaw=this.rotationYaw; + if (!this.worldObj.isRemote) { + if (this.prevRotationPitch != this.rotationPitch) { + IC2.network.get().updateTileEntityField(this, "rotationPitch"); + this.prevRotationPitch = this.rotationPitch; + } + if (this.prevRotationYaw != this.rotationYaw) { + IC2.network.get().updateTileEntityField(this, "rotationYaw"); + this.prevRotationYaw = this.rotationYaw; + } } - } - + } + @Override public ItemStack getWrenchDrop(EntityPlayer entityPlayer) { return IHLUtils.getThisModItemStack("spotlight"); } - - public void setDirectionVector(EntityLivingBase player) - { - ForgeDirection dir = ForgeDirection.getOrientation(this.getFacing()); - Vec3 lookVec = player.getLookVec(); - if(lookVec.xCoord*dir.offsetX+lookVec.yCoord*dir.offsetY+lookVec.zCoord*dir.offsetZ<0) - { - double x=player.posX-this.xCoord-0.5D; - double y=player.posY+player.getEyeHeight()-this.yCoord-0.5D; - double z=player.posZ-this.zCoord-0.5D; - double d=Math.sqrt(x*x+y*y+z*z); - directionVector=Vec3.createVectorHelper(x/d, y/d, z/d); - } - else - { - directionVector=player.getLookVec(); - } - this.rotationPitch=this.getVectorPitchAngle(directionVector); - this.rotationYaw=this.getVectorYawAngle(directionVector); - this.needLightTargetUpdate=true; - IC2.network.get().initiateTileEntityEvent(this, 0, true); - } - - private void generateGlowningAirList() - { - this.setLightOff(); - xyzGlowningAir.clear(); - Iterator lvi = lightSphereVectors.iterator(); - while(lvi.hasNext()) - { - traceVectorToLastAirBlock(lvi.next()); + + public void setDirectionVector(EntityLivingBase player) { + ForgeDirection dir = ForgeDirection.getOrientation(this.getFacing()); + Vec3 lookVec = player.getLookVec(); + if (lookVec.xCoord * dir.offsetX + lookVec.yCoord * dir.offsetY + lookVec.zCoord * dir.offsetZ < 0) { + double x = player.posX - this.xCoord - 0.5D; + double y = player.posY + player.getEyeHeight() - this.yCoord - 0.5D; + double z = player.posZ - this.zCoord - 0.5D; + double d = Math.sqrt(x * x + y * y + z * z); + directionX = (float) (x / d); + directionY = (float) (y / d); + directionZ = (float) (z / d); + } else { + Vec3 plook = player.getLookVec(); + directionX = (float) (plook.xCoord); + directionY = (float) (plook.yCoord); + directionZ = (float) (plook.zCoord); } + IC2.network.get().updateTileEntityField(this, "directionX"); + IC2.network.get().updateTileEntityField(this, "directionY"); + IC2.network.get().updateTileEntityField(this, "directionZ"); + this.rotationPitch = this.getVectorPitchAngle(directionX,directionY,directionZ); + this.rotationYaw = this.getVectorYawAngle(directionX,directionY,directionZ); + this.needLightTargetUpdate = true; + IC2.network.get().initiateTileEntityEvent(this, 0, true); } - - private void traceVectorToLastAirBlock(Vec3 vector) - { - double dx=directionVector.xCoord; - double dy=directionVector.yCoord; - double dz=directionVector.zCoord; - double dx1=vector.xCoord; - double dy1=vector.yCoord; - double dz1=vector.zCoord; - if(2*dx*dx1+dx1*dx1+2*dy*dy1+dy1*dy1+2*dz*dz1+dz1*dz1>0d) - { - double x1=xCoord+0.5d; - double y1=yCoord+0.5d; - double z1=zCoord+0.5d; - int xi1=xCoord; - int yi1=yCoord; - int zi1=zCoord; - int airx=xCoord; - int airy=yCoord; - int airz=zCoord; - for(int i=0;i=0) - { - return (float)(Math.acos(x/Math.sqrt(x*x+z*z))-Math.PI/2); - } - else - { - return (float)(-Math.acos(x/Math.sqrt(x*x+z*z))-Math.PI/2); - } - case 1: - if(z>=0) - { - return (float)(Math.acos(x/Math.sqrt(x*x+z*z))-Math.PI/2); - } - else - { - return (float)(-Math.acos(x/Math.sqrt(x*x+z*z))-Math.PI/2); - } - case 2: - if(y>=0) - { - return (float)(-Math.acos(x/Math.sqrt(x*x+y*y))-Math.PI/2); - } - else - { - return (float)(Math.acos(x/Math.sqrt(x*x+y*y))-Math.PI/2); - } - case 3: - if(y>=0) - { - return (float)(-Math.acos(x/Math.sqrt(x*x+y*y))-Math.PI/2); - } - else - { - return (float)(Math.acos(x/Math.sqrt(x*x+y*y))-Math.PI/2); - } - case 4: - if(y>=0) - { - return (float)(-Math.acos(z/Math.sqrt(z*z+y*y))-Math.PI/2); - } - else - { - return (float)(Math.acos(z/Math.sqrt(z*z+y*y))-Math.PI/2); - } - case 5: - if(y>=0) - { - return (float)(-Math.acos(z/Math.sqrt(z*z+y*y))-Math.PI/2); - } - else - { - return (float)(Math.acos(z/Math.sqrt(z*z+y*y))-Math.PI/2); - } - default: - return 0f; + + private float getVectorYawAngle(float x, float y, float z) { + switch (this.getFacing()) { + case 0: + if (z >= 0) { + return (float) (Math.acos(x / Math.sqrt(x * x + z * z)) - Math.PI / 2); + } else { + return (float) (-Math.acos(x / Math.sqrt(x * x + z * z)) - Math.PI / 2); + } + case 1: + if (z >= 0) { + return (float) (Math.acos(x / Math.sqrt(x * x + z * z)) - Math.PI / 2); + } else { + return (float) (-Math.acos(x / Math.sqrt(x * x + z * z)) - Math.PI / 2); + } + case 2: + if (y >= 0) { + return (float) (-Math.acos(x / Math.sqrt(x * x + y * y)) - Math.PI / 2); + } else { + return (float) (Math.acos(x / Math.sqrt(x * x + y * y)) - Math.PI / 2); + } + case 3: + if (y >= 0) { + return (float) (-Math.acos(x / Math.sqrt(x * x + y * y)) - Math.PI / 2); + } else { + return (float) (Math.acos(x / Math.sqrt(x * x + y * y)) - Math.PI / 2); + } + case 4: + if (y >= 0) { + return (float) (-Math.acos(z / Math.sqrt(z * z + y * y)) - Math.PI / 2); + } else { + return (float) (Math.acos(z / Math.sqrt(z * z + y * y)) - Math.PI / 2); + } + case 5: + if (y >= 0) { + return (float) (-Math.acos(z / Math.sqrt(z * z + y * y)) - Math.PI / 2); + } else { + return (float) (Math.acos(z / Math.sqrt(z * z + y * y)) - Math.PI / 2); + } + default: + return 0f; } } @Override - public void onNetworkEvent(int event) - { - this.worldObj.playSound(xCoord+0.5d, yCoord+0.5d, zCoord+0.5d, IHLModInfo.MODID+":spotlightRotating", 10F, 1f, true); + public void onNetworkEvent(int event) { + this.worldObj.playSound(xCoord + 0.5d, yCoord + 0.5d, zCoord + 0.5d, IHLModInfo.MODID + ":spotlightRotating", + 10F, 1f, true); } } - -/* -Y DOWN(0, -1, 0) 0 -/* +Y UP(0, 1, 0) 1 -/* -Z NORTH(0, 0, -1) 2 -/* +Z SOUTH(0, 0, 1) 3 -/* -X WEST(-1, 0, 0), 4 -/* +X EAST(1, 0, 0), 5 -//VALID_DIRECTIONS = {DOWN, UP, NORTH, SOUTH, WEST, EAST};*/ +/* + * -Y DOWN(0, -1, 0) 0 /* +Y UP(0, 1, 0) 1 /* -Z NORTH(0, 0, -1) 2 /* +Z + * SOUTH(0, 0, 1) 3 /* -X WEST(-1, 0, 0), 4 /* +X EAST(1, 0, 0), 5 + * //VALID_DIRECTIONS = {DOWN, UP, NORTH, SOUTH, WEST, EAST}; + */ -- cgit v1.2.3