From: Paul Hänsch Date: Fri, 14 Feb 2020 23:36:50 +0000 (+0100) Subject: Enable map teleporters, process mapmod events, clip map drawing on canvas for much... X-Git-Url: https://git.plutz.net/?a=commitdiff_plain;h=269ec3d8ebb9c20b030707d452e4b0b8c6eaeb7b;p=flarejs Enable map teleporters, process mapmod events, clip map drawing on canvas for much better performance --- diff --git a/engine.js b/engine.js index cca0717..52cf869 100644 --- a/engine.js +++ b/engine.js @@ -80,27 +80,26 @@ function Textfile(txtfile) { break; // frame duration from animation file (im millisec) case /^duration=[0-9]+ms$/.test(line): - ref["duration"] = line.split(/=|ms$/)[1]; + ref.duration = line.split(/=|ms$/)[1]; break; // frame duration from animation file (in whole seconds) case /^duration=[0-9]+s$/.test(line): - ref["duration"] = line.split(/=|s$/)[1] * 1000; + ref.duration = line.split(/=|s$/)[1] * 1000; break; - // numeric array types - case /^(location|hero_pos|render_size|render_offset)=[0-9,-]+$/.test(line): - key = line.split(/[=]/)[0]; value = line.split(/[=]/)[1].split(/,/); - ref[key] = value.map(m => m * 1); + // mapmod events + case /^mapmod=.*$/.test(line): + ref.mapmod = line.split(/[=]/)[1].split(/[;]/).map( m => m.split(/[,]/).map( n => n * 1 ? n * 1 : n) ); break; - // general numeric value (key=#) - case /^[^#].*=[0-9]+$/.test(line): - key = line.split(/[=]/)[0]; value = line.split(/=/).slice(1) * 1; - if (!ref[key]) ref[key] = value; - else if (!Array.isArray(ref[key])) ref[key] = [ref[key],value]; - else if ( Array.isArray(ref[key])) ref[key].push(value); + // general array type, will not be aggregated + case /^[^#].*=.*,.*$/.test(line): + key = line.split(/[=]/)[0]; value = line.split(/[=]/)[1].split(/,/); + ref[key] = value.map(m => m * 1 ? m * 1 : m); // parse numerics break; - // general key=value (non-numeric or non-scalar) + // general key=value case /^[^#].*=.+$/.test(line): - key = line.split(/[=]/)[0]; value = line.split(/=/).slice(1).join("="); + key = line.split(/[=]/)[0]; + value = line.split(/=/).slice(1).join("="); + value = value * 1 ? value * 1 : value; // try to parse as numeric if (!ref[key]) ref[key] = value; else if (!Array.isArray(ref[key])) ref[key] = [ref[key],value]; else if ( Array.isArray(ref[key])) ref[key].push(value); @@ -173,6 +172,8 @@ function Map(textdef) { const bg = this.info.layer.find(l => l.type == "background").data; const ob = this.info.layer.find(l => l.type == "object").data; var i, mm = []; + //var is = (posx > w * tw - canvas.canvas.width || posy < 0) ? 0 : this.tileAt(posx + canvas.canvas.width, posy); + // var ie = this.tileAt(posx, + canvas.canvas.height - posy); // mobs only have x/y pixel positions, // to draw them in order with map tiles we set up @@ -185,9 +186,11 @@ function Map(textdef) { // draw background layer first for ( i = 0; i < h * w; i++ ) draw_tile(bg[i], posx + dx[i], posy + dy[i]); + // for ( i = is; i < ie; i++ ) draw_tile(bg[i], posx + dx[i], posy + dy[i]); // draw object layer and mobs for ( i = 0; i < h * w; i++ ) { + // for ( i = is; i < ie; 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])); if (npcs[i]) npcs[i].draw(posx + dx[i], posy + dy[i]); @@ -203,9 +206,11 @@ function Map(textdef) { if (t && f) { frame = ((performance.now() - frametime) / f[0][2] |0) % f.length; + if ( x + t[2] - t[4] > 0 && y + t[3] - t[5] > 0 && x - t[4] < canvas.canvas.width && y - t[5] < canvas.canvas.height) canvas.drawImage(gfx[tileset.img], f[frame][0], f[frame][1], t[2], t[3], x - t[4], y - t[5], t[2], t[3]); } else if (t) { + if ( x + t[2] - t[4] > 0 && y + t[3] - t[5] > 0 && x - t[4] < canvas.canvas.width && y - t[5] < canvas.canvas.height) canvas.drawImage(gfx[tileset.img], t[0], t[1], t[2], t[3], x - t[4], y - t[5], t[2], t[3]); } @@ -425,14 +430,25 @@ function Controls(hero){ } function events(x, y) { - const i = map.tileAt(x,y); var ev; + const i = map.tileAt(x,y); let ev, e, n; if (map.events[i]) { - // intermap + // intramap (teleporters) + if ( ev = map.events[i].find(e => (e.activate == "on_trigger" && e.intramap)) ){ + ev = ev.intramap; + hero.place( map.xOf(ev[0], ev[1]), map.yOf(ev[0], ev[1]) ); + map.center( map.xOf(ev[0], ev[1]), map.yOf(ev[0], ev[1]) ); + } + // mapmod (e.g. opening doors, activating platforms, changing terrain, ...) + for (ev of map.events[i].filter(e => (e.activate == "on_trigger" && e.mapmod)) ){ + for (ev of ev.mapmod) + map.info.layer.find(l => l.type == ev[0]).data[ev[2] * map.info.header.width + ev[1]] = ev[3]; + } + // intermap (must be last because loading new map breaks further event search) if ( ev = map.events[i].find(e => (e.activate == "on_trigger" && e.intermap)) ){ - ev = ev.intermap.split(/[,]/); + ev = ev.intermap; map = new Map(ev[0]); - hero.place( map.xOf(ev[1] * 1, ev[2] * 1), map.yOf(ev[1] * 1, ev[2] * 1) ); - map.center( map.xOf(ev[1] * 1, ev[2] * 1), map.yOf(ev[1] * 1, ev[2] * 1) ); + hero.place( map.xOf(ev[1], ev[2]), map.yOf(ev[1], ev[2]) ); + map.center( map.xOf(ev[1], ev[2]), map.yOf(ev[1], ev[2]) ); } } } @@ -472,7 +488,7 @@ window.addEventListener("resize", function(){ // player = new Mob("animations/enemies/zombie.txt"); player.stats = { speed: 3 }; player = new Hero(); -map = new Map("/maps/arrival.txt"); +map = new Map("/maps/perdition_harbor.txt"); player.place(map.xOf(map.info.header.hero_pos[0], map.info.header.hero_pos[1]), map.yOf(map.info.header.hero_pos[0], map.info.header.hero_pos[1])); player.direct(6).stance();