From 6d5942bc8ae0382ba47a2c2e77c74002a20612f9 Mon Sep 17 00:00:00 2001 From: psxlover Date: Sun, 30 Sep 2012 22:42:09 +0300 Subject: [PATCH] Refactored lasers. Moved PowerLaser logic from EntityLaser. --- common/buildcraft/builders/TileBuilder.java | 2 +- .../buildcraft/builders/TilePathMarker.java | 2 +- common/buildcraft/core/EntityEnergyLaser.java | 21 ++-- common/buildcraft/core/EntityLaser.java | 116 ++++++++---------- common/buildcraft/core/EntityPowerLaser.java | 28 +++++ .../buildcraft/core/render/RenderLaser.java | 5 +- common/buildcraft/silicon/TileLaser.java | 4 +- 7 files changed, 95 insertions(+), 83 deletions(-) diff --git a/common/buildcraft/builders/TileBuilder.java b/common/buildcraft/builders/TileBuilder.java index f472838d..731f36d9 100644 --- a/common/buildcraft/builders/TileBuilder.java +++ b/common/buildcraft/builders/TileBuilder.java @@ -235,7 +235,7 @@ public class TileBuilder extends TileBuildCraft implements IBuilderInventory, IP for (BlockIndex b : path) { if (previous != null) { - EntityLaser laser = new EntityPowerLaser(worldObj, + EntityPowerLaser laser = new EntityPowerLaser(worldObj, new Position(previous.i + 0.5, previous.j + 0.5, previous.k + 0.5), new Position(b.i + 0.5, b.j + 0.5, b.k + 0.5)); diff --git a/common/buildcraft/builders/TilePathMarker.java b/common/buildcraft/builders/TilePathMarker.java index a9c65647..e10085c4 100644 --- a/common/buildcraft/builders/TilePathMarker.java +++ b/common/buildcraft/builders/TilePathMarker.java @@ -60,7 +60,7 @@ public class TilePathMarker extends TileMarker { if (CoreProxy.proxy.isRenderWorld(worldObj)) return; - EntityLaser laser = new EntityPowerLaser(worldObj, new Position(xCoord + 0.5, yCoord + 0.5, zCoord + 0.5), new Position(pathMarker.xCoord + 0.5, pathMarker.yCoord + 0.5, pathMarker.zCoord + 0.5)); + EntityPowerLaser laser = new EntityPowerLaser(worldObj, new Position(xCoord + 0.5, yCoord + 0.5, zCoord + 0.5), new Position(pathMarker.xCoord + 0.5, pathMarker.yCoord + 0.5, pathMarker.zCoord + 0.5)); laser.show(); laser.setTexture(DefaultProps.TEXTURE_PATH_ENTITIES + "/laser_1.png"); diff --git a/common/buildcraft/core/EntityEnergyLaser.java b/common/buildcraft/core/EntityEnergyLaser.java index 3dc5b5c6..b0c9d456 100644 --- a/common/buildcraft/core/EntityEnergyLaser.java +++ b/common/buildcraft/core/EntityEnergyLaser.java @@ -58,27 +58,20 @@ public class EntityEnergyLaser extends EntityLaser { } @Override - protected void updateData() { - super.updateData(); - + protected void updateDataClient() { + super.updateDataClient(); powerAverage = (float)decodeDouble(dataWatcher.getWatchableObjectInt(15)); } - + @Override - public void setPositions(Position head, Position tail) { - super.setPositions(head, tail); + protected void updateDataServer() { + super.updateDataServer(); dataWatcher.updateObject(15, Integer.valueOf(encodeDouble((double)powerAverage))); } @Override - protected void initClientSide() { - super.initClientSide(); + protected void entityInit() { + super.entityInit(); dataWatcher.addObject(15, Integer.valueOf(0)); } - - @Override - protected void initServerSide() { - super.initServerSide(); - dataWatcher.addObject(15, encodeDouble((double)powerAverage)); - } } diff --git a/common/buildcraft/core/EntityLaser.java b/common/buildcraft/core/EntityLaser.java index 534cf77e..3a68c453 100644 --- a/common/buildcraft/core/EntityLaser.java +++ b/common/buildcraft/core/EntityLaser.java @@ -16,19 +16,22 @@ import net.minecraft.src.Entity; import net.minecraft.src.NBTTagCompound; import net.minecraft.src.World; -public class EntityLaser extends Entity { +public abstract class EntityLaser extends Entity { protected Position head, tail; public double renderSize = 0; public double angleY = 0; public double angleZ = 0; - protected String texture; + + private boolean isVisible = false; + protected boolean needsUpdate = true; public EntityLaser(World world) { super(world); - initClientSide(); + head = new Position(0, 0, 0); + tail = new Position(0, 0, 0); } public EntityLaser(World world, Position head, Position tail) { @@ -39,46 +42,23 @@ public class EntityLaser extends Entity { this.tail = tail; setPositionAndRotation(head.x, head.y, head.z, 0, 0); - - initServerSide(); + setSize(10, 10); } - protected void initClientSide() { - - head = new Position(0, 0, 0); - tail = new Position(0, 0, 0); - - dataWatcher.addObject(8 , Integer.valueOf(0)); - dataWatcher.addObject(9 , Integer.valueOf(0)); + @Override + protected void entityInit() { + preventEntitySpawning = false; + noClip = true; + isImmuneToFire = true; + + dataWatcher.addObject(8, Integer.valueOf(0)); + dataWatcher.addObject(9, Integer.valueOf(0)); dataWatcher.addObject(10, Integer.valueOf(0)); dataWatcher.addObject(11, Integer.valueOf(0)); dataWatcher.addObject(12, Integer.valueOf(0)); dataWatcher.addObject(13, Integer.valueOf(0)); dataWatcher.addObject(14, Byte.valueOf((byte) 0)); - - dataWatcher.addObject(16, ""); - } - - protected void initServerSide() { - - preventEntitySpawning = false; - noClip = true; - isImmuneToFire = true; - - setPositionAndRotation(head.x, head.y, head.z, 0, 0); - setSize(10, 10); - - dataWatcher.addObject(8 , Integer.valueOf(encodeDouble(head.x))); - dataWatcher.addObject(9 , Integer.valueOf(encodeDouble(head.y))); - dataWatcher.addObject(10, Integer.valueOf(encodeDouble(head.z))); - dataWatcher.addObject(11, Integer.valueOf(encodeDouble(tail.x))); - dataWatcher.addObject(12, Integer.valueOf(encodeDouble(tail.y))); - dataWatcher.addObject(13, Integer.valueOf(encodeDouble(tail.z))); - - dataWatcher.addObject(14, Byte.valueOf((byte) 0)); - - dataWatcher.addObject(16, ""); } @Override @@ -87,8 +67,13 @@ public class EntityLaser extends Entity { if (head == null || tail == null) return; + if (CoreProxy.proxy.isSimulating(worldObj) && needsUpdate) { + updateDataServer(); + needsUpdate = false; + } + if (CoreProxy.proxy.isRenderWorld(worldObj)) - updateData(); + updateDataClient(); boundingBox.minX = Math.min(head.x, tail.x); boundingBox.minY = Math.min(head.y, tail.y); @@ -116,66 +101,62 @@ public class EntityLaser extends Entity { angleY = -Math.atan2(dy, dx) * 180 / Math.PI; } - protected void updateData() { - + protected void updateDataClient() { head.x = decodeDouble(dataWatcher.getWatchableObjectInt(8)); head.y = decodeDouble(dataWatcher.getWatchableObjectInt(9)); head.z = decodeDouble(dataWatcher.getWatchableObjectInt(10)); tail.x = decodeDouble(dataWatcher.getWatchableObjectInt(11)); tail.y = decodeDouble(dataWatcher.getWatchableObjectInt(12)); tail.z = decodeDouble(dataWatcher.getWatchableObjectInt(13)); - texture = dataWatcher.getWatchableObjectString(16); + + isVisible = (dataWatcher.getWatchableObjectByte(14) == 1); } - public void setPositions(Position head, Position tail) { - - this.head = head; - this.tail = tail; - - setPositionAndRotation(head.x, head.y, head.z, 0, 0); - - dataWatcher.updateObject(8 , Integer.valueOf(encodeDouble(head.x))); - dataWatcher.updateObject(9 , Integer.valueOf(encodeDouble(head.y))); + protected void updateDataServer() { + dataWatcher.updateObject(8, Integer.valueOf(encodeDouble(head.x))); + dataWatcher.updateObject(9, Integer.valueOf(encodeDouble(head.y))); dataWatcher.updateObject(10, Integer.valueOf(encodeDouble(head.z))); dataWatcher.updateObject(11, Integer.valueOf(encodeDouble(tail.x))); dataWatcher.updateObject(12, Integer.valueOf(encodeDouble(tail.y))); dataWatcher.updateObject(13, Integer.valueOf(encodeDouble(tail.z))); - onUpdate(); + dataWatcher.updateObject(14, Byte.valueOf((byte) (isVisible ? 1 : 0))); + } + + public void setPositions(Position head, Position tail) { + this.head = head; + this.tail = tail; + + setPositionAndRotation(head.x, head.y, head.z, 0, 0); + + needsUpdate = true; } public void show() { - dataWatcher.updateObject(14, Byte.valueOf((byte) 1)); + isVisible = true; + needsUpdate = true; } public void hide() { - dataWatcher.updateObject(14, Byte.valueOf((byte) 0)); + isVisible = false; + needsUpdate = true; } public boolean isVisible() { - return dataWatcher.getWatchableObjectByte(14) == 0 ? false : true; + return this.isVisible; } - public void setTexture(String texture) { - this.texture = texture; - dataWatcher.updateObject(16, texture); - } - - public String getTexture() { - return texture; - } + public abstract String getTexture(); protected int encodeDouble(double d) { - return (int) (d * 8000); + return (int) (d * 8192); } protected double decodeDouble(int i) { - return (i / 8000D); + return (i / 8192D); } - @Override - protected void entityInit() {} - + //The read/write to nbt seem to be useless @Override protected void readEntityFromNBT(NBTTagCompound nbt) { @@ -201,4 +182,9 @@ public class EntityLaser extends Entity { nbt.setDouble("tailY", tail.y); nbt.setDouble("tailZ", tail.z); } + + //Workaround for the laser's posY loosing it's precision e.g 103.5 becomes 104 + public Position renderOffset() { + return new Position(head.x - posX, head.y - posY, head.z - posZ); + } } diff --git a/common/buildcraft/core/EntityPowerLaser.java b/common/buildcraft/core/EntityPowerLaser.java index 25563658..9d88c2a4 100644 --- a/common/buildcraft/core/EntityPowerLaser.java +++ b/common/buildcraft/core/EntityPowerLaser.java @@ -4,6 +4,7 @@ import net.minecraft.src.World; import buildcraft.api.core.Position; public class EntityPowerLaser extends EntityLaser { + private String texture; public EntityPowerLaser(World world) { super(world); @@ -13,4 +14,31 @@ public class EntityPowerLaser extends EntityLaser { super(world, head, tail); } + @Override + protected void entityInit() { + super.entityInit(); + dataWatcher.addObject(15, ""); + } + + @Override + public String getTexture() { + return texture; + } + + public void setTexture(String texture) { + this.texture = texture; + needsUpdate = true; + } + + @Override + protected void updateDataClient() { + super.updateDataClient(); + texture = dataWatcher.getWatchableObjectString(15); + } + + @Override + protected void updateDataServer() { + super.updateDataServer(); + dataWatcher.updateObject(15, texture); + } } diff --git a/common/buildcraft/core/render/RenderLaser.java b/common/buildcraft/core/render/RenderLaser.java index eeb6bea7..70a8f09b 100644 --- a/common/buildcraft/core/render/RenderLaser.java +++ b/common/buildcraft/core/render/RenderLaser.java @@ -1,5 +1,6 @@ package buildcraft.core.render; +import buildcraft.api.core.Position; import net.minecraft.src.Entity; import net.minecraft.src.ModelBase; import net.minecraft.src.ModelRenderer; @@ -38,7 +39,9 @@ public class RenderLaser extends Render { GL11.glPushMatrix(); GL11.glDisable(2896 /* GL_LIGHTING */); - GL11.glTranslated(x, y, z); + + Position offset = laser.renderOffset(); + GL11.glTranslated(x + offset.x, y + offset.y, z + offset.z); GL11.glRotatef((float) laser.angleZ, 0, 1, 0); GL11.glRotatef((float) laser.angleY, 0, 0, 1); diff --git a/common/buildcraft/silicon/TileLaser.java b/common/buildcraft/silicon/TileLaser.java index d5977834..f6ba5561 100644 --- a/common/buildcraft/silicon/TileLaser.java +++ b/common/buildcraft/silicon/TileLaser.java @@ -207,7 +207,9 @@ public class TileLaser extends TileBuildCraft implements IPowerReceptor { assemblyTable.zCoord + 0.475 + (worldObj.rand.nextFloat() - 0.5) / 5F); laser.setPositions(head, tail); - laser.show(); + + if(!laser.isVisible()) + laser.show(); } protected void removeLaser() {