Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package at.tugraz.icg.svu.micromachines.client.gamelogic.cars;
- import com.jme.math.Vector2f;
- import java.lang.Math;
- import java.util.LinkedList;
- import at.tugraz.icg.svu.micromachines.client.gamelogic.events.Event;
- import at.tugraz.icg.svu.micromachines.client.gamelogic.events.KeyEvent;
- import at.tugraz.icg.svu.micromachines.client.gamelogic.events.SnapshotEvent;
- import at.tugraz.icg.svu.micromachines.client.models.CarModel;
- import at.tugraz.icg.svu.micromachines.client.racetracks.RaceTrack;
- /**
- * Class that simulates the real state and handles the convergence mechanism
- *
- * @author Martin Six
- */
- public class Ghost extends Car {
- /**
- * Distance used for warping the ghosts.
- */
- private static final int WARPING_BORDER = 5;
- /**
- * Time used for interpolation the position, rotation and velocity of the
- * cars
- */
- private static final int INTERPOLATION_DURATION = 1000;
- private Car car;
- private float rotDif;
- private float velDif;
- private Vector2f posDif;
- public Ghost(Car car) {
- super(car.name, car.gameLogic, car.getWeight(), car.acceleration,
- car.brakeSpeed, car.turnSpeed, car.maxForwardsVelocity,
- car.maxBackwardsVelocity);
- this.car = car;
- this.posDif = new Vector2f(0.0f,0.0f);
- this.rotDif = 0.0f;
- this.velDif = 0.0f;
- }
- private long resetState(){
- long time = 0;
- if(pel.size() > 0){
- SnapshotEvent snap = (SnapshotEvent)pel.get(pel.size()-1);
- this.position = snap.position;
- this.velocity = snap.velocity;
- this.rotation = snap.rotation;
- time = snap.getTimeStamp();
- }
- return time;
- }
- private void interpolate(float ipst, float dt, boolean noEventFlag){
- System.out.println("car.Position1 : " + car.position.toString());
- System.out.println("ghost.Position1 : " + this.position.toString());
- if(this.position.equals(car.position)){
- car.update(dt);
- return;
- }
- float time = (((float)getCurrentRelativeTime() - ipst)/ (float)INTERPOLATION_DURATION);
- if(!noEventFlag){
- posDif = new Vector2f((this.position.x - car.position.x),
- (this.position.y - car.position.y));
- velDif = this.velocity - car.velocity;
- rotDif = this.rotation - car.rotation;
- }
- float vecdistance = (float)Math.sqrt((double)Math.pow((double)posDif.x, 2) + (double)Math.pow((double)posDif.y, 2));
- System.out.println("VecDistance = " + vecdistance);
- if(vecdistance > WARPING_BORDER){
- car.update(dt);
- return;
- }
- Vector2f tmp = new Vector2f((posDif.getX() * time), (posDif.getY()*time));
- car.position.setX((car.position.getX() + tmp.getX()));
- car.position.setY((car.position.getY() + tmp.getY()));
- car.velocity += velDif * time;
- car.rotation += rotDif * time;
- car.update(dt);
- }
- /*****************************************************************************/
- @Override
- public void update(float dt) {
- /*###################################################################*/
- if (!getMBSM_FLAG() && !getMBKM_FLAG()){
- interpolate(ipst, dt, true);
- super.update(dt);
- return;
- }
- /*###################################################################*/
- if (getMBKM_FLAG()) {
- long eventTime = this.getEventTime();
- long localTime = getCurrentRelativeTime();
- /*
- * Set car back to state before actual event
- */
- long snapEventTime = resetState();
- /*
- * Spool forward to eventTime
- */
- while(snapEventTime < eventTime){
- super.update(dt);
- snapEventTime += (dt*1000);
- }
- /*
- * Do a snapshot of current state
- */
- SnapshotEvent newsnap = new SnapshotEvent(this.rotation,
- this.velocity, this.position, eventTime);
- pel.add(newsnap);
- /*
- * Set Keys from new Event
- */
- this.setKeyFlag(this.settedKeyFlagID, this.settedKeyFlag);
- /*
- * Spool forward to now
- */
- while(eventTime < localTime){
- super.update(dt);
- eventTime += (dt*1000);
- }
- ipst = (float)getCurrentRelativeTime();
- System.out.println("ipst1 : " + ipst);
- interpolate(ipst, dt, false);
- this.setMBKM_FLAG(false);
- return;
- }
- /*###################################################################*/
- if (getMBSM_FLAG()) {
- long eventTime = this.getEventTime();
- long localTime = getCurrentRelativeTime();
- /*
- * Spool forward to now
- */
- while(eventTime < localTime){
- super.update(dt);
- eventTime += (dt*1000);
- }
- ipst = (float)getCurrentRelativeTime();
- interpolate(ipst, dt, false);
- this.setMBSM_FLAG(false);
- return;
- }
- }
- /*****************************************************************************/
- @Override
- public CarModel initModel() {
- return car.initModel();
- }
- @Override
- public CarModel getModel() {
- return car.getModel();
- }
- @Override
- public void updateModel() {
- car.updateModel();
- }
- @Override
- public void setPosition(Vector2f position) {
- car.setPosition(position);
- super.setPosition(position);
- }
- @Override
- public void setRotation(float rotation) {
- car.setRotation(rotation);
- super.setRotation(rotation);
- }
- @Override
- public void setVelocity(float velocity) {
- car.setVelocity(velocity);
- super.setVelocity(velocity);
- }
- @Override
- public void resetToStartFormation(int index, RaceTrack raceTrack) {
- super.resetToStartFormation(index, raceTrack);
- car.resetToStartFormation(index, raceTrack);
- }
- }
Add Comment
Please, Sign In to add comment