From e41f7106b273f6ef0e3eec364b4eb346915b5382 Mon Sep 17 00:00:00 2001
From: =?utf8?q?Paul=20H=C3=A4nsch?= <paul@plutz.net>
Date: Wed, 12 Feb 2020 20:50:13 +0100
Subject: [PATCH] slightly simplified map drawing

---
 engine.js | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)

diff --git a/engine.js b/engine.js
index 4321640..0750e87 100644
--- a/engine.js
+++ b/engine.js
@@ -143,22 +143,22 @@ function Map(textdef) {
   this.draw = function(mobs) {
     const bg = this.info.layer.find(l => l.type == "background").data;
     const ob = this.info.layer.find(l => l.type == "object").data;
-    var x, y, i, mm = [];
+    var i, mm = [];
 
-    // mobs only have x/y pixel positions, to draw them in order with map tiles
-    // we set up an array with current tile positions of mobs
+    // mobs only have x/y pixel positions,
+    // to draw them in order with map tiles we set up
+    // an array with current tile positions of mobs
     mobs.forEach(m => {
       let i = this.tileAt(m.position[0], m.position[1]);
       mm[i] = mobs.filter(m => i == this.tileAt(m.position[0], m.position[1]));
     });
     canvas.fillRect(0,0, canvas.canvas.width, canvas.canvas.height);
 
-    for ( y = 0; y < h; y++ ) for ( x = 0; x < w; x++ ) {
-      i = y * h + x;
-      draw_tile(bg[i], posx + dx[i], posy + dy[i]);
-    }
-    for ( y = 0; y < h; y++ ) for ( x = 0; x < w; x++ ) {
-      i = y * h + x;
+    // draw background layer first
+    for ( i = 0; i < h * w; i++ ) draw_tile(bg[i], posx + dx[i], posy + dy[i]);
+
+    // draw object layer and mobs
+    for ( i = 0; i < h * w; i++ ) {
       draw_tile(ob[i], posx + dx[i], posy + dy[i]);
       if (mm[i]) mm[i].forEach(m => m.draw(posx + m.position[0], posy + m.position[1]));
     }
-- 
2.39.5