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 this.previous_animation = this.animation;
153 this.frametime = performance.now();
157 this.draw = function(x, y){
158 var f, a = this.info[this.animation];
159 var frame = ( performance.now() - this.frametime ) * a.frames / a.duration | 0;
163 frame = frame % a.frames;
166 if ( frame >= a.frames ){
167 this.animation = this.previous_animation;
168 this.previous_animation = "";
169 this.frametime = performance.now();
170 a = this.info[this.animation];
175 frame = frame % (a.frames * 2 - 2);
176 if ( frame >= a.frames ){
177 frame = a.frames - frame % a.frames - 1;
182 f = a.frame[frame][this.direction];
184 canvas.drawImage(gfx[this.info.image], f[0], f[1], f[2], f[3],
189 this.block = function() { this.animate("block" ); return this; };
190 this.cast = function() { this.animate("cast" ); return this; };
191 this.die = function() { this.animate("die" ); return this; };
192 this.hit = function() { this.animate("hit" ); return this; };
193 this.run = function() { this.animate("run" ); return this; };
194 this.shoot = function() { this.animate("shoot" ); return this; };
195 this.stance = function() { this.animate("stance"); return this; };
196 this.swing = function() { this.animate("swing" ); return this; };
199 function Hero(gender = "female", hair = "short"){
200 this.position = [0,0]; this.direction = 0;
201 this.hair = (gender == "female")?"long":hair;
202 this.animation = "stance";
204 if (! gamedata["/engine/hero_layers.txt"])
205 gamedata["/engine/hero_layers.txt"] = new Textfile("/engine/hero_layers.txt");
207 head: new Mob("/animations/avatar/"+gender+"/head_"+this.hair+".txt"),
208 chest: new Mob("/animations/avatar/"+gender+"/default_chest.txt"),
209 hands: new Mob("/animations/avatar/"+gender+"/default_hands.txt"),
210 legs : new Mob("/animations/avatar/"+gender+"/default_legs.txt"),
211 feet : new Mob("/animations/avatar/"+gender+"/default_feet.txt"),
212 main : new Mob("/animations/avatar/"+gender+"/dagger.txt"),
213 off : new Mob("/animations/avatar/"+gender+"/shield.txt")
216 this.dress = function(limb, item) {
217 this.limbs[limb] = new Mob("/animations/avatar/"+gender+"/"+item+".txt")
218 this.limbs[limb].place(this.position[0], this.position[1]).direct(this.direction);
219 this.animate(this.animation);
223 this.place = function(x,y) {
224 this.position = [x,y];
225 for (var limb in this.limbs) this.limbs[limb].place(x,y);
228 this.direct = function(d) {
230 for (var limb in this.limbs) this.limbs[limb].direct(d);
233 this.animate = function(anim){
234 this.animation = anim;
235 for (var limb in this.limbs) this.limbs[limb].animate(anim);
238 this.draw = function(x, y){
239 gamedata["/engine/hero_layers.txt"].layer[this.direction].forEach(limb => this.limbs[limb].draw(x, y));
243 this.block = function() { this.animate("block" ); return this; };
244 this.cast = function() { this.animate("cast" ); return this; };
245 this.die = function() { this.animate("die" ); return this; };
246 this.hit = function() { this.animate("hit" ); return this; };
247 this.run = function() { this.animate("run" ); return this; };
248 this.shoot = function() { this.animate("shoot" ); return this; };
249 this.stance = function() { this.animate("stance"); return this; };
250 this.swing = function() { this.animate("swing" ); return this; };
253 document.querySelector("body").setAttribute("style", "margin: 0; padding: 0;");
254 canvas = document.createElement("canvas").getContext("2d");
255 canvas.canvas.setAttribute("style", "display: block; border: 0; margin: 0; padding: 0;");
256 document.querySelector("body").appendChild(canvas.canvas);
258 canvas.canvas.width = window.innerWidth;
259 canvas.canvas.height = window.innerHeight;
260 window.addEventListener("resize", function(){
261 canvas.canvas.width = window.innerWidth;
262 canvas.canvas.height = window.innerHeight;
265 player = new Hero().place(20 * 64 , 20 * 32).direct(5);
266 map = new Map("/maps/arrival.txt");
268 setInterval (function() { map.center(player.position[0], player.position[1]).draw([player]);}, 33.33 );
270 // window.addEventListener("click", function(e){
271 // tile = map.tileAt(e.pageX + map.posx, e.pageY - map.posy );
272 // console.log( "X: " + (e.pageX + map.posx) + ", Y:" + (e.pageY - map.posy) +
273 // ", Tile: " + tile + " ( " + (tile % map.info.header.width) + ", " + (tile / map.info.header.width |0) + " )" );
277 // window.addEventListener("mousemove", function(e){
278 // tile = map.tileAt(e.pageX + map.posx, e.pageY - map.posy);
279 // mark = [(tile % map.info.header.width), (tile / map.info.header.width |0)];