]> git.plutz.net Git - flarejs/commitdiff
map drawing functions, improved file parsing
authorPaul Hänsch <paul@plutz.net>
Sat, 8 Feb 2020 05:36:28 +0000 (06:36 +0100)
committerPaul Hänsch <paul@plutz.net>
Sat, 8 Feb 2020 05:36:28 +0000 (06:36 +0100)
engine.js

index 5f2c2093db9aa760512c152ec46ea6e58f99d991..c1b7a9d454b19e610bc9edf172480ff01a3efbc8 100644 (file)
--- 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);