ItsAMysterious

EntityVehicle

Sep 27th, 2015
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 14.46 KB | None | 0 0
  1. package itsamysterious.mods.reallifemod.core.vehicles;
  2.  
  3. import java.util.List;
  4.  
  5. import org.lwjgl.input.Keyboard;
  6.  
  7. import io.netty.buffer.ByteBuf;
  8. import itsamysterious.mods.reallifemod.RealLifeMod;
  9. import itsamysterious.mods.reallifemod.core.packets.UpdateVehiclePacket;
  10. import itsamysterious.mods.reallifemod.core.sounds.SoundPlayer;
  11. import net.minecraft.entity.Entity;
  12. import net.minecraft.entity.player.EntityPlayer;
  13. import net.minecraft.init.Blocks;
  14. import net.minecraft.init.Items;
  15. import net.minecraft.nbt.NBTTagCompound;
  16. import net.minecraft.util.AxisAlignedBB;
  17. import net.minecraft.util.DamageSource;
  18. import net.minecraft.util.EntityDamageSourceIndirect;
  19. import net.minecraft.util.Vec3i;
  20. import net.minecraft.world.World;
  21. import net.minecraftforge.fml.common.registry.IEntityAdditionalSpawnData;
  22. import net.minecraftforge.fml.relauncher.Side;
  23. import net.minecraftforge.fml.relauncher.SideOnly;
  24.  
  25. public class EntityVehicle extends Entity implements IEntityAdditionalSpawnData {
  26.     private static final double g = 9.81;
  27.     protected VehicleFile file;
  28.  
  29.     public double backWheelRotation;
  30.     public double wheelRotL;
  31.     public double steeringangle;
  32.     private boolean isEmpty;
  33.     private boolean canDoStuff;
  34.  
  35.     private EntitySeat[] seats;
  36.     private double vehicleX;
  37.     private double vehicleY;
  38.     private double vehicleZ;
  39.     private double vehicleYaw;
  40.     private double vehiclePitch;
  41.  
  42.     @SideOnly(Side.CLIENT)
  43.     private double velocityY;
  44.     @SideOnly(Side.CLIENT)
  45.     private double velocityX;
  46.     @SideOnly(Side.CLIENT)
  47.     private double velocityZ;
  48.  
  49.     EntityPlayer mountedPlayer;
  50.     public double speed;
  51.     public double fuellevel;
  52.     private SoundPlayer player;
  53.     // Constants
  54.  
  55.     private float P;// Power Lever
  56.     private float Xb; // Brake pedal deflection
  57.     private float Xn; // Brake pedal deflection
  58.  
  59.     private double F;// Propulsive fort
  60.     private double V;// Propulsive fort
  61.  
  62.     private VehicleRadio radio;
  63.     private boolean updateMounted;
  64.  
  65.     public EntityVehicle(World world) {
  66.         super(world);
  67.         this.isEmpty = true;
  68.         this.preventEntitySpawning = true;
  69.         this.ignoreFrustumCheck = true;
  70.         this.setSize(0.9999F, 0.9f);
  71.  
  72.     }
  73.  
  74.     protected boolean canTriggerWalking() {
  75.         return false;
  76.     }
  77.  
  78.     public void entityInit() {
  79.         this.dataWatcher.addObject(17, new Integer(0));
  80.         this.dataWatcher.addObject(18, new Integer(1));
  81.         this.dataWatcher.addObject(19, new Float(0.0F));
  82.     }
  83.  
  84.     public AxisAlignedBB getCollisionBox(Entity entityIn) {
  85.         return entityIn.getEntityBoundingBox();
  86.     }
  87.  
  88.     public AxisAlignedBB getBoundingBox() {
  89.         return this.getEntityBoundingBox();
  90.     }
  91.  
  92.     public boolean canBePushed() {
  93.         return false;
  94.     }
  95.  
  96.     public EntityVehicle(World world, double x, double y, double z) {
  97.         this(world);
  98.         this.setPosition(x, y, z);
  99.         // this.seats = new EntitySeat[file.numDrivers];
  100.         // this.createSeats(world);
  101.         // this.canDoStuff = true;
  102.         this.motionX = 0.0D;
  103.         this.motionY = 0.0D;
  104.         this.motionZ = 0.0D;
  105.         this.prevPosX = x;
  106.         this.prevPosY = y;
  107.         this.prevPosZ = z;
  108.     }
  109.  
  110.     @Override
  111.     public double getMountedYOffset() {
  112.         return (double) this.height * 0.0D - 0.30000001192092896D;
  113.     }
  114.  
  115.     public boolean attackEntityFrom(DamageSource source, float amount) {
  116.         if (this.isEntityInvulnerable(source)) {
  117.             return false;
  118.         } else if (!this.worldObj.isRemote && !this.isDead) {
  119.             if (this.riddenByEntity != null && this.riddenByEntity == source.getEntity()
  120.                     && source instanceof EntityDamageSourceIndirect) {
  121.                 return false;
  122.             } else {
  123.                 this.setForwardDirection(-this.getForwardDirection());
  124.                 this.setTimeSinceHit(10);
  125.                 this.setDamageTaken(this.getDamageTaken() + amount * 10.0F);
  126.                 this.setBeenAttacked();
  127.                 boolean flag = source.getEntity() instanceof EntityPlayer
  128.                         && ((EntityPlayer) source.getEntity()).capabilities.isCreativeMode;
  129.  
  130.                 if (flag || this.getDamageTaken() > 40.0F) {
  131.                     if (this.riddenByEntity != null) {
  132.                         this.riddenByEntity.mountEntity(this);
  133.                     }
  134.  
  135.                     if (!flag) {
  136.                         this.dropItemWithOffset(Items.boat, 1, 0.0F);
  137.                     }
  138.  
  139.                     this.setDead();
  140.                 }
  141.  
  142.                 return true;
  143.             }
  144.         } else {
  145.             return true;
  146.         }
  147.     }
  148.  
  149.     @SideOnly(Side.CLIENT)
  150.     public void performHurtAnimation() {
  151.         this.setForwardDirection(-this.getForwardDirection());
  152.         this.setTimeSinceHit(10);
  153.         this.setDamageTaken(this.getDamageTaken() * 11.0F);
  154.     }
  155.  
  156.     private void createSeats(World w) {
  157.         for (int i = 0; i < this.seats.length; i++) {
  158.             System.out.println(seats.length);
  159.             this.seats[i] = new EntitySeat(w);
  160.             w.spawnEntityInWorld(seats[i]);
  161.         }
  162.         this.canDoStuff = true;
  163.     }
  164.  
  165.     @SideOnly(Side.CLIENT)
  166.     public void func_180426_a(double p_180426_1_, double p_180426_3_, double p_180426_5_, float p_180426_7_,
  167.             float p_180426_8_, int p_180426_9_, boolean p_180426_10_) {
  168.         if (p_180426_10_ && this.riddenByEntity != null) {
  169.             this.prevPosX = this.posX = p_180426_1_;
  170.             this.prevPosY = this.posY = p_180426_3_;
  171.             this.prevPosZ = this.posZ = p_180426_5_;
  172.             this.rotationYaw = p_180426_7_;
  173.             this.rotationPitch = p_180426_8_;
  174.             this.setPosition(p_180426_1_, p_180426_3_, p_180426_5_);
  175.             this.motionX = this.velocityX = 0.0D;
  176.             this.motionY = this.velocityY = 0.0D;
  177.             this.motionZ = this.velocityZ = 0.0D;
  178.         } else {
  179.             {
  180.                 double d3 = p_180426_1_ - this.posX;
  181.                 double d4 = p_180426_3_ - this.posY;
  182.                 double d5 = p_180426_5_ - this.posZ;
  183.                 double d6 = d3 * d3 + d4 * d4 + d5 * d5;
  184.  
  185.                 if (d6 <= 1.0D) {
  186.                     return;
  187.                 }
  188.  
  189.             }
  190.  
  191.             this.vehicleX = p_180426_1_;
  192.             this.vehicleY = p_180426_3_;
  193.             this.vehicleZ = p_180426_5_;
  194.             this.vehicleYaw = p_180426_7_;
  195.             this.vehiclePitch = p_180426_8_;
  196.             this.motionX = this.velocityX;
  197.             this.motionY = this.velocityY;
  198.             this.motionZ = this.velocityZ;
  199.         }
  200.     }
  201.  
  202.     @SideOnly(Side.CLIENT)
  203.     public void setVelocity(double x, double y, double z) {
  204.         this.velocityX = this.motionX = x;
  205.         this.velocityY = this.motionY = y;
  206.         this.velocityZ = this.motionZ = z;
  207.     }
  208.  
  209.     public void onUpdate() {
  210.         super.onUpdate();
  211.  
  212.         if (this.getTimeSinceHit() > 0) {
  213.             this.setTimeSinceHit(this.getTimeSinceHit() - 1);
  214.         }
  215.  
  216.         if (this.getDamageTaken() > 0.0F) {
  217.             this.setDamageTaken(this.getDamageTaken() - 1.0F);
  218.         }
  219.  
  220.         if (this.posY < -64.0D) {
  221.             this.kill();
  222.         }
  223.  
  224.         if (this.file == null && this.ticksExisted > 3) {
  225.             setDead();
  226.         }
  227.  
  228.         if (worldObj.isRemote) {
  229.             if (this.file != null && riddenByEntity != null) {
  230.                 this.simulateMovement();
  231.             }
  232.  
  233.         } else {
  234.             this.prevPosX = this.posX;
  235.             this.prevPosY = this.posY;
  236.             this.prevPosZ = this.posZ;
  237.  
  238.             this.setRotation(this.rotationYaw, this.rotationPitch);
  239.  
  240.             this.posX += this.vehicleX;
  241.             this.posY += this.vehicleY;
  242.             this.posZ += this.vehicleZ;
  243.             this.setPosition(this.posX, this.posY, this.posZ);
  244.  
  245.         }
  246.  
  247.         this.motionY -= 0.03999999910593033D;
  248.         this.moveEntity(this.motionX, this.motionY, this.motionZ);
  249.         this.motionX *= 0.9800000190734863D;
  250.         this.motionY *= 0.9800000190734863D;
  251.         this.motionZ *= 0.9800000190734863D;
  252.  
  253.         if (!onGround) {
  254.             this.motionX *= 0.9D;
  255.             this.motionY *= 0.9800000190734863D;
  256.             this.motionZ *= 0.9D;
  257.         } else {
  258.             this.motionY *= -0.5D;
  259.         }
  260.  
  261.         /*
  262.          * if (this.canDoStuff) { for (int i = 0; i < seats.length; i++) {
  263.          * Vector3f f = this.file.ridersPositions.get(i);
  264.          * this.seats[i].setPosition(this.posX + f.x, this.posY + f.y + 0.5,
  265.          * this.posZ + f.z); } }
  266.          */
  267.  
  268.         if (this.file == null && this.ticksExisted > 1) {
  269.             System.out.println("Missing a file!!");
  270.             this.setDead();
  271.         }
  272.  
  273.         if (!this.worldObj.isRemote) {
  274.  
  275.             List list = this.worldObj.getEntitiesWithinAABBExcludingEntity(this,
  276.                     this.getEntityBoundingBox().expand(0.20000000298023224D, 0.0D, 0.20000000298023224D));
  277.  
  278.             if (list != null && !list.isEmpty()) {
  279.                 for (int k1 = 0; k1 < list.size(); ++k1) {
  280.                     Entity entity = (Entity) list.get(k1);
  281.  
  282.                     if (entity != this.riddenByEntity && entity.canBePushed() && entity instanceof EntityVehicle) {
  283.                         entity.applyEntityCollision(this);
  284.                     }
  285.                 }
  286.             }
  287.  
  288.             if (this.ticksExisted % 4 == 0) {
  289.                 worldObj.playSoundAtEntity(this, this.file.startsound.toString(), 1.0f, 1.0f + (float) this.P * 0.1f);
  290.             }
  291.             if (this.P > 0) {
  292.                 worldObj.playSoundAtEntity(this, file.throttlesound.toString(), 1.0f, 1.0f);
  293.  
  294.             }
  295.  
  296.             if (this.riddenByEntity != null && this.riddenByEntity.isDead) {
  297.                 this.riddenByEntity = null;
  298.             }
  299.             double x = Math.sin(Math.toRadians(rotationYaw));
  300.             double z = Math.cos(Math.toRadians(rotationYaw));
  301.             if(worldObj.getBlockState(getPosition().subtract(new Vec3i(0,1,0))).getBlock()!=Blocks.air){
  302.                 //this.posY+=0.5;
  303.             }
  304.         }
  305.  
  306.     }
  307.  
  308.     public void simulateMovement() {
  309.         if (Keyboard.isKeyDown(Keyboard.KEY_A)) {
  310.             if (this.steeringangle < 30) {
  311.                 this.steeringangle += 3;
  312.             }
  313.         } else {
  314.             if (!Keyboard.isKeyDown(Keyboard.KEY_SPACE))
  315.                 if (this.steeringangle > 0) {
  316.                     this.steeringangle -= 3;
  317.                 }
  318.         }
  319.  
  320.         if (Keyboard.isKeyDown(Keyboard.KEY_D)) {
  321.             if (this.steeringangle > -30) {
  322.                 this.steeringangle -= 3;
  323.             }
  324.         } else {
  325.             if (!Keyboard.isKeyDown(Keyboard.KEY_SPACE))
  326.                 if (this.steeringangle < 0) {
  327.                     this.steeringangle += 3;
  328.                 }
  329.         }
  330.  
  331.         boolean pressedW;
  332.         if (Keyboard.isKeyDown(Keyboard.KEY_W)) {
  333.             if (this.speed < this.file.maxSpeed) {
  334.                 this.speed += file.acceleration * 20;
  335.  
  336.             }
  337.             if (this.P < 30) {
  338.                 this.P += 2.5;
  339.             }
  340.             pressedW = true;
  341.         } else {
  342.  
  343.             if (this.P > 5) {
  344.                 this.P -= 0.5;
  345.             } else
  346.                 this.P = 0;
  347.  
  348.         }
  349.  
  350.         if (Keyboard.isKeyDown(Keyboard.KEY_S)) {
  351.             if (this.speed > -this.file.maxReverseSpeed) {
  352.                 this.speed -= this.file.acceleration * 1000 / 60 / 60 * 20;
  353.             }
  354.         }
  355.  
  356.         if (Keyboard.isKeyDown(Keyboard.KEY_SPACE)) {
  357.             if (this.speed < -5) {
  358.                 this.speed += 5;
  359.             } else if (this.speed < 0) {
  360.                 this.speed = 0;
  361.             }
  362.  
  363.             if (this.speed > 5) {
  364.                 this.speed -= 5;
  365.             } else {
  366.                 this.speed = 0;
  367.             }
  368.         }
  369.  
  370.         double k = 0;
  371.         double R = 0;
  372.         if (this.steeringangle != 0) {
  373.             R = this.file.dimensions.x / this.steeringangle;
  374.             k = (double) 1.0D / R;
  375.         }
  376.         // double m = file.mass;
  377.         /*
  378.          * V=this.speed*1000/m; double u= motionX; double v= motionZ;//
  379.          * V=Math.pow(((u*u)+(v*v)),0.5) ; double ax=u-r*v; double ay=v+r*u;
  380.          * double Pw; Vector2d F; double Fa; double W = m*g;
  381.          */
  382.         // this.this.speed -= FLuft() + FSteig();
  383.         double r = ((this.speed * 1000) / 60 / 60 / 20) * k;
  384.         float ry = (float) (this.rotationYaw - r);
  385.         this.setRotation(ry, this.rotationPitch);
  386.         this.motionX = -(this.speed * 1000 / 60 / 60 / 20) * Math.sin(Math.toRadians(this.rotationYaw));
  387.         this.motionZ = (this.speed * 1000 / 60 / 60 / 20) * Math.cos(Math.toRadians(this.rotationYaw));
  388.         this.speed *= 0.988888881;
  389.  
  390.         this.wheelRotL += this.speed;
  391.         this.backWheelRotation += this.speed;
  392.  
  393.         double dx = this.posX + this.motionX;
  394.         double dy = this.posY + this.motionY;
  395.         double dz = this.posZ + this.motionZ;
  396.         this.setPosition(dx, dy, dz);
  397.         RealLifeMod.network.sendToServer(new UpdateVehiclePacket(getEntityId(), dx, dy, dz, ry));
  398.  
  399.     }
  400.  
  401.     @Override
  402.     public void readEntityFromNBT(NBTTagCompound tagCompund) {
  403.         NBTTagCompound vehicletag = tagCompund.getCompoundTag("VehicleTag");
  404.         this.fuellevel = vehicletag.getDouble("Fuel");
  405.         if (file == null)
  406.             if (Vehicles.getFromId(vehicletag.getInteger("FileID")) != null) {
  407.                 this.file = Vehicles.getFromId(vehicletag.getInteger("FileID"));
  408.             }
  409.  
  410.     }
  411.  
  412.     @Override
  413.     public void writeEntityToNBT(NBTTagCompound tagCompund) {
  414.         if (this.file != null) {
  415.             NBTTagCompound tag = new NBTTagCompound();
  416.             tag.setDouble("Fuel", this.fuellevel);
  417.             tagCompund.setInteger("FileID", file.getID());
  418.             tagCompund.setTag("VehicleTag", tag);
  419.         }
  420.  
  421.     }
  422.  
  423.     public boolean interactFirst(EntityPlayer player) {
  424.         if (this.riddenByEntity != null && this.riddenByEntity instanceof EntityPlayer) {
  425.             return true;
  426.         } else {
  427.             this.isEmpty = false;
  428.             player.mountEntity(this);
  429.             return true;
  430.         }
  431.     }
  432.  
  433.     public void updateRiderPosition() {
  434.         if (this.riddenByEntity != null) {
  435.             double k = 0;
  436.             double R = 0;
  437.             if (this.steeringangle != 0) {
  438.                 if (file.dimensions != null) {
  439.                     R = file.dimensions.z / steeringangle;
  440.                 } else
  441.                     R = 4.74 / steeringangle;
  442.                 k = 1 / R;
  443.             }
  444.             double r = ((this.speed * 1000) / 60 / 60 / 80) * k;
  445.             this.riddenByEntity.rotationYaw -= r;
  446.             double l = 1;
  447.             double x = -Math.sin(Math.toRadians(rotationYaw - 25)) * 0.1;
  448.             double z = Math.cos(Math.toRadians(rotationYaw - 25)) * 0.1;
  449.             this.riddenByEntity.setPosition(this.posX + x, this.posY - 0.3, this.posZ + z);
  450.         }
  451.     };
  452.  
  453.     public VehicleFile getFile() {
  454.         return this.file;
  455.     }
  456.  
  457.     public void setDamageTaken(float p_70266_1_) {
  458.         this.dataWatcher.updateObject(19, Float.valueOf(p_70266_1_));
  459.     }
  460.  
  461.     public float getDamageTaken() {
  462.         return this.dataWatcher.getWatchableObjectFloat(19);
  463.     }
  464.  
  465.     public void setTimeSinceHit(int p_70265_1_) {
  466.         this.dataWatcher.updateObject(17, Integer.valueOf(p_70265_1_));
  467.     }
  468.  
  469.     public int getTimeSinceHit() {
  470.         return this.dataWatcher.getWatchableObjectInt(17);
  471.     }
  472.  
  473.     public void setForwardDirection(int p_70269_1_) {
  474.         this.dataWatcher.updateObject(18, Integer.valueOf(p_70269_1_));
  475.     }
  476.  
  477.     public int getForwardDirection() {
  478.         return this.dataWatcher.getWatchableObjectInt(18);
  479.     }
  480.  
  481.     public boolean shouldDismountInWater(Entity rider) {
  482.         return true;
  483.     }
  484.  
  485.     protected double FLuft() {
  486.         return (1.44 * (this.speed * this.speed));// (Luftdichte/2)*Strömungswiderstand*Flaeche*Geschwindigkeit²
  487.     }
  488.  
  489.     protected double FRoll() {
  490.         if (file != null) {
  491.             return (file.mass) * g * VehicleHelper.rollresistancecoeff_tarmac * Math.cos(rotationPitch);
  492.         } else {
  493.             return 0;
  494.         }
  495.  
  496.     }
  497.  
  498.     // Steigungswiderstand
  499.     protected double FSteig() {
  500.         if (file != null) {
  501.             return (file.mass) * g * Math.sin(rotationPitch);
  502.         }
  503.         return 0;
  504.     }
  505.  
  506.     protected double FB() {
  507.         if (file != null) {
  508.             return (mF() * (file.mass)) * aG();
  509.         }
  510.         return 0;
  511.     }
  512.  
  513.     protected double mF() {
  514.         return FT();
  515.     }
  516.  
  517.     protected double FT() {
  518.         if (file != null) {
  519.             return -(file.mass) * aG();
  520.         }
  521.         return 0;
  522.     }
  523.  
  524.     protected double FWGes() {
  525.         return FLuft() + FRoll();// +FSteig()+FB());
  526.     }
  527.  
  528.     protected double aG() {
  529.         return Math.sqrt((vehicleX * vehicleX) + (vehicleZ * vehicleZ));
  530.     }
  531.  
  532.     public void setFile(VehicleFile file) {
  533.         this.file = file;
  534.     }
  535.  
  536.     @Override
  537.     public void writeSpawnData(ByteBuf buffer) {
  538.         if (file != null) {
  539.             buffer.writeInt(file.getID());
  540.         }
  541.     }
  542.  
  543.     @Override
  544.     public void readSpawnData(ByteBuf additionalData) {
  545.         this.readEntityFromNBT(getEntityData());
  546.         this.file = Vehicles.getFromId(additionalData.readInt());
  547.     }
  548.  
  549. }
Advertisement
Add Comment
Please, Sign In to add comment