Advertisement
ItsAMysterious

EntityVehicle

Sep 6th, 2015
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.83 KB | None | 0 0
  1. package itsamysterious.mods.reallifemod.core.vehicles;
  2.  
  3. import org.lwjgl.input.Keyboard;
  4. import org.lwjgl.util.vector.Vector3f;
  5.  
  6. import io.netty.buffer.ByteBuf;
  7. import itsamysterious.mods.reallifemod.RealLifeMod;
  8. import itsamysterious.mods.reallifemod.core.packets.MountVehicleMessage;
  9. import itsamysterious.mods.reallifemod.core.sounds.SoundPlayer;
  10. import net.minecraft.block.Block;
  11. import net.minecraft.client.Minecraft;
  12. import net.minecraft.entity.Entity;
  13. import net.minecraft.entity.IEntityMultiPart;
  14. import net.minecraft.entity.boss.EntityDragonPart;
  15. import net.minecraft.entity.player.EntityPlayer;
  16. import net.minecraft.nbt.NBTTagCompound;
  17. import net.minecraft.util.AxisAlignedBB;
  18. import net.minecraft.util.BlockPos;
  19. import net.minecraft.util.DamageSource;
  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.     private VehicleFile file;
  28.     public double backWheelRotation;
  29.     public double wheelRotL;
  30.     public double steeringangle;
  31.     private boolean isEmpty;
  32.     private boolean canDoStuff;
  33.  
  34.     private EntitySeat[] seats;
  35.     private double vehicleX;
  36.     private double vehicleY;
  37.     private double vehicleZ;
  38.     private float vehicleYaw;
  39.     private float vehiclePitch;
  40.     private double velocityY;
  41.     private double velocityX;
  42.     private double velocityZ;
  43.     public double speed;
  44.     public double fuellevel;
  45.     private SoundPlayer player;
  46.     //Constants
  47.  
  48.    
  49.    
  50.     private float P;//Power Lever
  51.     private float Xb; // Brake pedal deflection
  52.     private float Xn; // Brake pedal deflection
  53.    
  54.     private double F;//Propulsive fort
  55.     private double V;//Propulsive fort
  56.  
  57.     private VehicleRadio radio;
  58.    
  59.     public EntityVehicle(World world) {
  60.         super(world);
  61.         this.setSize(1, 2);
  62.         this.canDoStuff = false;
  63.         this.isEmpty = true;
  64.         this.preventEntitySpawning = true;
  65.     }
  66.  
  67.     public EntityVehicle(World world, VehicleFile file, double x, double y, double z) {
  68.         this(world);
  69.         this.setFile(file);
  70.         this.seats = new EntitySeat[file.numDrivers];
  71.         //this.createSeats(world);
  72.         this.setPosition(x, y, z);
  73.         this.canDoStuff = true;
  74.     }
  75.  
  76.     private void createSeats(World w) {
  77.         for (int i = 0; i < this.seats.length; i++) {
  78.             System.out.println(seats.length);
  79.             this.seats[i] = new EntitySeat(w);
  80.             w.spawnEntityInWorld(seats[i]);
  81.         }
  82.         this.canDoStuff = true;
  83.     }
  84.  
  85.     @Override
  86.     @SideOnly(Side.CLIENT)
  87.     public void setVelocity(double p_70016_1_, double p_70016_3_, double p_70016_5_) {
  88.         this.velocityX = (float) (this.motionX = p_70016_1_);
  89.         this.velocityY = (float) (this.motionY = p_70016_3_);
  90.         this.velocityZ = (float) (this.motionZ = p_70016_5_);
  91.     }
  92.  
  93.     @Override
  94.     public void entityInit() {
  95.         this.dataWatcher.addObject(17, new Integer(0));
  96.         this.dataWatcher.addObject(18, new Integer(1));
  97.         this.dataWatcher.addObject(19, new Float(0.0F));
  98.     }
  99.  
  100.     @Override
  101.     public void onUpdate() {
  102.         super.onUpdate();
  103.         this.prevPosX = this.posX;
  104.         this.prevPosY = this.posY;
  105.         this.prevPosZ = this.posZ;
  106.  
  107.         if (this.canDoStuff) {
  108.             for (int i = 0; i < seats.length; i++) {
  109.                 Vector3f f = this.file.ridersPositions.get(i);
  110.                 this.seats[i].setPosition(this.posX + f.x, this.posY + f.y + 0.5, this.posZ + f.z);
  111.             }
  112.         }
  113.  
  114.         wheelRotL+=speed;
  115.         backWheelRotation+=speed;
  116.         if(worldObj.isRemote)
  117.         this.move();
  118.         this.setPosition(vehicleX, vehicleY, vehicleZ);
  119.        
  120.     }
  121.  
  122.     public void move() {
  123.         if(this.riddenByEntity!=null){
  124.         if (Keyboard.isKeyDown(Keyboard.KEY_A)) {
  125.             if(steeringangle < 30){
  126.                 steeringangle += 3.5;
  127.             }
  128.         } else if (Keyboard.isKeyDown(Keyboard.KEY_D) ) {
  129.             if(steeringangle > -30){
  130.                 steeringangle -= 3.5;
  131.             }
  132.         } else {
  133.             if (steeringangle > 0) {
  134.                 steeringangle -= 2.5;
  135.                 if (steeringangle > 0) {
  136.                     steeringangle -= 2.5;
  137.                 }
  138.             }
  139.             if (steeringangle < 0) {
  140.                 steeringangle += 2.5;
  141.                 if (steeringangle < 0) {
  142.                     steeringangle += 2.5;
  143.                 }
  144.             }
  145.         }
  146.         boolean pressedW;
  147.         if(Keyboard.isKeyDown(Keyboard.KEY_W)){
  148.             riddenByEntity.playSound("reallifemod:lambo_throttles", 1.0f, 1.0f);
  149.             if(speed<file.maxSpeed){
  150.                 speed+=file.acceleration*20;
  151.                
  152.             }
  153.             if(P<30){
  154.                 P+=2.5;
  155.             }
  156.             pressedW=true;
  157.         }else
  158.         {
  159.        
  160.             if(P>5){
  161.                 P-=0.5;
  162.             }else
  163.                 P=0;
  164.  
  165.         }
  166.        
  167.         if(ticksExisted%3==0){
  168.             riddenByEntity.playSound("reallifemod:lambo_runs", 1.0f, 1.0f+(float)P*0.1f);
  169.         }
  170.        
  171.         if(Keyboard.isKeyDown(Keyboard.KEY_S)){
  172.             if(speed>-file.maxReverseSpeed){
  173.                 speed-=file.acceleration*1000/60/60*20;
  174.             }
  175.         }
  176.        
  177.         if(Keyboard.isKeyDown(Keyboard.KEY_SPACE)){
  178.             if(speed<-5){
  179.                 speed+=5;
  180.             }else
  181.                 if(speed<0){
  182.                     speed=0;
  183.                 }
  184.            
  185.             if(speed>5){
  186.                 speed-=5;
  187.             }else{
  188.                 speed=0;
  189.             }
  190.         }
  191.        
  192.         double k=0;
  193.         double R=0;
  194.         if(steeringangle!=0){
  195.             R=file.dimensions.z/steeringangle;
  196.             k=1/R;
  197.         }
  198.         //double m = file.mass;
  199.         /*V=speed*1000/m;
  200.         double u= motionX;
  201.         double v= motionZ;//
  202.         V=Math.pow(((u*u)+(v*v)),0.5)   ;
  203.         double ax=u-r*v;
  204.         double ay=v+r*u;
  205.         double Pw;
  206.         Vector2d F;
  207.         double Fa;
  208.         double W = m*g;
  209.         */
  210.        
  211.         double r=((speed*1000)/60/60/20)*k;
  212.         this.rotationYaw-=r;
  213.         this.velocityX=-(speed*1000/60/60/20)*Math.sin(Math.toRadians(rotationYaw));
  214.         this.velocityZ=(speed*1000/60/60/20)*Math.cos(Math.toRadians(rotationYaw));
  215.         if(worldObj.isRemote){
  216.             this.vehicleX+=velocityX;
  217.             this.vehicleY+=velocityY;
  218.             this.vehicleZ+=velocityZ;
  219.             speed*=0.988888881;
  220.         }else{}
  221.        
  222.         if(worldObj.isRemote){
  223.         if(Keyboard.isKeyDown(Keyboard.KEY_P)){
  224.             this.radio.play(Minecraft.getMinecraft().mcDataDir.getAbsolutePath()+"/RLM/music/mario_08.wav");
  225.         }}}
  226.     }
  227.     @Override
  228.     public void readEntityFromNBT(NBTTagCompound tagCompund) {
  229.  
  230.     }
  231.  
  232.     @Override
  233.     public void writeEntityToNBT(NBTTagCompound tagCompound) {
  234.  
  235.     }
  236.  
  237.     @Override
  238.     public boolean interactFirst(EntityPlayer player) {
  239.         if (this.riddenByEntity == null&&!player.isDead) {
  240.             player.mountEntity(this);
  241.             return true;
  242.         }
  243.         else
  244.         return false;
  245.     }
  246.  
  247.     @Override
  248.     public void updateRiderPosition() {
  249.         if (riddenByEntity != null) {
  250.             Vector3f pos=file.ridersPositions.get(0);
  251.            
  252.             double k=0;
  253.             double R=0;
  254.             if(steeringangle!=0){
  255.                 R=file.dimensions.z/steeringangle;
  256.                 k=1/R;
  257.             }
  258.             double r=((speed*1000)/60/60/80)*k;
  259.             riddenByEntity.rotationYaw-=r;
  260.         }
  261.         riddenByEntity.setAir(0);
  262.         riddenByEntity.setPosition(posX, posY-0.6, posZ);
  263.     };
  264.  
  265.  
  266.     public VehicleFile getFile() {
  267.         return this.file;
  268.     }
  269.  
  270.     public void setFile(VehicleFile file) {
  271.         this.file = file;
  272.     }
  273.  
  274.     public void setDamageTaken(float p_70266_1_) {
  275.         this.dataWatcher.updateObject(19, Float.valueOf(p_70266_1_));
  276.     }
  277.  
  278.     public float getDamageTaken() {
  279.         return this.dataWatcher.getWatchableObjectFloat(19);
  280.     }
  281.  
  282.     public void setTimeSinceHit(int p_70265_1_) {
  283.         this.dataWatcher.updateObject(17, Integer.valueOf(p_70265_1_));
  284.     }
  285.  
  286.     public int getTimeSinceHit() {
  287.         return this.dataWatcher.getWatchableObjectInt(17);
  288.     }
  289.  
  290.     public void setForwardDirection(int p_70269_1_) {
  291.         this.dataWatcher.updateObject(18, Integer.valueOf(p_70269_1_));
  292.     }
  293.  
  294.     public int getForwardDirection() {
  295.         return this.dataWatcher.getWatchableObjectInt(18);
  296.     }
  297.  
  298.     @Override
  299.     protected boolean canTriggerWalking() {
  300.         return false;
  301.     }
  302.  
  303.     @Override
  304.     public AxisAlignedBB getCollisionBox(Entity entityIn) {
  305.         return entityIn.getEntityBoundingBox();
  306.     }
  307.  
  308.     @Override
  309.     public AxisAlignedBB getBoundingBox() {
  310.         return this.getEntityBoundingBox();
  311.     }
  312.  
  313.     public boolean canRiderInteract() {
  314.         return true;
  315.     }
  316.  
  317.     public boolean shouldDismountInWater(Entity rider) {
  318.         return true;
  319.     }
  320.  
  321.     @Override
  322.     public void writeSpawnData(ByteBuf buffer) {
  323.        
  324.     }
  325.  
  326.     @Override
  327.     public void readSpawnData(ByteBuf additionalData) {
  328.        
  329.     }
  330.  
  331. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement