Skip to content

Commit

Permalink
Step 2.11: Draw and update screen in game loop
Browse files Browse the repository at this point in the history
  • Loading branch information
DAB0mB committed Aug 29, 2018
1 parent c2d56ba commit ab7d845
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions resources/scripts/engine/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Engine.Game = class Game {

this.assets = {};
this.events = new Map();
this.screen = new Engine.Screen(this);
this.keyStates = new Engine.KeyStates();
this.context = canvas.getContext("2d");
this.bufferedCanvas = document.createElement("canvas");
Expand All @@ -46,6 +47,13 @@ Engine.Game = class Game {
this.context.beginPath();
this.context.rect(0, 0, this.canvas.width, this.canvas.height);
this.context.fill();
this.drawScreen(this.context);
}

drawScreen(context) {
// If screen's assets are not yet loaded, don't draw it
if (this.screen.loading) return;
if (this.screen.draw) this.screen.draw(context);
}

update() {
Expand All @@ -56,6 +64,13 @@ Engine.Game = class Game {
this.updateScreen(span / this.speed);
}

updateScreen(span) {
this.screen.age += span;
// If screen's assets are not yet loaded, don't update it
if (this.screen.loading) return;
if (this.screen.update) this.screen.update(span);
}

// The main loop of the game
loop() {
// If paused, don't run loop. The canvas will remain as is
Expand Down

0 comments on commit ab7d845

Please sign in to comment.