mirror of
https://github.com/HbmMods/Hbm-s-Nuclear-Tech-GIT.git
synced 2026-02-24 15:00:48 +00:00
EntityRailCarBase
This commit is contained in:
parent
b6ae5565bd
commit
dd2f929821
@ -1,5 +1,7 @@
|
|||||||
package com.hbm.blocks.machine;
|
package com.hbm.blocks.machine;
|
||||||
|
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
import com.hbm.blocks.BlockDummyable;
|
import com.hbm.blocks.BlockDummyable;
|
||||||
import com.hbm.blocks.ModBlocks;
|
import com.hbm.blocks.ModBlocks;
|
||||||
import com.hbm.handler.MultiblockHandlerXR;
|
import com.hbm.handler.MultiblockHandlerXR;
|
||||||
@ -11,6 +13,7 @@ import net.minecraft.block.Block;
|
|||||||
import net.minecraft.block.material.Material;
|
import net.minecraft.block.material.Material;
|
||||||
import net.minecraft.entity.item.EntityItem;
|
import net.minecraft.entity.item.EntityItem;
|
||||||
import net.minecraft.entity.player.EntityPlayer;
|
import net.minecraft.entity.player.EntityPlayer;
|
||||||
|
import net.minecraft.item.Item;
|
||||||
import net.minecraft.item.ItemStack;
|
import net.minecraft.item.ItemStack;
|
||||||
import net.minecraft.tileentity.TileEntity;
|
import net.minecraft.tileentity.TileEntity;
|
||||||
import net.minecraft.world.World;
|
import net.minecraft.world.World;
|
||||||
@ -30,6 +33,11 @@ public class Watz extends BlockDummyable {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Item getItemDropped(int i, Random rand, int j) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int side, float hitX, float hitY, float hitZ) {
|
public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int side, float hitX, float hitY, float hitZ) {
|
||||||
return super.standardOpenBehavior(world, x, y, z, player, 0);
|
return super.standardOpenBehavior(world, x, y, z, player, 0);
|
||||||
|
|||||||
@ -1,8 +1,11 @@
|
|||||||
package com.hbm.blocks.rail;
|
package com.hbm.blocks.rail;
|
||||||
|
|
||||||
|
import com.hbm.util.fauxpointtwelve.BlockPos;
|
||||||
|
|
||||||
import net.minecraft.util.Vec3;
|
import net.minecraft.util.Vec3;
|
||||||
import net.minecraft.world.World;
|
import net.minecraft.world.World;
|
||||||
|
|
||||||
|
/** in retrospect, not the best name i could have chosen */
|
||||||
public interface IRailNTM {
|
public interface IRailNTM {
|
||||||
|
|
||||||
/** Returns a vector pointing to the closest snapping position given the starting position */
|
/** Returns a vector pointing to the closest snapping position given the starting position */
|
||||||
@ -16,12 +19,22 @@ public interface IRailNTM {
|
|||||||
* Motion ends up being *-1 if the train is going in reverse, still pointing forwards despite the speed being negative.
|
* Motion ends up being *-1 if the train is going in reverse, still pointing forwards despite the speed being negative.
|
||||||
* Also features a double[] wrapper with size 1 which holds the speed value that overshoots the rail.
|
* Also features a double[] wrapper with size 1 which holds the speed value that overshoots the rail.
|
||||||
* */
|
* */
|
||||||
public Vec3 getTravelLocation(World world, int x, int y, int z, double trainX, double trainY, double trainZ, double motionX, double motionY, double motionZ, double speed, double[] leftover);
|
public Vec3 getTravelLocation(World world, int x, int y, int z, double trainX, double trainY, double trainZ, double motionX, double motionY, double motionZ, double speed, RailLeaveInfo info);
|
||||||
|
|
||||||
/** Returns that rail'S gauge. Trains will derail if the gauge does not match. */
|
/** Returns that rail's gauge. Trains will derail if the gauge does not match. */
|
||||||
public TrackGauge getGauge(World world, int x, int y, int z);
|
public TrackGauge getGauge(World world, int x, int y, int z);
|
||||||
|
|
||||||
public static enum TrackGauge {
|
public static enum TrackGauge {
|
||||||
STANDARD //roughly 1.5m
|
STANDARD //roughly 1.5m
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** A wrapper for all relevant info required when leaving a rail */
|
||||||
|
public static class RailLeaveInfo {
|
||||||
|
/** The amount of blocks still left to travel after completing the rail */
|
||||||
|
public double overshoot;
|
||||||
|
/** The exit position of that rail */
|
||||||
|
public BlockPos pos;
|
||||||
|
public RailLeaveInfo dist(double d) { this.overshoot = d; return this; }
|
||||||
|
public RailLeaveInfo pos(BlockPos d) { this.pos = d; return this; }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,6 +2,7 @@ package com.hbm.blocks.rail;
|
|||||||
|
|
||||||
import com.hbm.blocks.BlockDummyable;
|
import com.hbm.blocks.BlockDummyable;
|
||||||
import com.hbm.lib.Library;
|
import com.hbm.lib.Library;
|
||||||
|
import com.hbm.util.fauxpointtwelve.BlockPos;
|
||||||
|
|
||||||
import net.minecraft.block.material.Material;
|
import net.minecraft.block.material.Material;
|
||||||
import net.minecraft.tileentity.TileEntity;
|
import net.minecraft.tileentity.TileEntity;
|
||||||
@ -47,16 +48,16 @@ public class RailStandardStraight extends BlockDummyable implements IRailNTM {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Vec3 getSnappingPos(World world, int x, int y, int z, double trainX, double trainY, double trainZ) {
|
public Vec3 getSnappingPos(World world, int x, int y, int z, double trainX, double trainY, double trainZ) {
|
||||||
return snapAndMove(world, x, y, z, trainX, trainY, trainZ, 0, 0, 0, 0, new double[1]);
|
return snapAndMove(world, x, y, z, trainX, trainY, trainZ, 0, 0, 0, 0, new RailLeaveInfo());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Vec3 getTravelLocation(World world, int x, int y, int z, double trainX, double trainY, double trainZ, double motionX, double motionY, double motionZ, double speed, double[] leftover) {
|
public Vec3 getTravelLocation(World world, int x, int y, int z, double trainX, double trainY, double trainZ, double motionX, double motionY, double motionZ, double speed, RailLeaveInfo info) {
|
||||||
return snapAndMove(world, x, y, z, trainX, trainY, trainZ, motionX, motionY, motionZ, speed, leftover);
|
return snapAndMove(world, x, y, z, trainX, trainY, trainZ, motionX, motionY, motionZ, speed, info);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Very simple function determining the snapping position and adding the motion value to it, if desired. */
|
/* Very simple function determining the snapping position and adding the motion value to it, if desired. */
|
||||||
public Vec3 snapAndMove(World world, int x, int y, int z, double trainX, double trainY, double trainZ, double motionX, double motionY, double motionZ, double speed, double[] leftover) {
|
public Vec3 snapAndMove(World world, int x, int y, int z, double trainX, double trainY, double trainZ, double motionX, double motionY, double motionZ, double speed, RailLeaveInfo info) {
|
||||||
int[] pos = this.findCore(world, x, y, z);
|
int[] pos = this.findCore(world, x, y, z);
|
||||||
if(pos == null) return Vec3.createVectorHelper(trainX, trainY, trainZ);
|
if(pos == null) return Vec3.createVectorHelper(trainX, trainY, trainZ);
|
||||||
int cX = pos[0];
|
int cX = pos[0];
|
||||||
@ -81,7 +82,8 @@ public class RailStandardStraight extends BlockDummyable implements IRailNTM {
|
|||||||
vec.xCoord = MathHelper.clamp_double(targetX, x - 2, x + 3);
|
vec.xCoord = MathHelper.clamp_double(targetX, x - 2, x + 3);
|
||||||
vec.yCoord = y;
|
vec.yCoord = y;
|
||||||
vec.zCoord = z + 0.5;
|
vec.zCoord = z + 0.5;
|
||||||
leftover[0] = Math.abs(targetX - vec.xCoord);
|
info.dist(Math.abs(targetX - vec.xCoord));
|
||||||
|
info.pos(new BlockPos(vec.xCoord + (motionX > 0 ? 1 : -1), y, z));
|
||||||
} else {
|
} else {
|
||||||
double targetZ = trainZ;
|
double targetZ = trainZ;
|
||||||
if(motionZ > 0) {
|
if(motionZ > 0) {
|
||||||
@ -92,7 +94,8 @@ public class RailStandardStraight extends BlockDummyable implements IRailNTM {
|
|||||||
vec.xCoord = x + 0.5;
|
vec.xCoord = x + 0.5;
|
||||||
vec.yCoord = y;
|
vec.yCoord = y;
|
||||||
vec.zCoord = MathHelper.clamp_double(targetZ, z - 2, z + 3);
|
vec.zCoord = MathHelper.clamp_double(targetZ, z - 2, z + 3);
|
||||||
leftover[0] = Math.abs(targetZ - vec.zCoord);
|
info.dist(Math.abs(targetZ - vec.zCoord));
|
||||||
|
info.pos(new BlockPos(x, y, vec.zCoord + (motionZ > 0 ? 1 : -1)));
|
||||||
}
|
}
|
||||||
|
|
||||||
return vec;
|
return vec;
|
||||||
|
|||||||
157
src/main/java/com/hbm/entity/train/EntityRailCarBase.java
Normal file
157
src/main/java/com/hbm/entity/train/EntityRailCarBase.java
Normal file
@ -0,0 +1,157 @@
|
|||||||
|
package com.hbm.entity.train;
|
||||||
|
|
||||||
|
import com.hbm.blocks.rail.IRailNTM;
|
||||||
|
import com.hbm.blocks.rail.IRailNTM.RailLeaveInfo;
|
||||||
|
import com.hbm.blocks.rail.IRailNTM.TrackGauge;
|
||||||
|
import com.hbm.util.fauxpointtwelve.BlockPos;
|
||||||
|
|
||||||
|
import cpw.mods.fml.relauncher.Side;
|
||||||
|
import cpw.mods.fml.relauncher.SideOnly;
|
||||||
|
import net.minecraft.block.Block;
|
||||||
|
import net.minecraft.entity.Entity;
|
||||||
|
import net.minecraft.nbt.NBTTagCompound;
|
||||||
|
import net.minecraft.util.MathHelper;
|
||||||
|
import net.minecraft.util.Vec3;
|
||||||
|
import net.minecraft.world.World;
|
||||||
|
|
||||||
|
public abstract class EntityRailCarBase extends Entity {
|
||||||
|
|
||||||
|
public boolean isOnRail = true;
|
||||||
|
private int turnProgress;
|
||||||
|
private double trainX;
|
||||||
|
private double trainY;
|
||||||
|
private double trainZ;
|
||||||
|
private double trainYaw;
|
||||||
|
private double trainPitch;
|
||||||
|
@SideOnly(Side.CLIENT) private double velocityX;
|
||||||
|
@SideOnly(Side.CLIENT) private double velocityY;
|
||||||
|
@SideOnly(Side.CLIENT) private double velocityZ;
|
||||||
|
|
||||||
|
public EntityRailCarBase(World world) {
|
||||||
|
super(world);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override protected void entityInit() { }
|
||||||
|
@Override protected void readEntityFromNBT(NBTTagCompound nbt) { }
|
||||||
|
@Override protected void writeEntityToNBT(NBTTagCompound nbt) { }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onUpdate() {
|
||||||
|
|
||||||
|
if(this.worldObj.isRemote) {
|
||||||
|
if(this.turnProgress > 0) {
|
||||||
|
double x = this.posX + (this.trainX - this.posX) / (double) this.turnProgress;
|
||||||
|
double y = this.posY + (this.trainY - this.posY) / (double) this.turnProgress;
|
||||||
|
double z = this.posZ + (this.trainZ - this.posZ) / (double) this.turnProgress;
|
||||||
|
double yaw = MathHelper.wrapAngleTo180_double(this.trainYaw - (double) this.rotationYaw);
|
||||||
|
this.rotationYaw = (float) ((double) this.rotationYaw + yaw / (double) this.turnProgress);
|
||||||
|
this.rotationPitch = (float) ((double) this.rotationPitch + (this.trainPitch - (double) this.rotationPitch) / (double) this.turnProgress);
|
||||||
|
--this.turnProgress;
|
||||||
|
this.setPosition(x, y, z);
|
||||||
|
this.setRotation(this.rotationYaw, this.rotationPitch);
|
||||||
|
} else {
|
||||||
|
this.setPosition(this.posX, this.posY, this.posZ);
|
||||||
|
this.setRotation(this.rotationYaw, this.rotationPitch);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
|
||||||
|
BlockPos anchor = this.getCurentAnchorPos();
|
||||||
|
Vec3 corePos = getRelPosAlongRail(anchor, this.getCurrentSpeed());
|
||||||
|
|
||||||
|
if(corePos == null) {
|
||||||
|
this.derail();
|
||||||
|
} else {
|
||||||
|
this.setPosition(corePos.xCoord, corePos.yCoord, corePos.zCoord);
|
||||||
|
|
||||||
|
anchor = this.getCurentAnchorPos(); //reset origin to new position
|
||||||
|
Vec3 frontPos = getRelPosAlongRail(anchor, this.getLengthSpan());
|
||||||
|
Vec3 backPos = getRelPosAlongRail(anchor, -this.getLengthSpan());
|
||||||
|
|
||||||
|
if(frontPos == null) this.derail();
|
||||||
|
if(backPos == null) this.derail();
|
||||||
|
|
||||||
|
if(frontPos != null && backPos != null) this.rotationYaw = generateYaw(frontPos, backPos);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public Vec3 getRelPosAlongRail(BlockPos anchor, double distanceToCover) {
|
||||||
|
|
||||||
|
double overshoot = 0;
|
||||||
|
float yaw = this.rotationYaw;
|
||||||
|
|
||||||
|
Vec3 next = null;
|
||||||
|
|
||||||
|
do {
|
||||||
|
|
||||||
|
int x = anchor.getX();
|
||||||
|
int y = anchor.getY();
|
||||||
|
int z = anchor.getZ();
|
||||||
|
Block block = worldObj.getBlock(x, y, z);
|
||||||
|
|
||||||
|
Vec3 rot = Vec3.createVectorHelper(1, 0, 0);
|
||||||
|
rot.rotateAroundY(yaw);
|
||||||
|
|
||||||
|
if(block instanceof IRailNTM) {
|
||||||
|
IRailNTM rail = (IRailNTM) block;
|
||||||
|
|
||||||
|
if(rail.getGauge(worldObj, x, y, z) == this.getGauge()) {
|
||||||
|
RailLeaveInfo info = new RailLeaveInfo();
|
||||||
|
Vec3 prev = next;
|
||||||
|
next = rail.getTravelLocation(worldObj, x, y, z, posX, posY, posZ, rot.xCoord, rot.yCoord, rot.zCoord, distanceToCover, info);
|
||||||
|
overshoot = info.overshoot;
|
||||||
|
anchor = info.pos;
|
||||||
|
yaw = generateYaw(next, prev);
|
||||||
|
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
} while(overshoot != 0); //if there's still length to cover, keep going
|
||||||
|
|
||||||
|
return next;
|
||||||
|
}
|
||||||
|
|
||||||
|
public float generateYaw(Vec3 front, Vec3 back) {
|
||||||
|
return 0F; //TODO
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Returns the amount of blocks that the train should move per tick */
|
||||||
|
public abstract double getCurrentSpeed();
|
||||||
|
/** Returns the gauge of this train */
|
||||||
|
public abstract TrackGauge getGauge();
|
||||||
|
/** Returns the length between the core and one of the bogies */
|
||||||
|
public abstract double getLengthSpan();
|
||||||
|
|
||||||
|
/** Returns the "true" position of the train, i.e. the block it wants to snap to */
|
||||||
|
public BlockPos getCurentAnchorPos() {
|
||||||
|
return new BlockPos(posX, posY, posZ);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void derail() {
|
||||||
|
isOnRail = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@SideOnly(Side.CLIENT)
|
||||||
|
public void setPositionAndRotation2(double posX, double posY, double posZ, float yaw, float pitch, int turnProg) {
|
||||||
|
this.trainX = posX;
|
||||||
|
this.trainY = posY;
|
||||||
|
this.trainZ = posZ;
|
||||||
|
this.trainYaw = (double) yaw;
|
||||||
|
this.trainPitch = (double) pitch;
|
||||||
|
this.turnProgress = turnProg + 2;
|
||||||
|
this.motionX = this.velocityX;
|
||||||
|
this.motionY = this.velocityY;
|
||||||
|
this.motionZ = this.velocityZ;
|
||||||
|
}
|
||||||
|
|
||||||
|
@SideOnly(Side.CLIENT)
|
||||||
|
public void setVelocity(double mX, double mY, double mZ) {
|
||||||
|
this.velocityX = this.motionX = mX;
|
||||||
|
this.velocityY = this.motionY = mY;
|
||||||
|
this.velocityZ = this.motionZ = mZ;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user