]> git.plutz.net Git - flarejs/commitdiff
slightly simplified map drawing
authorPaul Hänsch <paul@plutz.net>
Wed, 12 Feb 2020 19:50:13 +0000 (20:50 +0100)
committerPaul Hänsch <paul@plutz.net>
Wed, 12 Feb 2020 19:50:13 +0000 (20:50 +0100)
engine.js

index 4321640070c6e70e4271365758fa6ef0d4e2d9b4..0750e87e1a35ea9bc148b5cdae84ab3667c0cba4 100644 (file)
--- 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]));
     }