From 47fa4b01ad3ade8f5f9ef4c1bab972cf7ba4bbef Mon Sep 17 00:00:00 2001 From: =?utf8?q?Paul=20H=C3=A4nsch?= Date: Sat, 8 Feb 2020 06:36:28 +0100 Subject: [PATCH] map drawing functions, improved file parsing --- engine.js | 67 ++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 56 insertions(+), 11 deletions(-) diff --git a/engine.js b/engine.js index 5f2c209..c1b7a9d 100644 --- a/engine.js +++ b/engine.js @@ -9,28 +9,48 @@ function Textfile(txtfile) { var line, key, value, ref = this, section; for (line of fetch.responseText.split('\n')) switch(true) { case /^\[.*\]$/.test(line): - section = line.split(/[\]\[]/)[1]; - if (!this[section]) this[section] = {}; - else if (!Array.isArray(this[section])) this[section] = [this[section],{}]; - else if ( Array.isArray(this[section])) this[section].push({}); - ref = this[section]; + section = line.split(/[\]\[]/)[1]; ref = {}; + if (!this[section]) this[section] = ref; + else if (!Array.isArray(this[section])) this[section] = [this[section],ref]; + else if ( Array.isArray(this[section])) this[section].push(ref); break; case /^frame=[0-9]+,[0-7](,[0-9-]+){6}$/.test(line): key = line.split(/[=,]/).slice(1,3); value = line.split(/,/).slice(-6); if (!ref.frame) ref.frame = []; if (!ref.frame[key[0]]) ref.frame[key[0]] = []; - ref.frame[key[0]][key[1]] = value; + ref.frame[key[0]][key[1]] = value.map(x => parseInt(x)); break; case /^layer=[0-9]+,/.test(line): key = line.split(/[=,]/)[1]; value = line.split(/,/).slice(1); - if (!ref.layer) ref.layer = []; - ref.layer[key] = value; + if (!ref.layer) ref.layer = []; ref.layer[key] = value; + break; + case /^data=$/.test(line): + ref.data = []; + break; + case /^[0-9,]+,$/.test(line): + value = line.split(/,/).slice(0,-1); + ref.data = ref.data.concat(value); + break; + case /^[0-9,]+$/.test(line): + value = line.split(/,/); + ref.data = ref.data.concat(value); + ref.data = ref.data.map(x => parseInt(x)); + break; + case /^tile=[0-9]+,/.test(line): + key = line.split(/[=,]/)[1]; value = line.split(/,/).slice(1); + if (!ref.tile) ref.tile = []; ref.tile[key] = value.map(x => parseInt(x)); + break; + case /^animation=[0-9]+;.*;$/.test(line): + key = line.split(/[=;]/)[1]; value = line.split(/;/).slice(1,-1); + value = value.map(x => x.split(/,/)); + value.forEach(x => x[2] = parseInt(x[2])); + if (!ref.animation) ref.animation = []; ref.animation[key] = value; break; case /^duration=[0-9]+ms$/.test(line): - ref["duration"] = parseInt(line.split(/=|ms$/)[1]); + ref["duration"] = line.split(/=|ms$/)[1]; break; case /^duration=[0-9]+s$/.test(line): - ref["duration"] = parseInt(line.split(/=|s$/)[1]) * 1000; + ref["duration"] = line.split(/=|s$/)[1] * 1000; break; case /^[^#].*=.+$/.test(line): key = line.split(/[=]/)[0]; value = line.split(/=/).slice(1).join("="); @@ -41,6 +61,29 @@ function Textfile(txtfile) { } } +function Map() { + this.tileset = new Textfile("/tilesetdefs/tileset_grassland.txt"); + this.frametime = performance.now(); + if (! gfx[this.tileset.img]) { + gfx[this.tileset.img] = document.createElement("img"); + gfx[this.tileset.img].setAttribute("src", this.tileset.img); + } + + this.draw_tile = function(tile, x, y) { + t = this.tileset.tile[tile] + + if (this.tileset.animation[tile]) { + f = this.tileset.animation[tile]; + frame = ((performance.now() - this.frametime) / f[0][2] |0) % f.length; + canvas.drawImage(gfx[this.tileset.img], f[frame][0], f[frame][1], t[2], t[3], + x - t[4], y - t[5], t[2], t[3]); + } else { + canvas.drawImage(gfx[this.tileset.img], t[0], t[1], t[2], t[3], + x - t[4], y - t[5], t[2], t[3]); + } + } +} + function Mob(textdef) { if (! gamedata[textdef]) gamedata[textdef] = new Textfile(textdef); this.info = gamedata[textdef]; @@ -161,5 +204,7 @@ function Hero(gender = "female", hair = "short"){ canvas = document.getElementById("view").getContext("2d"); player = new Hero().place(240, 160).direct(5); +map = new Map(); + +setInterval( function() { canvas.clearRect(0,0, 480, 320); map.draw_tile(265, 240, 160); player.draw(); }, 33.33); -setInterval( function() { canvas.clearRect(0,0, 480, 320); player.draw(); }, 33.33); -- 2.39.2