train collision dummies

This commit is contained in:
Boblet 2023-05-12 14:35:06 +02:00
parent c7f28e8853
commit b8f32a511b
7 changed files with 156 additions and 13 deletions

View File

@ -110,7 +110,7 @@ public class EntityMappings {
addEntity(EntityTSmokeFX.class, "entity_t_smoke_fx", 1000);
addEntity(EntityNukeExplosionMK3.class, "entity_nuke_mk3", 1000);
addEntity(EntityVortex.class, "entity_vortex", 250);
addEntity(EntityMeteor.class, "entity_meteor", 1000);
addEntity(EntityMeteor.class, "entity_meteor", 250);
addEntity(EntityLaser.class, "entity_laser", 1000);
addEntity(EntityBoxcar.class, "entity_boxcar", 1000);
addEntity(EntityMissileTaint.class, "entity_missile_taint", 1000);

View File

@ -74,7 +74,7 @@ public class EntityMeteor extends Entity {
@Override
@SideOnly(Side.CLIENT)
public boolean isInRangeToRenderDist(double distance) {
return distance < 500000;
return true;
}
@Override

View File

@ -9,13 +9,15 @@ 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.entity.player.EntityPlayer;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.DamageSource;
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;
@ -27,6 +29,9 @@ public abstract class EntityRailCarBase extends Entity {
@SideOnly(Side.CLIENT) private double velocityX;
@SideOnly(Side.CLIENT) private double velocityY;
@SideOnly(Side.CLIENT) private double velocityZ;
public boolean initDummies = false;
public BoundingBoxDummyEntity[] dummies = new BoundingBoxDummyEntity[0];
public EntityRailCarBase(World world) {
super(world);
@ -68,6 +73,22 @@ public abstract class EntityRailCarBase extends Entity {
}
} else {
DummyConfig[] definitions = this.getDummies();
if(!this.initDummies) {
this.dummies = new BoundingBoxDummyEntity[definitions.length];
for(int i = 0; i < definitions.length; i++) {
DummyConfig def = definitions[i];
BoundingBoxDummyEntity dummy = new BoundingBoxDummyEntity(worldObj, this, def.width, def.height);
dummy.setPosition(posX, posY, posZ);
worldObj.spawnEntityInWorld(dummy);
this.dummies[i] = dummy;
}
this.initDummies = true;
}
BlockPos anchor = this.getCurentAnchorPos();
Vec3 corePos = getRelPosAlongRail(anchor, this.getCurrentSpeed());
@ -81,6 +102,7 @@ public abstract class EntityRailCarBase extends Entity {
if(frontPos == null || backPos == null) {
this.derail();
return;
} else {
this.prevRotationYaw = this.rotationYaw;
this.rotationYaw = this.movementYaw = generateYaw(frontPos, backPos);
@ -88,6 +110,18 @@ public abstract class EntityRailCarBase extends Entity {
this.velocityChanged = true;
}
}
for(int i = 0; i < definitions.length; i++) {
DummyConfig def = definitions[i];
BoundingBoxDummyEntity dummy = dummies[i];
Vec3 rot = Vec3.createVectorHelper(def.offset.xCoord, def.offset.yCoord, def.offset.zCoord);
rot.rotateAroundY((float) (-this.rotationYaw * Math.PI / 180));
double x = posX + rot.xCoord;
double y = posY + rot.yCoord;
double z = posZ + rot.zCoord;
dummy.setSize(def.width, def.height); // TEMP
dummy.setPosition(x, y, z);
}
}
}
@ -199,11 +233,85 @@ public abstract class EntityRailCarBase extends Entity {
/** Invisible entities that make up the dynamic bounding structure of the train, moving as the train rotates. */
public static class BoundingBoxDummyEntity extends Entity {
public BoundingBoxDummyEntity(World world) { this(world, 1F, 1F); }
public BoundingBoxDummyEntity(World world, float width, float height) { super(world); this.setSize(width, height);}
@Override protected void entityInit() { }
private int turnProgress;
private double trainX;
private double trainY;
private double trainZ;
public EntityRailCarBase train;
public BoundingBoxDummyEntity(World world) { this(world, null, 1F, 1F); }
public BoundingBoxDummyEntity(World world, EntityRailCarBase train, float width, float height) {
super(world);
this.setSize(width, height);
this.train = train;
if(train != null) this.dataWatcher.updateObject(3, train.getEntityId());
}
@Override protected void setSize(float width, float height) {
super.setSize(width, height);
this.dataWatcher.updateObject(4, width);
this.dataWatcher.updateObject(5, height);
}
@Override protected void entityInit() {
this.dataWatcher.addObject(3, new Integer(1));
this.dataWatcher.addObject(4, new Float(1F));
this.dataWatcher.addObject(5, new Float(1F));
}
@Override protected void writeEntityToNBT(NBTTagCompound nbt) { }
@Override public boolean writeToNBTOptional(NBTTagCompound nbt) { return false; }
@Override public void readEntityFromNBT(NBTTagCompound nbt) { this.setDead(); }
@Override public boolean canBePushed() { return true; }
@Override public boolean canBeCollidedWith() { return !this.isDead; }
@Override public boolean attackEntityFrom(DamageSource source, float amount) { if(train != null) return train.attackEntityFrom(source, amount); return super.attackEntityFrom(source, amount); }
@Override public boolean interactFirst(EntityPlayer player) { if(train != null) return train.interactFirst(player); return super.interactFirst(player); }
@Override public void onUpdate() {
if(!worldObj.isRemote) {
if(this.train.isDead) {
this.setDead();
}
} else {
if(this.turnProgress > 0) {
this.prevRotationYaw = this.rotationYaw;
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;
--this.turnProgress;
this.setPosition(x, y, z);
} else {
this.setPosition(this.posX, this.posY, this.posZ);
}
this.setSize(this.dataWatcher.getWatchableObjectFloat(4), this.dataWatcher.getWatchableObjectFloat(5));
}
}
@Override @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.turnProgress = turnProg + 2;
}
}
public DummyConfig[] getDummies() {
return new DummyConfig[0];
}
public static class DummyConfig {
public Vec3 offset;
public float width;
public float height;
public DummyConfig(float width, float height, Vec3 offset) {
this.width = width;
this.height = height;
this.offset = offset;
}
}
}

View File

@ -1,8 +1,11 @@
package com.hbm.entity.train;
import com.hbm.util.BobMathUtil;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.MathHelper;
import net.minecraft.util.Vec3;
import net.minecraft.world.World;
@ -32,9 +35,13 @@ public abstract class EntityRailCarRidable extends EntityRailCarCargo {
seat.rotateAroundY((float) (-this.rotationYaw * Math.PI / 180));
double x = posX + seat.xCoord;
double y = posY + seat.yCoord;
double z = posZ + seat.zCoord;
double dist = Vec3.createVectorHelper(player.posX - x, player.posY - y, player.posZ - z).lengthVector();
double deltaX = player.posX - x;
double deltaZ = player.posZ - z;
double radians = -Math.atan2(deltaX, deltaZ);
double degrees = MathHelper.wrapAngleTo180_double(radians * 180D / Math.PI - 90);
double dist = Math.abs(BobMathUtil.angularDifference(degrees, player.rotationYaw));
if(dist < nearestDist) {
nearestDist = dist;
@ -46,9 +53,13 @@ public abstract class EntityRailCarRidable extends EntityRailCarCargo {
Vec3 seat = getRiderSeatPosition();
seat.rotateAroundY((float) (-this.rotationYaw * Math.PI / 180));
double x = posX + seat.xCoord;
double y = posY + seat.yCoord;
double z = posZ + seat.zCoord;
double dist = Vec3.createVectorHelper(player.posX - x, player.posY - y, player.posZ - z).lengthVector();
double deltaX = player.posX - x;
double deltaZ = player.posZ - z;
double radians = -Math.atan2(deltaX, deltaZ);
double degrees = MathHelper.wrapAngleTo180_double(radians * 180D / Math.PI - 90);
double dist = Math.abs(BobMathUtil.angularDifference(degrees, player.rotationYaw));
if(dist < nearestDist) {
nearestDist = dist;
@ -56,7 +67,7 @@ public abstract class EntityRailCarRidable extends EntityRailCarCargo {
}
}
if(nearestDist > 20) return true;
if(nearestDist > 180) return true;
if(nearestSeat == -1) {
player.mountEntity(this);

View File

@ -30,7 +30,7 @@ public class TrainCargoTram extends EntityRailCarRidable implements IGUIProvider
public TrainCargoTram(World world) {
super(world);
this.setSize(2F, 1F);
this.setSize(1F, 1F);
}
public double speed = 0;
@ -78,7 +78,16 @@ public class TrainCargoTram extends EntityRailCarRidable implements IGUIProvider
}
@Override
public boolean attackEntityFrom(DamageSource p_70097_1_, float p_70097_2_) {
public DummyConfig[] getDummies() {
return new DummyConfig[] {
new DummyConfig(2F, 1F, Vec3.createVectorHelper(0, 0, 1.5)),
new DummyConfig(2F, 1F, Vec3.createVectorHelper(0, 0, 0)),
new DummyConfig(2F, 1F, Vec3.createVectorHelper(0, 0, -1.5))
};
}
@Override
public boolean attackEntityFrom(DamageSource source, float amount) {
if(!this.worldObj.isRemote && !this.isDead) {
this.setDead();
}

View File

@ -61,6 +61,7 @@ import com.hbm.entity.mob.siege.*;
import com.hbm.entity.particle.*;
import com.hbm.entity.projectile.*;
import com.hbm.entity.train.*;
import com.hbm.entity.train.EntityRailCarBase.BoundingBoxDummyEntity;
import com.hbm.entity.train.EntityRailCarRidable.SeatDummyEntity;
import com.hbm.handler.CasingEjector;
import com.hbm.handler.HbmKeybinds;
@ -685,6 +686,7 @@ public class ClientProxy extends ServerProxy {
RenderingRegistry.registerEntityRenderingHandler(EntityMagnusCartus.class, new RenderMagnusCartus());
//trains
RenderingRegistry.registerEntityRenderingHandler(SeatDummyEntity.class, new RenderEmpty());
RenderingRegistry.registerEntityRenderingHandler(BoundingBoxDummyEntity.class, new RenderEmpty());
RenderingRegistry.registerEntityRenderingHandler(TrainCargoTram.class, new RenderTrainCargoTram());
//items
RenderingRegistry.registerEntityRenderingHandler(EntityMovingItem.class, new RenderMovingItem());

View File

@ -1,5 +1,6 @@
package com.hbm.util;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.NumberFormat;
@ -9,6 +10,7 @@ import java.util.List;
import javax.annotation.Nonnegative;
import cpw.mods.fml.relauncher.ReflectionHelper;
import net.minecraft.util.MathHelper;
import net.minecraft.util.Vec3;
import net.minecraftforge.common.util.ForgeDirection;
@ -193,4 +195,15 @@ public class BobMathUtil {
public static double squirt(double x) {
return Math.sqrt(x + 1D / ((x + 2D) * (x + 2D))) - 1D / (x + 2D);
}
/** A convenient way to re-define the value of pi, should the laws of nature change. */
public static void setPi(double pi) {
Field field = ReflectionHelper.findField(Math.class, "PI");
try { field.setDouble(null, pi); } catch(Exception e) { }
}
public static double angularDifference(double alpha, double beta) {
double delta = (beta - alpha + 180) % 360 - 180;
return delta < -180 ? delta + 360 : delta;
}
}