2 load: def => this[def] ? this[def] : this[def] = new Textfile(def)
7 this[def] = document.createElement("img");
8 this[def].setAttribute("src", def);
14 function Textfile(txtfile) {
15 var lines, fetch = new XMLHttpRequest();
16 fetch.open("GET", txtfile, false);
19 var line, key, value, ref = this, section;
20 for (line of fetch.responseText.split('\n')) switch(true) {
21 case /^\[.*\]$/.test(line):
22 section = line.split(/[\]\[]/)[1]; ref = {};
23 if (!this[section]) this[section] = ref;
24 else if (!Array.isArray(this[section])) this[section] = [this[section],ref];
25 else if ( Array.isArray(this[section])) this[section].push(ref);
27 case /^frame=[0-9]+,[0-7](,[0-9-]+){6}$/.test(line):
28 key = line.split(/[=,]/).slice(1,3); value = line.split(/,/).slice(-6);
29 if (!ref.frame) ref.frame = [];
30 if (!ref.frame[key[0]]) ref.frame[key[0]] = [];
31 ref.frame[key[0]][key[1]] = value.map(x => parseInt(x));
33 case /^layer=[0-9]+,/.test(line):
34 key = line.split(/[=,]/)[1]; value = line.split(/,/).slice(1);
35 if (!ref.layer) ref.layer = []; ref.layer[key] = value;
37 case /^data=$/.test(line):
40 case /^[0-9,]+,$/.test(line):
41 value = line.split(/,/).slice(0,-1);
42 ref.data = ref.data.concat(value);
44 case /^[0-9,]+$/.test(line):
45 value = line.split(/,/);
46 ref.data = ref.data.concat(value);
47 ref.data = ref.data.map(x => parseInt(x));
49 case /^tile=[0-9]+,/.test(line):
50 key = line.split(/[=,]/)[1]; value = line.split(/,/).slice(1);
51 if (!ref.tile) ref.tile = []; ref.tile[key] = value.map(x => parseInt(x));
53 case /^animation=[0-9]+;.*;$/.test(line):
54 key = line.split(/[=;]/)[1]; value = line.split(/;/).slice(1,-1);
55 value = value.map(x => x.split(/,/));
56 value.forEach(x => x[2] = parseInt(x[2]));
57 if (!ref.animation) ref.animation = []; ref.animation[key] = value;
59 case /^duration=[0-9]+ms$/.test(line):
60 ref["duration"] = line.split(/=|ms$/)[1];
62 case /^duration=[0-9]+s$/.test(line):
63 ref["duration"] = line.split(/=|s$/)[1] * 1000;
65 case /^[^#].*=[0-9]+$/.test(line):
66 key = line.split(/[=]/)[0]; value = line.split(/=/).slice(1) * 1;
67 if (!ref[key]) ref[key] = value;
68 else if (!Array.isArray(ref[key])) ref[key] = [ref[key],value];
69 else if ( Array.isArray(ref[key])) ref[key].push(value);
71 case /^[^#].*=.+$/.test(line):
72 key = line.split(/[=]/)[0]; value = line.split(/=/).slice(1).join("=");
73 if (!ref[key]) ref[key] = value;
74 else if (!Array.isArray(ref[key])) ref[key] = [ref[key],value];
75 else if ( Array.isArray(ref[key])) ref[key].push(value);
80 function Map(textdef) {
81 this.frametime = performance.now();
82 this.info = gamedata.load(textdef);
83 this.tileset = gamedata.load(this.info.header.tileset);
84 gfx.load(this.tileset.img);
86 var h = this.info.header.height, w = this.info.header.width;
87 var th = this.info.header.tileheight, tw = this.info.header.tilewidth;
88 var posx = canvas.canvas.width / 2, posy = canvas.canvas.height / 2 - h * th/2;
90 canvas.fillStyle = "rgba("+this.info.header.background_color+")";
92 this.tileAt = function(x, y) {
93 nx = (y + th / 2) / th + (x - w * tw / 2) / tw |0;
94 ny = (y + th / 2) / th - (x - w * tw / 2) / tw |0;
98 this.center = function(x, y) {
99 posx = canvas.canvas.width / 2 - x;
100 posy = canvas.canvas.height / 2 - y;
104 this.draw = function(mobs) {
106 var bg = this.info.layer.find(l => l.type == "background").data;
107 var ob = this.info.layer.find(l => l.type == "object").data;
110 i = this.tileAt(m.position[0], m.position[1]);
111 mm[i] = mobs.filter(m => i == this.tileAt(m.position[0], m.position[1]));
113 canvas.fillRect(0,0, canvas.canvas.width, canvas.canvas.height);
115 for ( y = 0; y < h; y++ ) for ( x = 0; x < w; x++ ) {
117 dx = posx + (w + x - y) * tw /2;
118 dy = posy + (x + y) * th / 2;
119 this.draw_tile(bg[i], dx, dy);
120 this.draw_tile(ob[i], dx, dy);
121 if (mm[i]) mm[i].forEach(m => m.draw(dx, dy));
125 this.draw_tile = function(tile, x, y) {
127 var t = this.tileset.tile[tile];
128 var f = this.tileset.animation[tile];
131 frame = ((performance.now() - this.frametime) / f[0][2] |0) % f.length;
132 canvas.drawImage(gfx[this.tileset.img], f[frame][0], f[frame][1], t[2], t[3],
133 x - t[4], y - t[5], t[2], t[3]);
135 canvas.drawImage(gfx[this.tileset.img], t[0], t[1], t[2], t[3],
136 x - t[4], y - t[5], t[2], t[3]);
141 function Mob(textdef) {
142 this.position = [0, 0];
143 var info = gamedata.load(textdef);
145 var animation = "stance";
146 var previous_animation = "";
147 var frametime = performance.now();
150 this.place = function(x, y) { this.position = [x, y]; return this; }
151 this.direct = function(d) { direction = d % 8; return this; }
152 this.animate = function(a) {
153 if ( a != animation ) {
154 previous_animation = animation;
156 frametime = performance.now();
161 this.draw = function(x, y){
162 var f, a = info[animation];
163 var frame = ( performance.now() - frametime ) * a.frames / a.duration | 0;
167 frame = frame % a.frames;
170 if ( frame >= a.frames ){
171 animation = previous_animation;
172 previous_animation = "";
173 frametime = performance.now();
179 frame = frame % (a.frames * 2 - 2);
180 if ( frame >= a.frames ){
181 frame = a.frames - frame % a.frames - 1;
186 f = a.frame[frame][direction];
188 canvas.drawImage(gfx[info.image], f[0], f[1], f[2], f[3],
193 this.block = () => this.animate("block" );
194 this.cast = () => this.animate("cast" );
195 this.die = () => this.animate("die" );
196 this.hit = () => this.animate("hit" );
197 this.run = () => this.animate("run" );
198 this.shoot = () => this.animate("shoot" );
199 this.stance = () => this.animate("stance");
200 this.swing = () => this.animate("swing" );
203 function Hero(gender = "female", hair = "short"){
204 this.position = [0,0];
205 this.stats = gamedata.load("/engine/stats.txt");
206 var layers = gamedata.load("/engine/hero_layers.txt");
207 var hair = (gender == "female")?"long":hair;
208 var direction = 0, animation = "stance";
211 head : new Mob("/animations/avatar/"+gender+"/head_"+hair+".txt"),
212 chest: new Mob("/animations/avatar/"+gender+"/default_chest.txt"),
213 hands: new Mob("/animations/avatar/"+gender+"/default_hands.txt"),
214 legs : new Mob("/animations/avatar/"+gender+"/default_legs.txt"),
215 feet : new Mob("/animations/avatar/"+gender+"/default_feet.txt"),
216 main : new Mob("/animations/avatar/"+gender+"/dagger.txt"),
217 off : new Mob("/animations/avatar/"+gender+"/shield.txt")
220 this.dress = function(limb, item) {
221 limbs[limb] = new Mob("/animations/avatar/"+gender+"/"+item+".txt")
222 limbs[limb].place(this.position[0], this.position[1]).direct(direction);
223 this.animate(animation);
227 this.place = function(x,y) {
228 this.position = [x,y];
229 for (var limb in limbs) limbs[limb].place(x,y);
232 this.direct = function(d) {
234 for (var limb in limbs) limbs[limb].direct(d);
237 this.animate = function(anim){
239 for (var limb in limbs) limbs[limb].animate(anim);
242 this.draw = function(x, y){
243 layers.layer[direction].forEach(limb => limbs[limb].draw(x, y));
247 this.block = () => this.animate("block" );
248 this.cast = () => this.animate("cast" );
249 this.die = () => this.animate("die" );
250 this.hit = () => this.animate("hit" );
251 this.run = () => this.animate("run" );
252 this.shoot = () => this.animate("shoot" );
253 this.stance = () => this.animate("stance");
254 this.swing = () => this.animate("swing" );
257 function Controls(hero, map){
262 d.l = keys[65] || keys[37]; d.r = keys[68] || keys[39];
263 d.u = keys[87] || keys[38]; d.d = keys[83] || keys[40];
264 if ( d.l ) hero.direct(0).run(); if ( d.r ) hero.direct(4).run();
265 if ( d.u ) hero.direct(2).run(); if ( d.d ) hero.direct(6).run();
266 if ( d.u && d.l ) hero.direct(1).run();
267 if ( d.u && d.r ) hero.direct(3).run();
268 if ( d.d && d.l ) hero.direct(7).run();
269 if ( d.d && d.r ) hero.direct(5).run();
270 if ( ! (d.l || d.r || d.u || d.d) ) hero.stance();
272 window.addEventListener("keydown", function(e){ keys[e.keyCode] = true ; input();} );
273 window.addEventListener("keyup" , function(e){ keys[e.keyCode] = false; input();} );
276 document.querySelector("body").setAttribute("style", "margin: 0; padding: 0;");
277 canvas = document.createElement("canvas").getContext("2d");
278 canvas.canvas.setAttribute("style", "display: block; border: 0; margin: 0; padding: 0;");
279 document.querySelector("body").appendChild(canvas.canvas);
281 canvas.canvas.width = window.innerWidth;
282 canvas.canvas.height = window.innerHeight;
283 window.addEventListener("resize", function(){
284 canvas.canvas.width = window.innerWidth;
285 canvas.canvas.height = window.innerHeight;
289 map = new Map("/maps/arrival.txt");
290 player.place(map.info.header.hero_pos.split(/,/)[0] * map.info.header.tilewidth,
291 map.info.header.hero_pos.split(/,/)[1] * map.info.header.tileheight);
292 player.direct(5).stance();
293 c = new Controls(player, map);
295 setInterval (function() { map.center(player.position[0], player.position[1]).draw([player]);}, 33.33 );