From 12f22e0cbb36f931782e20f0b890ca7d2c3959f0 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Paul=20H=C3=A4nsch?= Date: Sat, 8 Feb 2020 02:46:20 +0100 Subject: [PATCH] load game data dynamically from files --- engine.js | 88 ++++++++++++++++++++++++++++++++++++++++--------------- index.cgi | 1 - 2 files changed, 64 insertions(+), 25 deletions(-) diff --git a/engine.js b/engine.js index c70679c..3bdf87b 100644 --- a/engine.js +++ b/engine.js @@ -1,16 +1,57 @@ +var gamedata = {}; +var gfx = {}; + +function Textfile(txtfile) { + var lines, fetch = new XMLHttpRequest(); + fetch.open("GET", txtfile, false); + fetch.send(); + + 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]; + 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; + 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; + break; + case /^duration=[0-9]+ms$/.test(line): + ref["duration"] = parseInt(line.split(/=|ms$/)[1]); + break; + case /^duration=[0-9]+s$/.test(line): + ref["duration"] = parseInt(line.split(/=|s$/)[1]) * 1000; + break; + case /^[^#].*=.+$/.test(line): + key = line.split(/[=]/)[0]; value = line.split(/=/).slice(1).join("="); + 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); + break; + } +} + function Mob(textdef) { + if (! gamedata[textdef]) gamedata[textdef] = new Textfile(textdef); + this.info = gamedata[textdef]; this.direction = 0; this.position = [0, 0]; - this.info = gamedata[textdef]; this.animation = "stance"; this.previous_animation = ""; this.frametime = performance.now(); - this.image = document.querySelector("img[src='"+ this.info.image +"']"); - if (!this.image) { - this.image = document.createElement("img"); - this.image.setAttribute("src", this.info.image); - this.image.setAttribute("style", "display: none;"); - document.querySelector("body").appendChild(this.image); + if (! gfx[this.info.image]) { + gfx[this.info.image] = document.createElement("img"); + gfx[this.info.image].setAttribute("src", this.info.image); } this.place = function(x, y) { this.position = [x, y]; return this; } @@ -24,14 +65,14 @@ function Mob(textdef) { this.draw = function(){ var f, a = this.info[this.animation]; - var frame = ( performance.now() - this.frametime ) * a.frames.length / a.duration | 0; + var frame = ( performance.now() - this.frametime ) * a.frames / a.duration | 0; switch(a.type){ case "looped": - frame = frame % a.frames.length; + frame = frame % a.frames; break; case "play_once": - if ( frame >= a.frames.length ){ + if ( frame >= a.frames ){ this.animation = this.previous_animation; this.previous_animation = ""; this.frametime = performance.now(); @@ -40,27 +81,27 @@ function Mob(textdef) { } break; case "back_forth": - frame = frame % (a.frames.length * 2 - 2); - if ( frame >= a.frames.length ){ - frame = a.frames.length - frame % a.frames.length - 1; + frame = frame % (a.frames * 2 - 2); + if ( frame >= a.frames ){ + frame = a.frames - frame % a.frames - 1; } break; default: break; } - f = a.frames[frame][this.direction]; + f = a.frame[frame][this.direction]; - canvas.drawImage(this.image, f[0], f[1], f[2], f[3], + canvas.drawImage(gfx[this.info.image], f[0], f[1], f[2], f[3], this.position[0] - f[4], this.position[1] - f[5], f[2], f[3]); - // var fetch = new XMLHttpRequest(); - // fetch.open("GET", textdef, false); fetch.send(); - // this.description = fetch.responseText.split('\n'); } } -function Player(gender = "female", hair = "short"){ - this.x=0; this.y=0; this.direction=0; +function Hero(gender = "female", hair = "short"){ + this.position = [0,0]; this.direction = 0; + + if (! gamedata["/engine/hero_layers.txt"]) + gamedata["/engine/hero_layers.txt"] = new Textfile("/engine/hero_layers.txt"); this.limbs = { head : (gender == "female")?new Mob("/animations/avatar/female/head_long.txt") :new Mob("/animations/avatar/male/head_"+hair+".txt"), @@ -73,7 +114,7 @@ function Player(gender = "female", hair = "short"){ } this.place = function(x,y) { - this.x = x; this.y = y; + this.position = [x,y]; for (var limb in this.limbs) this.limbs[limb].place(x,y); return this; } @@ -87,13 +128,12 @@ function Player(gender = "female", hair = "short"){ return this; } this.draw = function(){ - gamedata["/engine/hero_layers.txt"].layers[this.direction].forEach(limb => this.limbs[limb].draw()); + gamedata["/engine/hero_layers.txt"].layer[this.direction].forEach(limb => this.limbs[limb].draw()); return this; } } canvas = document.getElementById("view").getContext("2d"); -// player = new Mob("/animations/avatar/male/clothes.txt"); -player = new Player().place(240, 160).direct(5); +player = new Hero().place(240, 160).direct(5); setInterval( function() { canvas.clearRect(0,0, 480, 320); player.draw(); }, 50); diff --git a/index.cgi b/index.cgi index 0d13998..4a594e6 100755 --- a/index.cgi +++ b/index.cgi @@ -13,7 +13,6 @@ if [ "$_PATH" = / ]; then FlareJS - EOF -- 2.39.2