4 function Textfile(txtfile) {
5 var lines, fetch = new XMLHttpRequest();
6 fetch.open("GET", txtfile, false);
9 var line, key, value, ref = this, section;
10 for (line of fetch.responseText.split('\n')) switch(true) {
11 case /^\[.*\]$/.test(line):
12 section = line.split(/[\]\[]/)[1]; ref = {};
13 if (!this[section]) this[section] = ref;
14 else if (!Array.isArray(this[section])) this[section] = [this[section],ref];
15 else if ( Array.isArray(this[section])) this[section].push(ref);
17 case /^frame=[0-9]+,[0-7](,[0-9-]+){6}$/.test(line):
18 key = line.split(/[=,]/).slice(1,3); value = line.split(/,/).slice(-6);
19 if (!ref.frame) ref.frame = [];
20 if (!ref.frame[key[0]]) ref.frame[key[0]] = [];
21 ref.frame[key[0]][key[1]] = value.map(x => parseInt(x));
23 case /^layer=[0-9]+,/.test(line):
24 key = line.split(/[=,]/)[1]; value = line.split(/,/).slice(1);
25 if (!ref.layer) ref.layer = []; ref.layer[key] = value;
27 case /^data=$/.test(line):
30 case /^[0-9,]+,$/.test(line):
31 value = line.split(/,/).slice(0,-1);
32 ref.data = ref.data.concat(value);
34 case /^[0-9,]+$/.test(line):
35 value = line.split(/,/);
36 ref.data = ref.data.concat(value);
37 ref.data = ref.data.map(x => parseInt(x));
39 case /^tile=[0-9]+,/.test(line):
40 key = line.split(/[=,]/)[1]; value = line.split(/,/).slice(1);
41 if (!ref.tile) ref.tile = []; ref.tile[key] = value.map(x => parseInt(x));
43 case /^animation=[0-9]+;.*;$/.test(line):
44 key = line.split(/[=;]/)[1]; value = line.split(/;/).slice(1,-1);
45 value = value.map(x => x.split(/,/));
46 value.forEach(x => x[2] = parseInt(x[2]));
47 if (!ref.animation) ref.animation = []; ref.animation[key] = value;
49 case /^duration=[0-9]+ms$/.test(line):
50 ref["duration"] = line.split(/=|ms$/)[1];
52 case /^duration=[0-9]+s$/.test(line):
53 ref["duration"] = line.split(/=|s$/)[1] * 1000;
55 case /^[^#].*=[0-9]+$/.test(line):
56 key = line.split(/[=]/)[0]; value = line.split(/=/).slice(1) * 1;
57 if (!ref[key]) ref[key] = value;
58 else if (!Array.isArray(ref[key])) ref[key] = [ref[key],value];
59 else if ( Array.isArray(ref[key])) ref[key].push(value);
61 case /^[^#].*=.+$/.test(line):
62 key = line.split(/[=]/)[0]; value = line.split(/=/).slice(1).join("=");
63 if (!ref[key]) ref[key] = value;
64 else if (!Array.isArray(ref[key])) ref[key] = [ref[key],value];
65 else if ( Array.isArray(ref[key])) ref[key].push(value);
70 function Map(textdef) {
71 this.frametime = performance.now();
72 if (gamedata[textdef]) this.info = gamedata[textdef];
73 else gamedata[textdef] = this.info = new Textfile(textdef);
74 if (gamedata[this.info.header.tileset]) this.tileset = gamedata[this.info.header.tileset];
75 else gamedata[this.info.header.tileset] = this.tileset = new Textfile(this.info.header.tileset);
76 if (! gfx[this.tileset.img]) {
77 gfx[this.tileset.img] = document.createElement("img");
78 gfx[this.tileset.img].setAttribute("src", this.tileset.img);
81 var h = this.info.header.height, w = this.info.header.width;
82 var th = this.info.header.tileheight, tw = this.info.header.tilewidth;
83 var posx = canvas.canvas.width / 2, posy = canvas.canvas.height / 2 - h * th/2;
85 canvas.fillStyle = "rgba("+this.info.header.background_color+")";
87 this.tileAt = function(x, y) {
88 nx = (y + th / 2) / th + (x - w * tw / 2) / tw |0;
89 ny = (y + th / 2) / th - (x - w * tw / 2) / tw |0;
93 this.center = function(x, y) {
94 posx = canvas.canvas.width / 2 - x;
95 posy = canvas.canvas.height / 2 - y;
99 this.draw = function(mobs) {
101 var bg = this.info.layer.find(l => l.type == "background").data;
102 var ob = this.info.layer.find(l => l.type == "object").data;
105 i = this.tileAt(m.position[0], m.position[1]);
106 mm[i] = mobs.filter(m => i == this.tileAt(m.position[0], m.position[1]));
108 canvas.fillRect(0,0, canvas.canvas.width, canvas.canvas.height);
110 for ( y = 0; y < h; y++ ) for ( x = 0; x < w; x++ ) {
112 dx = posx + (w + x - y) * tw /2;
113 dy = posy + (x + y) * th / 2;
114 this.draw_tile(bg[i], dx, dy);
115 this.draw_tile(ob[i], dx, dy);
116 if (mm[i]) mm[i].forEach(m => m.draw(dx, dy));
120 this.draw_tile = function(tile, x, y) {
121 var t = this.tileset.tile[tile];
122 var f = this.tileset.animation[tile];
125 frame = ((performance.now() - this.frametime) / f[0][2] |0) % f.length;
126 canvas.drawImage(gfx[this.tileset.img], f[frame][0], f[frame][1], t[2], t[3],
127 x - t[4], y - t[5], t[2], t[3]);
129 canvas.drawImage(gfx[this.tileset.img], t[0], t[1], t[2], t[3],
130 x - t[4], y - t[5], t[2], t[3]);
135 function Mob(textdef) {
136 if (! gamedata[textdef]) gamedata[textdef] = new Textfile(textdef);
137 this.info = gamedata[textdef];
139 this.position = [0, 0];
140 this.animation = "stance";
141 this.previous_animation = "";
142 this.frametime = performance.now();
143 if (! gfx[this.info.image]) {
144 gfx[this.info.image] = document.createElement("img");
145 gfx[this.info.image].setAttribute("src", this.info.image);
148 this.place = function(x, y) { this.position = [x, y]; return this; }
149 this.direct = function(d) { this.direction = d % 8; return this; }
150 this.animate = function(a) {
151 if ( a != this.animation ) {
152 this.previous_animation = this.animation;
154 this.frametime = performance.now();
159 this.draw = function(x, y){
160 var f, a = this.info[this.animation];
161 var frame = ( performance.now() - this.frametime ) * a.frames / a.duration | 0;
165 frame = frame % a.frames;
168 if ( frame >= a.frames ){
169 this.animation = this.previous_animation;
170 this.previous_animation = "";
171 this.frametime = performance.now();
172 a = this.info[this.animation];
177 frame = frame % (a.frames * 2 - 2);
178 if ( frame >= a.frames ){
179 frame = a.frames - frame % a.frames - 1;
184 f = a.frame[frame][this.direction];
186 canvas.drawImage(gfx[this.info.image], f[0], f[1], f[2], f[3],
191 this.block = function() { this.animate("block" ); return this; };
192 this.cast = function() { this.animate("cast" ); return this; };
193 this.die = function() { this.animate("die" ); return this; };
194 this.hit = function() { this.animate("hit" ); return this; };
195 this.run = function() { this.animate("run" ); return this; };
196 this.shoot = function() { this.animate("shoot" ); return this; };
197 this.stance = function() { this.animate("stance"); return this; };
198 this.swing = function() { this.animate("swing" ); return this; };
201 function Hero(gender = "female", hair = "short"){
202 this.position = [0,0]; this.direction = 0;
203 this.hair = (gender == "female")?"long":hair;
204 this.animation = "stance";
206 if (! gamedata["/engine/hero_layers.txt"])
207 gamedata["/engine/hero_layers.txt"] = new Textfile("/engine/hero_layers.txt");
209 head: new Mob("/animations/avatar/"+gender+"/head_"+this.hair+".txt"),
210 chest: new Mob("/animations/avatar/"+gender+"/default_chest.txt"),
211 hands: new Mob("/animations/avatar/"+gender+"/default_hands.txt"),
212 legs : new Mob("/animations/avatar/"+gender+"/default_legs.txt"),
213 feet : new Mob("/animations/avatar/"+gender+"/default_feet.txt"),
214 main : new Mob("/animations/avatar/"+gender+"/dagger.txt"),
215 off : new Mob("/animations/avatar/"+gender+"/shield.txt")
218 this.dress = function(limb, item) {
219 this.limbs[limb] = new Mob("/animations/avatar/"+gender+"/"+item+".txt")
220 this.limbs[limb].place(this.position[0], this.position[1]).direct(this.direction);
221 this.animate(this.animation);
225 this.place = function(x,y) {
226 this.position = [x,y];
227 for (var limb in this.limbs) this.limbs[limb].place(x,y);
230 this.direct = function(d) {
232 for (var limb in this.limbs) this.limbs[limb].direct(d);
235 this.animate = function(anim){
236 this.animation = anim;
237 for (var limb in this.limbs) this.limbs[limb].animate(anim);
240 this.draw = function(x, y){
241 gamedata["/engine/hero_layers.txt"].layer[this.direction].forEach(limb => this.limbs[limb].draw(x, y));
245 this.block = function() { this.animate("block" ); return this; };
246 this.cast = function() { this.animate("cast" ); return this; };
247 this.die = function() { this.animate("die" ); return this; };
248 this.hit = function() { this.animate("hit" ); return this; };
249 this.run = function() { this.animate("run" ); return this; };
250 this.shoot = function() { this.animate("shoot" ); return this; };
251 this.stance = function() { this.animate("stance"); return this; };
252 this.swing = function() { this.animate("swing" ); return this; };
255 function Controls(hero, map){
260 d.l = keys[65] || keys[37]; d.r = keys[68] || keys[39];
261 d.u = keys[87] || keys[38]; d.d = keys[83] || keys[40];
262 if ( d.l ) hero.direct(0).run(); if ( d.r ) hero.direct(4).run();
263 if ( d.u ) hero.direct(2).run(); if ( d.d ) hero.direct(6).run();
264 if ( d.u && d.l ) hero.direct(1).run();
265 if ( d.u && d.r ) hero.direct(3).run();
266 if ( d.d && d.l ) hero.direct(7).run();
267 if ( d.d && d.r ) hero.direct(5).run();
268 if ( ! (d.l || d.r || d.u || d.d) ) hero.stance();
270 window.addEventListener("keydown", function(e){ keys[e.keyCode] = true ; input();} );
271 window.addEventListener("keyup" , function(e){ keys[e.keyCode] = false; input();} );
274 document.querySelector("body").setAttribute("style", "margin: 0; padding: 0;");
275 canvas = document.createElement("canvas").getContext("2d");
276 canvas.canvas.setAttribute("style", "display: block; border: 0; margin: 0; padding: 0;");
277 document.querySelector("body").appendChild(canvas.canvas);
279 canvas.canvas.width = window.innerWidth;
280 canvas.canvas.height = window.innerHeight;
281 window.addEventListener("resize", function(){
282 canvas.canvas.width = window.innerWidth;
283 canvas.canvas.height = window.innerHeight;
287 map = new Map("/maps/arrival.txt");
288 player.place(map.info.header.hero_pos.split(/,/)[0] * map.info.header.tilewidth,
289 map.info.header.hero_pos.split(/,/)[1] * map.info.header.tileheight);
290 player.direct(5).stance();
291 c = new Controls(player, map);
293 setInterval (function() { map.center(player.position[0], player.position[1]).draw([player]);}, 33.33 );