made pipe items speed based only

This commit is contained in:
Ellpeck 2020-04-15 23:13:05 +02:00
parent 9ea6f7353f
commit fffcb9da6d
2 changed files with 49 additions and 47 deletions

View file

@ -27,8 +27,6 @@ import java.util.function.Consumer;
public class PipeItem implements INBTSerializable<CompoundNBT> {
public static final int PIPE_TIME = 20;
public ItemStack stack;
public float x;
public float y;
@ -42,17 +40,13 @@ public class PipeItem implements INBTSerializable<CompoundNBT> {
private BlockPos startInventory;
private BlockPos destPipe;
private BlockPos destInventory;
private int pipeTimer;
private BlockPos currGoalPos;
private int currentTile;
private boolean dropOnObstruction;
private long lastWorldTick;
public PipeItem(ItemStack stack, BlockPos startPipe, BlockPos startInventory) {
public PipeItem(ItemStack stack) {
this.stack = stack;
this.startInventory = startInventory;
this.x = MathHelper.lerp(0.5F, startInventory.getX(), startPipe.getX()) + 0.5F;
this.y = MathHelper.lerp(0.5F, startInventory.getY(), startPipe.getY()) + 0.5F;
this.z = MathHelper.lerp(0.5F, startInventory.getZ(), startPipe.getZ()) + 0.5F;
}
public PipeItem(CompoundNBT nbt) {
@ -60,13 +54,21 @@ public class PipeItem implements INBTSerializable<CompoundNBT> {
this.deserializeNBT(nbt);
}
public void setDestination(BlockPos startPipe, BlockPos destPipe, BlockPos destInventory, GraphPath<BlockPos, NetworkEdge> path) {
public void setDestination(BlockPos startPipe, BlockPos startInventory, BlockPos destPipe, BlockPos destInventory, GraphPath<BlockPos, NetworkEdge> path) {
this.startPipe = startPipe;
this.startInventory = startInventory;
this.destPipe = destPipe;
this.destInventory = destInventory;
this.currGoalPos = startPipe;
this.path = compilePath(path);
this.currentTile = 0;
this.pipeTimer = 0;
// initialize position if new
if (this.x == 0 && this.y == 0 && this.z == 0) {
this.x = MathHelper.lerp(0.5F, startInventory.getX(), startPipe.getX()) + 0.5F;
this.y = MathHelper.lerp(0.5F, startInventory.getY(), startPipe.getY()) + 0.5F;
this.z = MathHelper.lerp(0.5F, startInventory.getZ(), startPipe.getZ()) + 0.5F;
}
}
public void updateInPipe(PipeTileEntity currPipe) {
@ -77,9 +79,9 @@ public class PipeItem implements INBTSerializable<CompoundNBT> {
return;
this.lastWorldTick = worldTick;
this.pipeTimer++;
BlockPos goalPos;
if (this.pipeTimer >= PIPE_TIME) {
float speed = 0.05F;
BlockPos myPos = new BlockPos(this.x, this.y, this.z);
if (!myPos.equals(currPipe.getPos()) && !myPos.equals(this.startInventory)) {
// we're done with the current pipe, so switch to the next one
currPipe.items.remove(this);
PipeTileEntity next = this.getNextTile(currPipe, true);
@ -97,15 +99,15 @@ public class PipeItem implements INBTSerializable<CompoundNBT> {
return;
} else {
next.items.add(this);
this.pipeTimer = 0;
goalPos = next.getPos();
}
} else if (this.pipeTimer > PIPE_TIME / 2) {
} else {
double dist = new Vec3d(this.currGoalPos).squareDistanceTo(this.x - 0.5F, this.y - 0.5F, this.z - 0.5F);
if (dist < speed * speed) {
// we're past the start of the pipe, so move to the center of the next pipe
PipeTileEntity next = this.getNextTile(currPipe, false);
if (next == null) {
if (this.reachedDestination()) {
goalPos = this.destInventory;
this.currGoalPos = this.destInventory;
} else {
currPipe.items.remove(this);
if (!currPipe.getWorld().isRemote)
@ -113,28 +115,26 @@ public class PipeItem implements INBTSerializable<CompoundNBT> {
return;
}
} else {
goalPos = next.getPos();
this.currGoalPos = next.getPos();
}
}
} else {
// we're at the start of the pipe, so just move towards its center
goalPos = currPipe.getPos();
}
this.lastX = this.x;
this.lastY = this.y;
this.lastZ = this.z;
Vec3d dist = new Vec3d(goalPos.getX() + 0.5F - this.x, goalPos.getY() + 0.5F - this.y, goalPos.getZ() + 0.5F - this.z);
Vec3d dist = new Vec3d(this.currGoalPos.getX() + 0.5F - this.x, this.currGoalPos.getY() + 0.5F - this.y, this.currGoalPos.getZ() + 0.5F - this.z);
dist = dist.normalize();
this.x += dist.x / PIPE_TIME;
this.y += dist.y / PIPE_TIME;
this.z += dist.z / PIPE_TIME;
this.x += dist.x * speed;
this.y += dist.y * speed;
this.z += dist.z * speed;
}
private void onPathObstructed(PipeTileEntity currPipe, boolean tryReturn) {
if (!this.dropOnObstruction && tryReturn) {
PipeNetwork network = PipeNetwork.get(currPipe.getWorld());
if (network.routeItemToLocation(currPipe.getPos(), this.startPipe, this.startInventory, () -> this)) {
if (network.routeItemToLocation(currPipe.getPos(), this.destInventory, this.startPipe, this.startInventory, () -> this)) {
this.dropOnObstruction = true;
return;
}
@ -177,11 +177,11 @@ public class PipeItem implements INBTSerializable<CompoundNBT> {
CompoundNBT nbt = new CompoundNBT();
nbt.put("stack", this.stack.serializeNBT());
nbt.put("start_pipe", NBTUtil.writeBlockPos(this.startPipe));
nbt.put("start_inventory", NBTUtil.writeBlockPos(this.startInventory));
nbt.put("start_inv", NBTUtil.writeBlockPos(this.startInventory));
nbt.put("dest_pipe", NBTUtil.writeBlockPos(this.destPipe));
nbt.put("dest_inv", NBTUtil.writeBlockPos(this.destInventory));
nbt.put("curr_goal", NBTUtil.writeBlockPos(this.currGoalPos));
nbt.putBoolean("drop_on_obstruction", this.dropOnObstruction);
nbt.putInt("timer", this.pipeTimer);
nbt.putInt("tile", this.currentTile);
nbt.putFloat("x", this.x);
nbt.putFloat("y", this.y);
@ -197,11 +197,11 @@ public class PipeItem implements INBTSerializable<CompoundNBT> {
public void deserializeNBT(CompoundNBT nbt) {
this.stack = ItemStack.read(nbt.getCompound("stack"));
this.startPipe = NBTUtil.readBlockPos(nbt.getCompound("start_pipe"));
this.startInventory = NBTUtil.readBlockPos(nbt.getCompound("start_inventory"));
this.startInventory = NBTUtil.readBlockPos(nbt.getCompound("start_inv"));
this.destPipe = NBTUtil.readBlockPos(nbt.getCompound("dest_pipe"));
this.destInventory = NBTUtil.readBlockPos(nbt.getCompound("dest_inv"));
this.currGoalPos = NBTUtil.readBlockPos(nbt.getCompound("curr_goal"));
this.dropOnObstruction = nbt.getBoolean("drop_on_obstruction");
this.pipeTimer = nbt.getInt("timer");
this.currentTile = nbt.getInt("tile");
this.x = nbt.getFloat("x");
this.y = nbt.getFloat("y");

View file

@ -111,11 +111,11 @@ public class PipeNetwork implements ICapabilitySerializable<CompoundNBT>, GraphL
this.refreshNode(edge.endPipe, this.world.getBlockState(edge.endPipe));
}
public boolean tryInsertItem(BlockPos startPipePos, BlockPos originInv, ItemStack stack) {
return this.routeItem(startPipePos, stack, () -> new PipeItem(stack, startPipePos, originInv));
public boolean tryInsertItem(BlockPos startPipePos, BlockPos startInventory, ItemStack stack) {
return this.routeItem(startPipePos, startInventory, stack, () -> new PipeItem(stack));
}
public boolean routeItem(BlockPos startPipePos, ItemStack stack, Supplier<PipeItem> itemSupplier) {
public boolean routeItem(BlockPos startPipePos, BlockPos startInventory, ItemStack stack, Supplier<PipeItem> itemSupplier) {
if (!this.isNode(startPipePos))
return false;
if (!this.world.isBlockLoaded(startPipePos))
@ -127,12 +127,12 @@ public class PipeNetwork implements ICapabilitySerializable<CompoundNBT>, GraphL
PipeTileEntity pipe = this.getPipe(pipePos);
BlockPos dest = pipe.getAvailableDestination(stack);
if (dest != null)
return this.routeItemToLocation(startPipePos, pipe.getPos(), dest, itemSupplier);
return this.routeItemToLocation(startPipePos, startInventory, pipe.getPos(), dest, itemSupplier);
}
return false;
}
public boolean routeItemToLocation(BlockPos startPipePos, BlockPos destPipe, BlockPos destInventory, Supplier<PipeItem> itemSupplier) {
public boolean routeItemToLocation(BlockPos startPipePos, BlockPos startInventory, BlockPos destPipe, BlockPos destInventory, Supplier<PipeItem> itemSupplier) {
if (!this.isNode(startPipePos))
return false;
if (!this.world.isBlockLoaded(startPipePos))
@ -141,8 +141,10 @@ public class PipeNetwork implements ICapabilitySerializable<CompoundNBT>, GraphL
if (startPipe == null)
return false;
GraphPath<BlockPos, NetworkEdge> path = this.dijkstra.getPath(startPipePos, destPipe);
if (path == null)
return false;
PipeItem item = itemSupplier.get();
item.setDestination(startPipePos, destPipe, destInventory, path);
item.setDestination(startPipePos, startInventory, destPipe, destInventory, path);
if (!startPipe.items.contains(item))
startPipe.items.add(item);
PacketHandler.sendToAllLoaded(this.world, startPipePos, new PacketItemEnterPipe(startPipePos, item));