2018-08-24 13:48:53 +02:00
|
|
|
const Screens = {
|
|
|
|
Exit: -1,
|
|
|
|
Menu: 0,
|
|
|
|
Game: 1,
|
|
|
|
}
|
|
|
|
|
2018-08-21 15:58:08 +02:00
|
|
|
class Game {
|
|
|
|
|
|
|
|
constructor(canvas) {
|
2018-08-22 17:42:19 +02:00
|
|
|
|
|
|
|
this.canvas = canvas;
|
|
|
|
|
2018-08-21 15:58:08 +02:00
|
|
|
// Create menu
|
2018-08-24 13:48:53 +02:00
|
|
|
this.menu = new Menu(canvas);
|
2018-08-21 15:58:08 +02:00
|
|
|
|
|
|
|
// Create game scene
|
|
|
|
let box = new Box();
|
|
|
|
this.scene = new Scene(document.getElementById('canvas'), box);
|
|
|
|
|
2018-08-24 13:48:53 +02:00
|
|
|
this.changeScreen(this.menu);
|
2018-08-21 15:58:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
changeScreen(screen) {
|
2018-08-23 13:59:52 +02:00
|
|
|
if (this.mainScreen !== undefined) {
|
2018-08-21 15:58:08 +02:00
|
|
|
this.mainScreen.removeListeners();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.mainScreen = screen;
|
|
|
|
screen.status = Status.Running;
|
|
|
|
screen.addListeners();
|
|
|
|
}
|
|
|
|
|
2018-08-22 11:20:45 +02:00
|
|
|
update(time = 0.02) {
|
2018-08-21 15:58:08 +02:00
|
|
|
this.mainScreen.update(time);
|
|
|
|
|
|
|
|
if (this.mainScreen.status === Status.Finished) {
|
2018-08-24 13:48:53 +02:00
|
|
|
if (this.mainScreen.after === Screens.Game) {
|
2018-08-23 11:50:05 +02:00
|
|
|
this.scene.initialize();
|
2018-08-21 15:58:08 +02:00
|
|
|
this.changeScreen(this.scene);
|
2018-08-24 13:48:53 +02:00
|
|
|
} else if (this.mainScreen.after === Screens.Exit) {
|
2018-08-22 17:42:19 +02:00
|
|
|
if (navigator.app !== undefined && typeof navigator.app.exitApp === 'function') {
|
|
|
|
navigator.app.exitApp();
|
2018-08-22 16:34:14 +02:00
|
|
|
}
|
2018-08-24 13:48:53 +02:00
|
|
|
} else if (this.mainScreen.after === Screens.Menu) {
|
|
|
|
this.changeScreen(this.menu);
|
2018-08-21 15:58:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2018-08-22 17:42:19 +02:00
|
|
|
this.autoResize();
|
2018-08-21 15:58:08 +02:00
|
|
|
this.mainScreen.render();
|
|
|
|
}
|
2018-08-22 17:42:19 +02:00
|
|
|
|
|
|
|
autoResize() {
|
|
|
|
this.canvas.width = window.innerWidth;
|
|
|
|
this.canvas.height = window.innerHeight;
|
|
|
|
}
|
2018-08-21 15:58:08 +02:00
|
|
|
}
|