+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; }
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();
}
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"),
}
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;
}
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);