trying to sort out time, failing pretty hard
parent
efeef2782c
commit
22573653ad
|
|
@ -19,7 +19,6 @@ public class Engine extends Canvas {
|
|||
public static int WIDTH, HEIGHT;
|
||||
public static String startScene = null;
|
||||
public static String name = null;
|
||||
public static BufferStrategy bs;
|
||||
|
||||
public Engine(String gameFolder) {
|
||||
|
||||
|
|
@ -61,23 +60,34 @@ public class Engine extends Canvas {
|
|||
this.requestFocus();
|
||||
this.addKeyListener(new Input());
|
||||
|
||||
createBufferStrategy(2);
|
||||
bs = getBufferStrategy();
|
||||
//createBufferStrategy(2);
|
||||
//bs = getBufferStrategy();
|
||||
|
||||
Time.nanos = System.nanoTime();
|
||||
|
||||
while(true) {
|
||||
long startTime = System.currentTimeMillis();
|
||||
updateScene();
|
||||
Graphics2D g = (Graphics2D)bs.getDrawGraphics();
|
||||
render(g);
|
||||
bs.show();
|
||||
int elapsed = (int)(System.currentTimeMillis() - startTime);
|
||||
try{
|
||||
Thread.sleep(17 - elapsed);
|
||||
}catch(Exception e) {
|
||||
|
||||
|
||||
Time.startTime = System.currentTimeMillis();
|
||||
if (System.currentTimeMillis() > Time.nextSecond) {
|
||||
Time.nextSecond += 1000;
|
||||
Time.FPS = Time.framesInCurrentSecond;
|
||||
Time.framesInCurrentSecond = 0;
|
||||
//System.out.println("FPS: " + Time.FPS);
|
||||
}
|
||||
}
|
||||
Time.framesInCurrentSecond++;
|
||||
|
||||
render();
|
||||
updateScene();
|
||||
Time.tickTime = (System.nanoTime() - Time.nanos)/1000d;
|
||||
Time.deltaTime = Time.tickTime * Time.timeScale;
|
||||
Time.nanos = System.nanoTime();
|
||||
// System.out.println("dTime: " + Time.deltaTime);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void render() {
|
||||
repaint();
|
||||
}
|
||||
|
||||
private void updateScene() {
|
||||
|
|
@ -144,8 +154,8 @@ public class Engine extends Canvas {
|
|||
|
||||
}
|
||||
|
||||
private void render(Graphics2D g) {
|
||||
SceneManager.render(g);
|
||||
public void update(Graphics g) {
|
||||
SceneManager.render((Graphics2D)g);
|
||||
g.setColor(Color.BLACK);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,6 +15,6 @@ public class RectRenderer extends DiveScript{
|
|||
public void render(Graphics2D g) {
|
||||
g.setColor(color);
|
||||
g.fillRect((int)entity.x, (int)entity.y, width, height);
|
||||
|
||||
//System.out.println("" + entity.x + " " + entity.y);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,13 +2,14 @@ package diveengine2d;
|
|||
|
||||
public class RigidBody extends DiveScript{
|
||||
|
||||
public float dx, dy, drot;
|
||||
public float friction;
|
||||
public double dx, dy, drot;
|
||||
public double friction;
|
||||
|
||||
public void update() {
|
||||
entity.x += dx;
|
||||
entity.y += dy;
|
||||
entity.rotation += drot;
|
||||
entity.x += dx * Time.deltaTime;
|
||||
entity.y += dy * Time.deltaTime;
|
||||
entity.rotation += drot * Time.deltaTime;
|
||||
double friction = 1 - ((1 - this.friction) * Time.deltaTime);
|
||||
dx *= friction;
|
||||
dy *= friction;
|
||||
drot *= friction;
|
||||
|
|
|
|||
|
|
@ -0,0 +1,13 @@
|
|||
package diveengine2d;
|
||||
|
||||
public class Time {
|
||||
|
||||
public static long nanos;
|
||||
public static int framesInCurrentSecond;
|
||||
public static int FPS = 0;
|
||||
public static double tickTime = 0;
|
||||
public static double timeScale = 1;
|
||||
public static double deltaTime = 0;
|
||||
public static long nextSecond = System.currentTimeMillis() + 1000, startTime = 0;
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue