This commit is contained in:
Ben Spiers 2014-09-14 05:20:16 +01:00
parent 1cfaec3692
commit b29f2adc4d
7 changed files with 93 additions and 29 deletions

View file

@ -2,8 +2,10 @@ package mekanism.api;
import net.minecraft.entity.Entity;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.util.MathHelper;
import net.minecraft.util.Vec3;
import net.minecraftforge.common.util.ForgeDirection;
/**
* Pos3D - a way of performing operations on objects in a three dimensional environment.
@ -80,6 +82,14 @@ public class Pos3D
return new Pos3D(entity.motionX, entity.motionY, entity.motionZ);
}
/**
* Centres a block-derived Pos3D
*/
public Pos3D centre()
{
return translate(0.5, 0.5, 0.5);
}
/**
* Translates this Pos3D by the defined values.
* @param x - amount to translate on the x axis
@ -106,6 +116,26 @@ public class Pos3D
return translate(pos.xPos, pos.yPos, pos.zPos);
}
/**
* Performs the same operation as translate(x, y, z), but by a set amount in a ForgeDirection
*/
public Pos3D translate(ForgeDirection direction, double amount)
{
return translate(direction.offsetX * amount, direction.offsetY * amount, direction.offsetZ * amount);
}
/**
* Performs the same operation as translate(x, y, z), but by a set amount in a ForgeDirection
*/
public Pos3D translateExcludingSide(ForgeDirection direction, double amount)
{
if(direction.offsetX == 0) xPos += amount;
if(direction.offsetY == 0) yPos += amount;
if(direction.offsetZ == 0) zPos += amount;
return this;
}
/**
* Returns the distance between this and the defined Pos3D.
* @param pos - the Pos3D to find the distance to
@ -207,6 +237,18 @@ public class Pos3D
return scale(scale, scale, scale);
}
public static AxisAlignedBB getAABB(Pos3D pos1, Pos3D pos2)
{
return AxisAlignedBB.getBoundingBox(
Math.min(pos1.xPos, pos2.xPos),
Math.min(pos1.yPos, pos2.yPos),
Math.min(pos1.zPos, pos2.zPos),
Math.max(pos1.xPos, pos2.xPos),
Math.max(pos1.yPos, pos2.yPos),
Math.max(pos1.zPos, pos2.zPos)
);
}
@Override
public Pos3D clone()
{

View file

@ -5,6 +5,7 @@ import java.io.File;
import mekanism.api.Coord4D;
import mekanism.api.MekanismConfig.client;
import mekanism.api.MekanismConfig.general;
import mekanism.api.Pos3D;
import mekanism.client.entity.EntityLaser;
import mekanism.client.gui.GuiAmbientAccumulator;
import mekanism.client.gui.GuiChemicalCrystallizer;
@ -553,8 +554,8 @@ public class ClientProxy extends CommonProxy
}
@Override
public void renderLaser(World world, Coord4D from, Coord4D to, ForgeDirection direction)
public void renderLaser(World world, Pos3D from, Pos3D to, ForgeDirection direction, double energy)
{
Minecraft.getMinecraft().effectRenderer.addEffect(new EntityLaser(world, from, to, direction));
Minecraft.getMinecraft().effectRenderer.addEffect(new EntityLaser(world, from, to, direction, energy));
}
}

View file

@ -1,6 +1,5 @@
package mekanism.client.entity;
import mekanism.api.Coord4D;
import mekanism.api.Pos3D;
import mekanism.client.render.MekanismRenderer;
@ -21,16 +20,16 @@ public class EntityLaser extends EntityFX
double length;
ForgeDirection direction;
public EntityLaser(World world, Coord4D start, Coord4D end, ForgeDirection dir)
public EntityLaser(World world, Pos3D start, Pos3D end, ForgeDirection dir, double energy)
{
super(world, (start.xCoord + end.xCoord)/2D + 0.5D, (start.yCoord + end.yCoord)/2D + 0.5D, (start.zCoord+end.zCoord)/2D + 0.5D);
super(world, (start.xPos + end.xPos)/2D, (start.yPos + end.yPos)/2D, (start.zPos+end.zPos)/2D);
particleMaxAge = 5;
particleRed = 1;
particleGreen = 0;
particleBlue = 0;
particleAlpha = 0.1F;
particleScale = 0.1F;
length = new Pos3D(end).distance(new Pos3D(start));
particleScale = (float) Math.min(energy / 50000, 0.5);
length = end.distance(start);
direction = dir;
}

View file

@ -2,10 +2,10 @@ package mekanism.common;
import java.io.File;
import mekanism.api.Coord4D;
import mekanism.api.MekanismAPI;
import mekanism.api.MekanismConfig.general;
import mekanism.api.MekanismConfig.usage;
import mekanism.api.Pos3D;
import mekanism.api.util.EnergyUtils.EnergyType;
import mekanism.common.base.IUpgradeTile;
import mekanism.common.block.BlockMachine.MachineType;
@ -462,5 +462,5 @@ public class CommonProxy
return context.getServerHandler().playerEntity;
}
public void renderLaser(World world, Coord4D from, Coord4D to, ForgeDirection direction) {}
public void renderLaser(World world, Pos3D from, Pos3D to, ForgeDirection direction, double energy) {}
}

View file

@ -1,9 +1,13 @@
package mekanism.common;
import java.util.List;
import mekanism.api.Coord4D;
import mekanism.api.MekanismConfig.general;
import mekanism.api.Pos3D;
import mekanism.api.lasers.ILaserReceptor;
import net.minecraft.entity.Entity;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.MovingObjectPosition;
import net.minecraft.util.Vec3;
@ -12,21 +16,27 @@ import net.minecraftforge.common.util.ForgeDirection;
public class LaserManager
{
public static void fireLaser(Coord4D from, ForgeDirection direction, double energy, World world)
public static void fireLaser(TileEntity from, ForgeDirection direction, double energy, World world)
{
Coord4D rangeFrom = from.getFromSide(direction, 1);
Coord4D to = from.getFromSide(direction, general.laserRange);
MovingObjectPosition mop = world.rayTraceBlocks(Vec3.createVectorHelper(rangeFrom.xCoord, rangeFrom.yCoord, rangeFrom.zCoord), Vec3.createVectorHelper(to.xCoord, to.yCoord, to.zCoord));
fireLaser(new Pos3D(from).centre().translate(direction, 0.501), direction, energy, world);
}
public static void fireLaser(Pos3D from, ForgeDirection direction, double energy, World world)
{
Pos3D to = from.clone().translate(direction, general.laserRange);
MovingObjectPosition mop = world.rayTraceBlocks(Vec3.createVectorHelper(from.xPos, from.yPos, from.zPos), Vec3.createVectorHelper(to.xPos, to.yPos, to.zPos));
if(mop != null)
{
TileEntity tile = world.getTileEntity(mop.blockX, mop.blockY, mop.blockZ);
to = new Pos3D(mop.hitVec);
Coord4D toCoord = new Coord4D(mop.blockX, mop.blockY, mop.blockZ);
TileEntity tile = toCoord.getTileEntity(world);
if(tile instanceof ILaserReceptor)
{
if(((ILaserReceptor)tile).canLasersDig() && energy > ((ILaserReceptor)tile).energyToDig())
{
//TODO dig block
}
else
{
@ -34,22 +44,32 @@ public class LaserManager
}
}
}
from.translateExcludingSide(direction, -0.1);
to.translateExcludingSide(direction, 0.1);
for(Entity e : (List<Entity>)world.getEntitiesWithinAABB(Entity.class, Pos3D.getAABB(from, to)))
{
if(!e.isImmuneToFire()) e.setFire((int)(energy / 1000));
}
}
public static void fireLaserClient(Coord4D from, ForgeDirection direction, World world)
public static void fireLaserClient(TileEntity from, ForgeDirection direction, double energy, World world)
{
Coord4D rangeFrom = from.getFromSide(direction, 1);
Coord4D to = from.getFromSide(direction, general.laserRange);
MovingObjectPosition mop = world.rayTraceBlocks(Vec3.createVectorHelper(rangeFrom.xCoord, rangeFrom.yCoord, rangeFrom.zCoord), Vec3.createVectorHelper(to.xCoord, to.yCoord, to.zCoord));
fireLaserClient(new Pos3D(from).centre().translate(direction, 0.501), direction, energy, world);
}
public static void fireLaserClient(Pos3D from, ForgeDirection direction, double energy, World world)
{
Pos3D to = from.clone().translate(direction, general.laserRange);
MovingObjectPosition mop = world.rayTraceBlocks(Vec3.createVectorHelper(from.xPos, from.yPos, from.zPos), Vec3.createVectorHelper(to.xPos, to.yPos, to.zPos));
if(mop != null)
{
Mekanism.proxy.renderLaser(world, from, new Coord4D(mop.blockX, mop.blockY, mop.blockZ), direction);
}
else
{
Mekanism.proxy.renderLaser(world, from, to, direction);
to = new Pos3D(mop.hitVec);
}
from.translate(direction, -0.501);
Mekanism.proxy.renderLaser(world, from, to, direction, energy);
}
}

View file

@ -32,7 +32,7 @@ public class TileEntityLaser extends TileEntityElectricBlock
{
if(on)
{
LaserManager.fireLaserClient(Coord4D.get(this), ForgeDirection.getOrientation(facing), worldObj);
LaserManager.fireLaserClient(this, ForgeDirection.getOrientation(facing), usage.laserUsage, worldObj);
}
}
else
@ -45,7 +45,7 @@ public class TileEntityLaser extends TileEntityElectricBlock
Mekanism.packetHandler.sendToAllAround(new TileEntityMessage(Coord4D.get(this), getNetworkedData(new ArrayList())), Coord4D.get(this).getTargetPoint(50D));
}
LaserManager.fireLaser(Coord4D.get(this), ForgeDirection.getOrientation(facing), usage.laserUsage, worldObj);
LaserManager.fireLaser(this, ForgeDirection.getOrientation(facing), usage.laserUsage, worldObj);
setEnergy(getEnergy() - usage.laserUsage);
}
else if(on)

View file

@ -17,6 +17,7 @@ public class TileEntityLaserAmplifier extends TileEntityContainerBlock implement
{
public static final double MAX_ENERGY = 5E9;
public double collectedEnergy = 0;
public double lastFired = 0;
public double threshold = 0;
public int ticks = 0;
@ -58,7 +59,7 @@ public class TileEntityLaserAmplifier extends TileEntityContainerBlock implement
{
if(on)
{
LaserManager.fireLaserClient(Coord4D.get(this), ForgeDirection.getOrientation(facing), worldObj);
LaserManager.fireLaserClient(this, ForgeDirection.getOrientation(facing), toFire(), worldObj);
}
}
else
@ -76,14 +77,15 @@ public class TileEntityLaserAmplifier extends TileEntityContainerBlock implement
if(shouldFire() && toFire() > 0)
{
if(!on)
if(!on || toFire() != lastFired)
{
on = true;
Mekanism.packetHandler.sendToAllAround(new TileEntityMessage(Coord4D.get(this), getNetworkedData(new ArrayList())), Coord4D.get(this).getTargetPoint(50D));
}
LaserManager.fireLaser(Coord4D.get(this), ForgeDirection.getOrientation(facing), toFire(), worldObj);
LaserManager.fireLaser(this, ForgeDirection.getOrientation(facing), toFire(), worldObj);
setEnergy(getEnergy() - toFire());
lastFired = toFire();
}
else if(on)
{