Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package lab;
import javafx.animation.AnimationTimer;
import javafx.scene.canvas.Canvas;
public class GameController {
private World world;
private Canvas canvas;
private AnimationTimer animationTimer;
public GameController(Canvas canvas) {
this.canvas = canvas;
}
public void startGame() {
this.world = new World(canvas.getWidth(), canvas.getHeight());
//Draw scene on a separate thread to avoid blocking UI.
animationTimer = new AnimationTimer() {
private Long previous;
@Override
public void handle(long now) {
if (previous == null) {
previous = now;
} else {
drawScene((now - previous)/1e9);
previous = now;
}
}
};
animationTimer.start();
}
public void stopGame() {
animationTimer.stop();
}
private void drawScene(double deltaT) {
world.draw(canvas);
world.simulate(deltaT);
}
}