/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package cubicsplines;
import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.BasicGame;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
/**
*
* @author Adam
*/
public class CubicSpline extends BasicGame {
Image plane;
Image planeMirror;
public static double getTime()
{
double time = (double)System.nanoTime() / 1000000000.0; // convert to double
return (time);
}
public CubicSpline()
{
super("Cubic Spline interpolator");
}
@Override
public void init(GameContainer gc) throws SlickException
{
gc.setTargetFrameRate(60);
plane = new Image("assets/block.png");
planeMirror = plane.copy();
}
ObjectProperty p1 = new ObjectProperty();
ObjectProperty p2 = new ObjectProperty();
Cubic remote = new Cubic();
int gameTick;
@Override
public void update(GameContainer gc, int delta) throws SlickException
{
double curTime = getTime();
// Minic the proccess of obtaining a network network packet
if(gameTick > 500)
{
gameTick = 0;
remote.addNewPosition
(
p2.posx, // from pos x
p2.posy, // from pos y
p1.posx, // to pos x
p1.posy, // to pos y
p2.velocityX, // from velocity x
p2.velocityY, // from velocity y
p1.velocityX, // to velocity x
p1.velocityY // to velocity y
);
p2.posx = p1.posx;
p2.posy = p1.posy;
p2.velocityX = p1.velocityX;
p2.velocityY = p1.velocityY;
}
remote.lerpDebug = true;
remote.follow();
gameTick += delta;
updateInput(gc, delta);
}
public void render(GameContainer gc, Graphics g) throws SlickException
{
plane.draw(p1.posx, p1.posy);
planeMirror.draw(remote.xPos, remote.yPos);
}
public void updateInput(GameContainer gc, int delta)
{
Input input = gc.getInput();
float rotation = this.plane.getRotation();
float hip = p1.speed * delta;
if(input.isKeyDown(Input.KEY_A))
{
this.plane.rotate(-0.2f * delta);
}
if(input.isKeyDown(Input.KEY_D))
{
this.plane.rotate(0.2f * delta);
}
if(input.isKeyDown(Input.KEY_S))
{
p1.velocityX += Math.sin(Math.toRadians(rotation));
p1.velocityY -= Math.cos(Math.toRadians(rotation));
}
if(input.isKeyDown(Input.KEY_W))
{
p1.velocityX -= Math.sin(Math.toRadians(rotation));
p1.velocityY += Math.cos(Math.toRadians(rotation));
}
// add drag
p1.velocityX *= p1.drag;
p1.velocityY *= p1.drag;
p1.posx += p1.velocityX * hip;
p1.posy += p1.velocityY * hip;
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws SlickException
{
AppGameContainer app = new AppGameContainer(new CubicSpline());
app.setDisplayMode(800, 600, false);
app.start();
}
}