more train coupling stuff

This commit is contained in:
Bob 2023-05-27 23:38:15 +02:00
parent 85ce4008a1
commit e120654403
5 changed files with 58 additions and 7 deletions

View File

@ -1,5 +1,6 @@
package com.hbm.entity.train;
import java.util.ArrayList;
import java.util.List;
import com.hbm.blocks.rail.IRailNTM;
@ -93,6 +94,7 @@ public abstract class EntityRailCarBase extends Entity {
if(neighbor.getCoupledTo(closestNeighborCoupling) != null) continue;
this.couple(closestOwnCoupling, neighbor);
neighbor.couple(closestNeighborCoupling, this);
player.swingItem();
return true;
}
}
@ -282,10 +284,14 @@ public abstract class EntityRailCarBase extends Entity {
public abstract TrackGauge getGauge();
/** Returns the length between the core and one of the bogies */
public abstract double getLengthSpan();
/* Returns a collision box, usually smaller than the entity's AABB for rendering, which is used for colliding trains */
/** Returns a collision box, usually smaller than the entity's AABB for rendering, which is used for colliding trains */
public AxisAlignedBB getCollisionBox() {
return this.boundingBox;
}
/** Returns a collision box used for block collisions when derailed */
@Override public AxisAlignedBB getBoundingBox() {
return this.boundingBox;
}
/** Returns the "true" position of the train, i.e. the block it wants to snap to */
public BlockPos getCurentAnchorPos() {
@ -417,6 +423,8 @@ public abstract class EntityRailCarBase extends Entity {
if(dist <= 0) return null;
if(coupling == TrainCoupling.BACK) dist *= -1;
Vec3 rot = Vec3.createVectorHelper(0, 0, dist);
rot.rotateAroundY((float) (-this.rotationYaw * Math.PI / 180D));
rot.xCoord += this.renderX;
@ -433,4 +441,11 @@ public abstract class EntityRailCarBase extends Entity {
if(coupling == TrainCoupling.FRONT) this.coupledFront = to;
if(coupling == TrainCoupling.BACK) this.coupledBack = to;
}
public static class LogicalTrainUnit {
List<EntityRailCarBase> trains = new ArrayList();
//TBI
}
}

View File

@ -129,7 +129,7 @@ public abstract class EntityRailCarRidable extends EntityRailCarCargo {
if(nearestSeat == -1) {
player.mountEntity(this);
} else {
SeatDummyEntity dummySeat = new SeatDummyEntity(worldObj, this);
SeatDummyEntity dummySeat = new SeatDummyEntity(worldObj, this, nearestSeat);
Vec3 passengerSeat = this.getPassengerSeats()[nearestSeat];
passengerSeat.rotateAroundY((float) (-this.rotationYaw * Math.PI / 180));
double x = renderX + passengerSeat.xCoord;
@ -194,16 +194,17 @@ public abstract class EntityRailCarRidable extends EntityRailCarCargo {
private double trainX;
private double trainY;
private double trainZ;
public EntityRailCarBase train;
public EntityRailCarRidable train;
public SeatDummyEntity(World world) { super(world); this.setSize(0.5F, 0.1F);}
public SeatDummyEntity(World world, EntityRailCarBase train) {
public SeatDummyEntity(World world, EntityRailCarRidable train, int index) {
this(world);
this.train = train;
if(train != null) this.dataWatcher.updateObject(3, train.getEntityId());
this.dataWatcher.updateObject(4, index);
}
@Override protected void entityInit() { this.dataWatcher.addObject(3, new Integer(0)); }
@Override protected void entityInit() { this.dataWatcher.addObject(3, new Integer(0)); this.dataWatcher.addObject(4, new Integer(0)); }
@Override protected void writeEntityToNBT(NBTTagCompound nbt) { }
@Override public boolean writeToNBTOptional(NBTTagCompound nbt) { return false; }
@Override public void readEntityFromNBT(NBTTagCompound nbt) { this.setDead(); }
@ -238,7 +239,30 @@ public abstract class EntityRailCarRidable extends EntityRailCarCargo {
@Override
public void updateRiderPosition() {
if(this.riddenByEntity != null) {
this.riddenByEntity.setPosition(this.posX, this.posY + 1, this.posZ);
if(train == null) {
int eid = this.dataWatcher.getWatchableObjectInt(3);
Entity entity = worldObj.getEntityByID(eid);
if(entity instanceof EntityRailCarRidable) {
train = (EntityRailCarRidable) entity;
}
}
//fallback for when train is null
if(train == null) {
this.riddenByEntity.setPosition(posX, posY + 1, posZ);
return;
}
//doing it like this instead of with the position directly removes any discrepancies caused by entity tick order
//mmhmhmhm silky smooth
int index = this.dataWatcher.getWatchableObjectInt(4);
Vec3 rot = this.train.getPassengerSeats()[index];
rot.rotateAroundY((float) (-train.rotationYaw * Math.PI / 180));
double x = train.renderX + rot.xCoord;
double y = train.renderY + rot.yCoord;
double z = train.renderZ + rot.zCoord;
this.riddenByEntity.setPosition(x, y, z);
}
}
}

View File

@ -51,11 +51,12 @@ public class TrainCargoTram extends EntityRailCarElectric implements IGUIProvide
@Override public TrackGauge getGauge() { return TrackGauge.STANDARD; }
@Override public double getLengthSpan() { return 1.5; }
@Override public Vec3 getRiderSeatPosition() { return Vec3.createVectorHelper(0.375, 2.25, 0.5); }
@Override public Vec3 getRiderSeatPosition() { return Vec3.createVectorHelper(0.375, 2.375, 0.5); }
@Override public boolean shouldRiderSit() { return false; }
@Override public int getSizeInventory() { return 29; }
@Override public String getInventoryName() { return this.hasCustomInventoryName() ? this.getEntityName() : "container.trainTram"; }
@Override public AxisAlignedBB getCollisionBox() { return AxisAlignedBB.getBoundingBox(renderX, renderY, renderZ, renderX, renderY + 1, renderZ).expand(4, 0, 4); }
@Override public double getCouplingDist(TrainCoupling coupling) { return coupling != null ? 2.75 : 0; }
@Override public int getMaxPower() { return this.getPowerConsumption() * 100; }
@Override public int getPowerConsumption() { return 10; }

View File

@ -2,6 +2,7 @@ package com.hbm.entity.train;
import com.hbm.blocks.rail.IRailNTM.TrackGauge;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.util.DamageSource;
import net.minecraft.util.Vec3;
import net.minecraft.world.World;
@ -28,6 +29,8 @@ public class TrainCargoTramTrailer extends EntityRailCarCargo {
@Override public double getLengthSpan() { return 1.5; }
@Override public int getSizeInventory() { return 29; }
@Override public String getInventoryName() { return this.hasCustomInventoryName() ? this.getEntityName() : "container.trainTramTrailer"; }
@Override public AxisAlignedBB getCollisionBox() { return AxisAlignedBB.getBoundingBox(renderX, renderY, renderZ, renderX, renderY + 1, renderZ).expand(4, 0, 4); }
@Override public double getCouplingDist(TrainCoupling coupling) { return coupling != null ? 2.75 : 0; }
@Override
public double getCurrentSpeed() {

View File

@ -8,12 +8,14 @@ import com.hbm.entity.train.TrainCargoTram;
import com.hbm.entity.train.TrainCargoTramTrailer;
import com.hbm.items.ItemEnumMulti;
import com.hbm.util.EnumUtil;
import com.hbm.util.fauxpointtwelve.BlockPos;
import net.minecraft.block.Block;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.util.Vec3;
import net.minecraft.world.World;
public class ItemTrain extends ItemEnumMulti {
@ -74,7 +76,13 @@ public class ItemTrain extends ItemEnumMulti {
if(train != null && train.getGauge() == ((IRailNTM) b).getGauge(world, x, y, z)) {
if(!world.isRemote) {
train.setPosition(x + fx, y + fy, z + fz);
BlockPos anchor = train.getCurentAnchorPos();
train.rotationYaw = entity.rotationYaw;
Vec3 corePos = train.getRelPosAlongRail(anchor, 0);
train.setPosition(corePos.xCoord, corePos.yCoord, corePos.zCoord);
Vec3 frontPos = train.getRelPosAlongRail(anchor, train.getLengthSpan());
Vec3 backPos = train.getRelPosAlongRail(anchor, -train.getLengthSpan());
train.rotationYaw = train.generateYaw(frontPos, backPos);
world.spawnEntityInWorld(train);
}