Advertisement
Guest User

Untitled

a guest
Apr 21st, 2011
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.63 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5.  
  6. package cubicsplines;
  7.  
  8.  
  9. import org.newdawn.slick.AppGameContainer;
  10. import org.newdawn.slick.BasicGame;
  11. import org.newdawn.slick.GameContainer;
  12. import org.newdawn.slick.Graphics;
  13. import org.newdawn.slick.Image;
  14. import org.newdawn.slick.Input;
  15. import org.newdawn.slick.SlickException;
  16.  
  17. /**
  18.  *
  19.  * @author Adam
  20.  */
  21. public class CubicSpline extends BasicGame {
  22.  
  23.     Image plane;
  24.     Image planeMirror;
  25.    
  26.     public static double getTime()
  27.     {
  28.          double time = (double)System.nanoTime() / 1000000000.0; // convert to double
  29.          return (time);
  30.     }
  31.  
  32.     public CubicSpline()
  33.     {
  34.          super("Cubic Spline interpolator");
  35.     }
  36.  
  37.     @Override
  38.     public void init(GameContainer gc) throws SlickException
  39.     {
  40.         gc.setTargetFrameRate(60);
  41.  
  42.         plane = new Image("assets/block.png");
  43.         planeMirror = plane.copy();
  44.     }
  45.  
  46.     ObjectProperty p1 = new ObjectProperty();
  47.     ObjectProperty p2 = new ObjectProperty();
  48.  
  49.     Cubic remote = new Cubic();
  50.  
  51.  
  52.     int gameTick;
  53.  
  54.  
  55.     @Override
  56.     public void update(GameContainer gc, int delta) throws SlickException
  57.     {
  58.         double curTime = getTime();
  59.  
  60.         // Minic the proccess of obtaining a network network packet
  61.         if(gameTick > 500)
  62.         {
  63.             gameTick = 0;
  64.            
  65.  
  66.             remote.addNewPosition
  67.             (
  68.                     p2.posx,        // from pos x
  69.                     p2.posy,        // from pos y
  70.                     p1.posx,        // to pos x
  71.                     p1.posy,        // to pos y
  72.                     p2.velocityX,   // from velocity x
  73.                     p2.velocityY,   // from velocity y
  74.                     p1.velocityX,   // to velocity x
  75.                     p1.velocityY    // to velocity y
  76.             );
  77.  
  78.             p2.posx = p1.posx;
  79.             p2.posy = p1.posy;
  80.  
  81.             p2.velocityX = p1.velocityX;
  82.             p2.velocityY = p1.velocityY;
  83.         }
  84.  
  85.  
  86.          remote.lerpDebug = true;
  87.          remote.follow();
  88.  
  89.        
  90.         gameTick += delta;
  91.         updateInput(gc, delta);
  92.     }
  93.  
  94.     public void render(GameContainer gc, Graphics g) throws SlickException
  95.     {
  96.         plane.draw(p1.posx, p1.posy);
  97.         planeMirror.draw(remote.xPos, remote.yPos);
  98.     }
  99.  
  100.     public void updateInput(GameContainer gc, int delta)
  101.     {
  102.         Input input = gc.getInput();
  103.        
  104.         float rotation =  this.plane.getRotation();
  105.         float hip =  p1.speed * delta;
  106.  
  107.         if(input.isKeyDown(Input.KEY_A))
  108.         {
  109.             this.plane.rotate(-0.2f * delta);
  110.         }
  111.  
  112.         if(input.isKeyDown(Input.KEY_D))
  113.         {
  114.             this.plane.rotate(0.2f * delta);
  115.         }
  116.  
  117.         if(input.isKeyDown(Input.KEY_S))
  118.         {
  119.             p1.velocityX += Math.sin(Math.toRadians(rotation));
  120.             p1.velocityY -= Math.cos(Math.toRadians(rotation));
  121.         }
  122.  
  123.         if(input.isKeyDown(Input.KEY_W))
  124.         {
  125.             p1.velocityX -=  Math.sin(Math.toRadians(rotation));
  126.             p1.velocityY +=  Math.cos(Math.toRadians(rotation));
  127.         }
  128.  
  129.         // add drag
  130.         p1.velocityX *= p1.drag;
  131.         p1.velocityY *= p1.drag;
  132.  
  133.         p1.posx += p1.velocityX * hip;
  134.         p1.posy += p1.velocityY * hip;
  135.     }
  136.  
  137.     /**
  138.      * @param args the command line arguments
  139.      */
  140.     public static void main(String[] args) throws SlickException
  141.     {
  142.          AppGameContainer app = new AppGameContainer(new CubicSpline());
  143.          app.setDisplayMode(800, 600, false);
  144.          app.start();
  145.     }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement