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 int WIDTH, HEIGHT;
|
||||||
public static String startScene = null;
|
public static String startScene = null;
|
||||||
public static String name = null;
|
public static String name = null;
|
||||||
public static BufferStrategy bs;
|
|
||||||
|
|
||||||
public Engine(String gameFolder) {
|
public Engine(String gameFolder) {
|
||||||
|
|
||||||
|
|
@ -61,23 +60,34 @@ public class Engine extends Canvas {
|
||||||
this.requestFocus();
|
this.requestFocus();
|
||||||
this.addKeyListener(new Input());
|
this.addKeyListener(new Input());
|
||||||
|
|
||||||
createBufferStrategy(2);
|
//createBufferStrategy(2);
|
||||||
bs = getBufferStrategy();
|
//bs = getBufferStrategy();
|
||||||
|
|
||||||
|
Time.nanos = System.nanoTime();
|
||||||
|
|
||||||
while(true) {
|
while(true) {
|
||||||
long startTime = System.currentTimeMillis();
|
|
||||||
updateScene();
|
Time.startTime = System.currentTimeMillis();
|
||||||
Graphics2D g = (Graphics2D)bs.getDrawGraphics();
|
if (System.currentTimeMillis() > Time.nextSecond) {
|
||||||
render(g);
|
Time.nextSecond += 1000;
|
||||||
bs.show();
|
Time.FPS = Time.framesInCurrentSecond;
|
||||||
int elapsed = (int)(System.currentTimeMillis() - startTime);
|
Time.framesInCurrentSecond = 0;
|
||||||
try{
|
//System.out.println("FPS: " + Time.FPS);
|
||||||
Thread.sleep(17 - elapsed);
|
|
||||||
}catch(Exception e) {
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
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() {
|
private void updateScene() {
|
||||||
|
|
@ -144,8 +154,8 @@ public class Engine extends Canvas {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void render(Graphics2D g) {
|
public void update(Graphics g) {
|
||||||
SceneManager.render(g);
|
SceneManager.render((Graphics2D)g);
|
||||||
g.setColor(Color.BLACK);
|
g.setColor(Color.BLACK);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,6 @@ public class RectRenderer extends DiveScript{
|
||||||
public void render(Graphics2D g) {
|
public void render(Graphics2D g) {
|
||||||
g.setColor(color);
|
g.setColor(color);
|
||||||
g.fillRect((int)entity.x, (int)entity.y, width, height);
|
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 class RigidBody extends DiveScript{
|
||||||
|
|
||||||
public float dx, dy, drot;
|
public double dx, dy, drot;
|
||||||
public float friction;
|
public double friction;
|
||||||
|
|
||||||
public void update() {
|
public void update() {
|
||||||
entity.x += dx;
|
entity.x += dx * Time.deltaTime;
|
||||||
entity.y += dy;
|
entity.y += dy * Time.deltaTime;
|
||||||
entity.rotation += drot;
|
entity.rotation += drot * Time.deltaTime;
|
||||||
|
double friction = 1 - ((1 - this.friction) * Time.deltaTime);
|
||||||
dx *= friction;
|
dx *= friction;
|
||||||
dy *= friction;
|
dy *= friction;
|
||||||
drot *= 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