Guest User

Untitled

a guest
May 23rd, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.40 KB | None | 0 0
  1. package at.tugraz.icg.svu.micromachines.client.gamelogic.cars;
  2.  
  3. import com.jme.math.Vector2f;
  4. import java.lang.Math;
  5. import java.util.LinkedList;
  6. import at.tugraz.icg.svu.micromachines.client.gamelogic.events.Event;
  7. import at.tugraz.icg.svu.micromachines.client.gamelogic.events.KeyEvent;
  8. import at.tugraz.icg.svu.micromachines.client.gamelogic.events.SnapshotEvent;
  9. import at.tugraz.icg.svu.micromachines.client.models.CarModel;
  10. import at.tugraz.icg.svu.micromachines.client.racetracks.RaceTrack;
  11.  
  12. /**
  13.  * Class that simulates the real state and handles the convergence mechanism
  14.  *
  15.  * @author Martin Six
  16.  */
  17. public class Ghost extends Car {
  18.  
  19.     /**
  20.      * Distance used for warping the ghosts.
  21.      */
  22.     private static final int WARPING_BORDER = 5;
  23.  
  24.     /**
  25.      * Time used for interpolation the position, rotation and velocity of the
  26.      * cars
  27.      */
  28.     private static final int INTERPOLATION_DURATION = 1000;
  29.  
  30.     private Car car;
  31.    
  32.     private float rotDif;
  33.     private float velDif;
  34.     private Vector2f posDif;
  35.    
  36.  
  37.     public Ghost(Car car) {
  38.         super(car.name, car.gameLogic, car.getWeight(), car.acceleration,
  39.                 car.brakeSpeed, car.turnSpeed, car.maxForwardsVelocity,
  40.                 car.maxBackwardsVelocity);
  41.         this.car = car;
  42.         this.posDif = new Vector2f(0.0f,0.0f);
  43.         this.rotDif = 0.0f;
  44.         this.velDif = 0.0f;
  45.     }
  46.    
  47.     private long resetState(){
  48.         long time = 0;
  49.         if(pel.size() > 0){
  50.             SnapshotEvent snap = (SnapshotEvent)pel.get(pel.size()-1);
  51.             this.position = snap.position;
  52.             this.velocity = snap.velocity;
  53.             this.rotation = snap.rotation;
  54.             time          = snap.getTimeStamp();
  55.         }
  56.         return time;
  57.     }
  58.  
  59.     private void interpolate(float ipst, float dt, boolean noEventFlag){
  60.         System.out.println("car.Position1   : " + car.position.toString());
  61.         System.out.println("ghost.Position1 : " + this.position.toString());
  62.        
  63.         if(this.position.equals(car.position)){
  64.             car.update(dt);
  65.             return;
  66.         }
  67.        
  68.         float time = (((float)getCurrentRelativeTime() - ipst)/ (float)INTERPOLATION_DURATION);
  69.        
  70.         if(!noEventFlag){
  71.             posDif = new Vector2f((this.position.x - car.position.x),
  72.                     (this.position.y - car.position.y));
  73.            
  74.             velDif = this.velocity - car.velocity;
  75.             rotDif = this.rotation - car.rotation;
  76.         }
  77.  
  78.         float vecdistance = (float)Math.sqrt((double)Math.pow((double)posDif.x, 2) + (double)Math.pow((double)posDif.y, 2));
  79.         System.out.println("VecDistance = " + vecdistance);
  80.        
  81.         if(vecdistance > WARPING_BORDER){
  82.             car.update(dt);
  83.             return;
  84.         }
  85.        
  86.         Vector2f tmp = new Vector2f((posDif.getX() * time), (posDif.getY()*time));
  87.        
  88.         car.position.setX((car.position.getX() + tmp.getX()));
  89.         car.position.setY((car.position.getY() + tmp.getY()));
  90.         car.velocity += velDif * time;
  91.         car.rotation += rotDif * time;
  92.  
  93.         car.update(dt);
  94.     }
  95. /*****************************************************************************/
  96.     @Override
  97.     public void update(float dt) {
  98.        
  99.         /*###################################################################*/
  100.         if (!getMBSM_FLAG() && !getMBKM_FLAG()){
  101.             interpolate(ipst, dt, true);
  102.             super.update(dt);
  103.             return;
  104.         }
  105.  
  106.         /*###################################################################*/
  107.         if (getMBKM_FLAG()) {
  108.             long eventTime  = this.getEventTime();
  109.             long localTime  = getCurrentRelativeTime();
  110.            
  111.             /*
  112.              * Set car back to state before actual event
  113.              */
  114.             long snapEventTime = resetState();
  115.            
  116.             /*
  117.              * Spool forward to eventTime
  118.              */
  119.             while(snapEventTime < eventTime){
  120.                 super.update(dt);
  121.                 snapEventTime += (dt*1000);
  122.             }
  123.            
  124.             /*
  125.              * Do a snapshot of current state
  126.              */
  127.             SnapshotEvent newsnap = new SnapshotEvent(this.rotation,
  128.                     this.velocity, this.position, eventTime);
  129.             pel.add(newsnap);
  130.    
  131.             /*
  132.              * Set Keys from new Event
  133.              */
  134.             this.setKeyFlag(this.settedKeyFlagID, this.settedKeyFlag);
  135.            
  136.             /*
  137.              * Spool forward to now
  138.              */
  139.             while(eventTime < localTime){
  140.                 super.update(dt);
  141.                 eventTime += (dt*1000);
  142.             }
  143.            
  144.             ipst = (float)getCurrentRelativeTime();
  145.             System.out.println("ipst1 : " + ipst);
  146.            
  147.             interpolate(ipst, dt, false);
  148.             this.setMBKM_FLAG(false);
  149.             return;
  150.         }
  151.  
  152.         /*###################################################################*/
  153.         if (getMBSM_FLAG()) {
  154.             long eventTime  = this.getEventTime();
  155.             long localTime  = getCurrentRelativeTime();
  156.            
  157.             /*
  158.              * Spool forward to now
  159.              */
  160.             while(eventTime < localTime){
  161.                 super.update(dt);
  162.                 eventTime += (dt*1000);
  163.             }
  164.             ipst = (float)getCurrentRelativeTime();
  165.            
  166.             interpolate(ipst, dt, false);
  167.             this.setMBSM_FLAG(false);
  168.             return;
  169.         }
  170.     }
  171. /*****************************************************************************/
  172.    
  173.     @Override
  174.     public CarModel initModel() {
  175.         return car.initModel();
  176.     }
  177.  
  178.     @Override
  179.     public CarModel getModel() {
  180.         return car.getModel();
  181.     }
  182.  
  183.     @Override
  184.     public void updateModel() {
  185.         car.updateModel();
  186.     }
  187.  
  188.     @Override
  189.     public void setPosition(Vector2f position) {
  190.         car.setPosition(position);
  191.         super.setPosition(position);
  192.     }
  193.  
  194.     @Override
  195.     public void setRotation(float rotation) {
  196.         car.setRotation(rotation);
  197.         super.setRotation(rotation);
  198.     }
  199.  
  200.     @Override
  201.     public void setVelocity(float velocity) {
  202.         car.setVelocity(velocity);
  203.         super.setVelocity(velocity);
  204.     }
  205.  
  206.     @Override
  207.     public void resetToStartFormation(int index, RaceTrack raceTrack) {
  208.         super.resetToStartFormation(index, raceTrack);
  209.         car.resetToStartFormation(index, raceTrack);
  210.     }
  211.  
  212. }
Add Comment
Please, Sign In to add comment