fixed laser animation, close #1629

This commit is contained in:
SpaceToad 2014-04-26 19:56:24 +02:00
parent 69c8d75371
commit 046564f253
4 changed files with 156 additions and 52 deletions

View file

@ -8,25 +8,27 @@
*/ */
package buildcraft.silicon; package buildcraft.silicon;
import static net.minecraft.util.AxisAlignedBB.getBoundingBox;
import java.util.List;
import net.minecraft.block.BlockContainer; import net.minecraft.block.BlockContainer;
import net.minecraft.block.material.Material; import net.minecraft.block.material.Material;
import net.minecraft.client.renderer.texture.IIconRegister; import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.tileentity.TileEntity; import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.util.IIcon; import net.minecraft.util.IIcon;
import net.minecraft.util.MovingObjectPosition;
import net.minecraft.util.Vec3;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World; import net.minecraft.world.World;
import net.minecraftforge.common.util.ForgeDirection; import net.minecraftforge.common.util.ForgeDirection;
import buildcraft.core.CreativeTabBuildCraft; import buildcraft.core.CreativeTabBuildCraft;
import buildcraft.core.ICustomHighlight;
import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly; import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.util.MovingObjectPosition;
import buildcraft.core.ICustomHighlight;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.util.Vec3;
import java.util.List;
import static net.minecraft.util.AxisAlignedBB.getBoundingBox;
public class BlockLaser extends BlockContainer implements ICustomHighlight { public class BlockLaser extends BlockContainer implements ICustomHighlight {
@ -118,12 +120,13 @@ public class BlockLaser extends BlockContainer implements ICustomHighlight {
@Override @Override
public IIcon getIcon(int i, int j) { public IIcon getIcon(int i, int j) {
if (i == ForgeDirection.values()[j].getOpposite().ordinal()) if (i == ForgeDirection.values()[j].getOpposite().ordinal()) {
return textureBottom; return textureBottom;
else if (i == j) } else if (i == j) {
return textureTop; return textureTop;
else } else {
return textureSide; return textureSide;
}
} }
@ -145,4 +148,9 @@ public class BlockLaser extends BlockContainer implements ICustomHighlight {
textureBottom = par1IconRegister.registerIcon("buildcraft:laser_bottom"); textureBottom = par1IconRegister.registerIcon("buildcraft:laser_bottom");
textureSide = par1IconRegister.registerIcon("buildcraft:laser_side"); textureSide = par1IconRegister.registerIcon("buildcraft:laser_side");
} }
@Override
public boolean isSideSolid(IBlockAccess world, int x, int y, int z, ForgeDirection side) {
return false;
}
} }

View file

@ -8,6 +8,8 @@
*/ */
package buildcraft.silicon; package buildcraft.silicon;
import buildcraft.silicon.render.RenderLaserBlock;
import cpw.mods.fml.client.registry.ClientRegistry;
import cpw.mods.fml.client.registry.RenderingRegistry; import cpw.mods.fml.client.registry.RenderingRegistry;
public class SiliconProxyClient extends SiliconProxy { public class SiliconProxyClient extends SiliconProxy {
@ -15,5 +17,7 @@ public class SiliconProxyClient extends SiliconProxy {
public void registerRenderers() { public void registerRenderers() {
SiliconProxy.laserBlockModel = RenderingRegistry.getNextAvailableRenderId(); SiliconProxy.laserBlockModel = RenderingRegistry.getNextAvailableRenderId();
RenderingRegistry.registerBlockHandler(new SiliconRenderBlock()); RenderingRegistry.registerBlockHandler(new SiliconRenderBlock());
ClientRegistry.bindTileEntitySpecialRenderer(TileLaser.class, new RenderLaserBlock());
} }
} }

View file

@ -8,36 +8,39 @@
*/ */
package buildcraft.silicon; package buildcraft.silicon;
import buildcraft.api.power.ILaserTarget;
import buildcraft.BuildCraftCore;
import buildcraft.BuildCraftFactory;
import buildcraft.BuildCraftMod;
import buildcraft.BuildCraftSilicon;
import buildcraft.api.core.Position;
import buildcraft.api.core.SafeTimeTracker;
import buildcraft.api.gates.IAction;
import buildcraft.api.gates.IActionReceptor;
import buildcraft.api.power.IPowerReceptor;
import buildcraft.api.power.PowerHandler;
import buildcraft.api.power.PowerHandler.PowerReceiver;
import buildcraft.api.power.PowerHandler.Type;
import buildcraft.core.EntityEnergyLaser;
import buildcraft.core.IMachine;
import buildcraft.core.TileBuildCraft;
import buildcraft.core.proxy.CoreProxy;
import buildcraft.core.triggers.ActionMachineControl;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity; import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.common.util.ForgeDirection; import net.minecraftforge.common.util.ForgeDirection;
import buildcraft.BuildCraftCore;
import buildcraft.api.core.NetworkData;
import buildcraft.api.core.Position;
import buildcraft.api.core.SafeTimeTracker;
import buildcraft.api.gates.IAction;
import buildcraft.api.gates.IActionReceptor;
import buildcraft.api.power.ILaserTarget;
import buildcraft.api.power.IPowerReceptor;
import buildcraft.api.power.PowerHandler;
import buildcraft.api.power.PowerHandler.PowerReceiver;
import buildcraft.api.power.PowerHandler.Type;
import buildcraft.core.Box;
import buildcraft.core.EntityLaser;
import buildcraft.core.IMachine;
import buildcraft.core.LaserData;
import buildcraft.core.TileBuildCraft;
import buildcraft.core.triggers.ActionMachineControl;
public class TileLaser extends TileBuildCraft implements IPowerReceptor, IActionReceptor, IMachine { public class TileLaser extends TileBuildCraft implements IPowerReceptor, IActionReceptor, IMachine {
private static final float LASER_OFFSET = 2.0F / 16.0F; private static final float LASER_OFFSET = 2.0F / 16.0F;
private EntityEnergyLaser laser = null;
@NetworkData
public LaserData laser = new LaserData();
private final SafeTimeTracker laserTickTracker = new SafeTimeTracker(10); private final SafeTimeTracker laserTickTracker = new SafeTimeTracker(10);
private final SafeTimeTracker searchTracker = new SafeTimeTracker(100, 100); private final SafeTimeTracker searchTracker = new SafeTimeTracker(100, 100);
private final SafeTimeTracker networkTracker = new SafeTimeTracker(3); private final SafeTimeTracker networkTracker = new SafeTimeTracker(3);
@ -46,6 +49,15 @@ public class TileLaser extends TileBuildCraft implements IPowerReceptor, IAction
private ActionMachineControl.Mode lastMode = ActionMachineControl.Mode.Unknown; private ActionMachineControl.Mode lastMode = ActionMachineControl.Mode.Unknown;
private static final PowerHandler.PerditionCalculator PERDITION = new PowerHandler.PerditionCalculator(0.5F); private static final PowerHandler.PerditionCalculator PERDITION = new PowerHandler.PerditionCalculator(0.5F);
private static final short POWER_AVERAGING = 100;
private int powerIndex = 0;
@NetworkData
private double powerAverage = 0;
private final double power[] = new double[POWER_AVERAGING];
public TileLaser() { public TileLaser() {
powerHandler = new PowerHandler(this, Type.MACHINE); powerHandler = new PowerHandler(this, Type.MACHINE);
initPowerProvider(); initPowerProvider();
@ -56,12 +68,23 @@ public class TileLaser extends TileBuildCraft implements IPowerReceptor, IAction
powerHandler.setPerdition(PERDITION); powerHandler.setPerdition(PERDITION);
} }
@Override
public void initialize () {
super.initialize();
laser.head = new Position(xCoord, yCoord, zCoord);
laser.tail = new Position(xCoord + 2, yCoord + 2, zCoord + 2);
}
@Override @Override
public void updateEntity() { public void updateEntity() {
super.updateEntity(); super.updateEntity();
if (!!worldObj.isRemote) laser.iterateTexture();
if (!!worldObj.isRemote) {
return; return;
}
// If a gate disabled us, remove laser and do nothing. // If a gate disabled us, remove laser and do nothing.
if (lastMode == ActionMachineControl.Mode.Off) { if (lastMode == ActionMachineControl.Mode.Off) {
@ -89,9 +112,7 @@ public class TileLaser extends TileBuildCraft implements IPowerReceptor, IAction
// We have a table and can work, so we create a laser if // We have a table and can work, so we create a laser if
// necessary. // necessary.
if (laser == null) { laser.isVisible = true;
createLaser();
}
// We have a laser and may update it // We have a laser and may update it
if (laser != null && canUpdateLaser()) { if (laser != null && canUpdateLaser()) {
@ -103,7 +124,7 @@ public class TileLaser extends TileBuildCraft implements IPowerReceptor, IAction
laserTarget.receiveLaserEnergy(power); laserTarget.receiveLaserEnergy(power);
if (laser != null) { if (laser != null) {
laser.pushPower(power); pushPower(power);
} }
onPowerSent(power); onPowerSent(power);
@ -128,8 +149,9 @@ public class TileLaser extends TileBuildCraft implements IPowerReceptor, IAction
protected boolean isValidTable() { protected boolean isValidTable() {
if (laserTarget == null || laserTarget.isInvalidTarget() || !laserTarget.requiresLaserEnergy()) if (laserTarget == null || laserTarget.isInvalidTarget() || !laserTarget.requiresLaserEnergy()) {
return false; return false;
}
return true; return true;
} }
@ -185,16 +207,11 @@ public class TileLaser extends TileBuildCraft implements IPowerReceptor, IAction
} }
} }
if (targets.isEmpty()) if (targets.isEmpty()) {
return; return;
laserTarget = targets.get(worldObj.rand.nextInt(targets.size()));
} }
protected void createLaser() { laserTarget = targets.get(worldObj.rand.nextInt(targets.size()));
laser = new EntityEnergyLaser(worldObj, new Position(xCoord, yCoord, zCoord), new Position(xCoord, yCoord, zCoord));
worldObj.spawnEntityInWorld(laser);
} }
protected void updateLaser() { protected void updateLaser() {
@ -229,18 +246,16 @@ public class TileLaser extends TileBuildCraft implements IPowerReceptor, IAction
Position tail = new Position(laserTarget.getXCoord() + 0.475 + (worldObj.rand.nextFloat() - 0.5) / 5F, laserTarget.getYCoord() + 9F / 16F, Position tail = new Position(laserTarget.getXCoord() + 0.475 + (worldObj.rand.nextFloat() - 0.5) / 5F, laserTarget.getYCoord() + 9F / 16F,
laserTarget.getZCoord() + 0.475 + (worldObj.rand.nextFloat() - 0.5) / 5F); laserTarget.getZCoord() + 0.475 + (worldObj.rand.nextFloat() - 0.5) / 5F);
laser.setPositions(head, tail); laser.head = head;
laser.tail = tail;
if (!laser.isVisible()) { if (!laser.isVisible) {
laser.show(); laser.isVisible = true;
} }
} }
protected void removeLaser() { protected void removeLaser() {
if (laser != null) { laser.isVisible = false;
laser.setDead();
laser = null;
}
} }
@Override @Override
@ -308,4 +323,34 @@ public class TileLaser extends TileBuildCraft implements IPowerReceptor, IAction
lastMode = ActionMachineControl.Mode.Off; lastMode = ActionMachineControl.Mode.Off;
} }
} }
private void pushPower(double received) {
powerAverage -= power[powerIndex];
powerAverage += received;
power[powerIndex] = received;
powerIndex++;
if (powerIndex == power.length) {
powerIndex = 0;
}
}
public ResourceLocation getTexture() {
double avg = powerAverage / POWER_AVERAGING;
if (avg <= 1.0) {
return EntityLaser.LASER_TEXTURES[0];
} else if (avg <= 2.0) {
return EntityLaser.LASER_TEXTURES[1];
} else if (avg <= 3.0) {
return EntityLaser.LASER_TEXTURES[2];
} else {
return EntityLaser.LASER_TEXTURES[3];
}
}
@Override
public AxisAlignedBB getRenderBoundingBox() {
return new Box(this).extendToEncompass(laser.tail).getBoundingBox();
}
} }

View file

@ -0,0 +1,47 @@
/**
* Copyright (c) 2011-2014, SpaceToad and the BuildCraft Team
* http://www.mod-buildcraft.com
*
* BuildCraft is distributed under the terms of the Minecraft Mod Public
* License 1.0, or MMPL. Please check the contents of the license located in
* http://www.mod-buildcraft.com/MMPL-1.0.txt
*/
package buildcraft.silicon.render;
import net.minecraft.client.renderer.tileentity.TileEntityRendererDispatcher;
import net.minecraft.client.renderer.tileentity.TileEntitySpecialRenderer;
import net.minecraft.tileentity.TileEntity;
import org.lwjgl.opengl.GL11;
import buildcraft.core.render.RenderLaser;
import buildcraft.silicon.TileLaser;
public class RenderLaserBlock extends TileEntitySpecialRenderer {
@Override
public void renderTileEntityAt(TileEntity tileentity, double x, double y, double z, float f) {
TileLaser laser = ((TileLaser) tileentity);
if (laser != null) {
GL11.glPushMatrix();
GL11.glPushAttrib(GL11.GL_ENABLE_BIT);
GL11.glEnable(GL11.GL_CULL_FACE);
GL11.glEnable(GL11.GL_LIGHTING);
GL11.glEnable(GL11.GL_BLEND);
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
GL11.glTranslated(x, y, z);
GL11.glTranslated(-tileentity.xCoord, -tileentity.yCoord, -tileentity.zCoord);
GL11.glPushMatrix();
RenderLaser.doRenderLaser(TileEntityRendererDispatcher.instance.field_147553_e,
laser.laser, laser.getTexture());
GL11.glPopMatrix();
//GL11.glEnable(GL11.GL_LIGHTING);
GL11.glPopAttrib();
GL11.glPopMatrix();
}
}
}