From: Paul Hänsch Date: Wed, 12 Feb 2020 19:50:13 +0000 (+0100) Subject: slightly simplified map drawing X-Git-Url: http://git.plutz.net/?p=flarejs;a=commitdiff_plain;h=e41f7106b273f6ef0e3eec364b4eb346915b5382 slightly simplified map drawing --- 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])); }