From: Paul Hänsch Date: Mon, 17 Feb 2020 01:47:22 +0000 (+0100) Subject: process INCLUDE directive when loading file X-Git-Url: http://git.plutz.net/?p=flarejs;a=commitdiff_plain process INCLUDE directive when loading file --- diff --git a/engine.js b/engine.js index d4b9209..0390d83 100644 --- a/engine.js +++ b/engine.js @@ -26,8 +26,23 @@ function Textfile(txtfile) { fetch.open("GET", txtfile, false); fetch.send(); + function merge(obj, file) { + for (each in file) { + if (Array.isArray(obj[each]) && Array.isArray(file[each])) obj[each] = [ ...obj[each], ...file[each] ]; + else if (Array.isArray(obj[each])) obj[each] = [ ...obj[each], file[each] ]; + else if (Array.isArray(file[each])) obj[each] = [ obj[each], ...file[each] ]; + else if (each in obj) obj[each] = [ obj[each], file[each] ]; + else obj[each] = file[each]; + } + } + var line, key, value, ref = this, section; for (line of fetch.responseText.split('\n')) switch(true) { + // INCLUDE file + case /^INCLUDE .+$/.test(line): + //console.log("Including: " + line.split(/ /)[1]); + merge(ref, new Textfile(line.split(/ /)[1])); + break; // section headers that always become array elements case /^\[(npc|event|layer|dialog)\]$/.test(line): section = line.split(/[\]\[]/)[1]; ref = {}; @@ -41,6 +56,10 @@ function Textfile(txtfile) { else if (!Array.isArray(this[section])) this[section] = [this[section],ref]; else if ( Array.isArray(this[section])) this[section].push(ref); break; + // Clear section on empty line + case /^ *$/.test(line): + ref = this; + break; // frame definition from animation files // frame=#frame,direction,srcX,srcY,width,height,dstX,dstY case /^frame=[0-9]+,[0-7](,[0-9-]+){6}$/.test(line):