implement loading screen

This commit is contained in:
LabyStudio
2022-02-03 14:50:32 +01:00
parent 356eab2b0c
commit 64b84c3f64
12 changed files with 212 additions and 18 deletions
+41 -6
View File
@@ -4,10 +4,22 @@ window.Minecraft = class {
* Create Minecraft instance and render it on a canvas
*/
constructor(canvasWrapperId) {
this.currentScreen = null;
this.loadingScreen = null;
// Create window and world renderer
this.window = new GameWindow(this, canvasWrapperId);
this.worldRenderer = new WorldRenderer(this, this.window);
this.timer = new Timer(20);
// Create current screen and overlay
this.ingameOverlay = new IngameOverlay(this.window);
// Display loading screen
this.loadingScreen = new GuiLoadingScreen();
this.loadingScreen.setTitle("Building terrain...");
this.displayScreen(this.loadingScreen);
this.frames = 0;
this.lastTime = Date.now();
@@ -22,10 +34,6 @@ window.Minecraft = class {
this.player = new Player(this.world);
this.pickedBlock = 1;
// Create current screen and overlay
this.ingameOverlay = new IngameOverlay(this.window);
this.currentScreen = null;
// Initialize
this.init();
}
@@ -80,25 +88,52 @@ window.Minecraft = class {
onRender(partialTicks) {
// Player rotation
if (this.window.mouseLocked) {
if (this.window.mouseLocked && !(this.currentScreen === "null")) {
this.player.turn(this.window.mouseMotionX, this.window.mouseMotionY);
this.window.mouseMotionX = 0;
this.window.mouseMotionY = 0;
}
while (this.world.updateLights()) ;
// Update lights
while (this.world.updateLights()) {
// Empty
}
// Render the game
this.worldRenderer.render(partialTicks);
}
displayScreen(screen) {
// Switch screen
this.currentScreen = screen;
// Initialize new screen
if (screen === null) {
this.window.requestFocus();
} else {
screen.init(this, this.window.width, this.window.height);
}
}
onTick() {
// Tick world
this.world.onTick();
// Tick the player
this.player.onTick();
// Update loading progress
if (!(this.loadingScreen === null)) {
let progress = Math.max(0, 1 - this.world.lightUpdateQueue.length / 10000);
this.loadingScreen.setProgress(progress);
// Finish loading
if (progress >= 1) {
this.loadingScreen = null;
this.displayScreen(null);
}
}
}
onMouseClicked(button) {