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]));
}