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 this.tileAt = function(x, y) {
82 nx = y / this.info.header.tileheight + x / this.info.header.tilewidth |0;
83 ny = y / this.info.header.tileheight - x / this.info.header.tilewidth |0;
84 return ny * this.info.header.width + nx;
87 this.draw = function(posx, posy, mobs) {
89 var h = this.info.header.height;
90 var w = this.info.header.width;
91 var th = this.info.header.tileheight;
92 var tw = this.info.header.tilewidth;
94 posx = canvas.canvas.width / 2 - posx + w * tw / 2;
95 posy = canvas.canvas.height / 2 - posy;
97 canvas.fillStyle = "rgba("+this.info.header.background_color+")";
98 canvas.fillRect(0,0, canvas.canvas.width, canvas.canvas.height);
100 [ this.info.layer[0].data, this.info.layer[1].data ].forEach(layer => {
101 for (y = 0; y < 2 * h - 1; y++ )
102 if ( y < h ) for ( x = 0; x <= y; x++ ){
104 if (layer[i]) this.draw_tile( layer[i],
105 posx - (y / 2 - x) * tw,
107 } else for ( x = 0; x < 2 * h - y - 1; x++ ){
108 i = (w - x - 2) * h + x + y + 1;
109 if (layer[i]) this.draw_tile( layer[i],
110 posx - (h - 1 - x - y / 2) * tw,
116 this.draw_tile = function(tile, x, y) {
117 t = this.tileset.tile[tile], x = x|0, y = y|0;
119 if (this.tileset.animation[tile]) {
120 f = this.tileset.animation[tile];
121 frame = ((performance.now() - this.frametime) / f[0][2] |0) % f.length;
122 canvas.drawImage(gfx[this.tileset.img], f[frame][0], f[frame][1], t[2], t[3],
123 x - t[4], y - t[5], t[2], t[3]);
125 canvas.drawImage(gfx[this.tileset.img], t[0], t[1], t[2], t[3],
126 x - t[4], y - t[5], t[2], t[3]);
131 function Mob(textdef) {
132 if (! gamedata[textdef]) gamedata[textdef] = new Textfile(textdef);
133 this.info = gamedata[textdef];
135 this.position = [0, 0];
136 this.animation = "stance";
137 this.previous_animation = "";
138 this.frametime = performance.now();
139 if (! gfx[this.info.image]) {
140 gfx[this.info.image] = document.createElement("img");
141 gfx[this.info.image].setAttribute("src", this.info.image);
144 this.place = function(x, y) { this.position = [x, y]; return this; }
145 this.direct = function(d) { this.direction = d % 8; return this; }
146 this.animate = function(a) {
147 this.previous_animation = this.animation;
149 this.frametime = performance.now();
153 this.draw = function(){
154 var f, a = this.info[this.animation];
155 var frame = ( performance.now() - this.frametime ) * a.frames / a.duration | 0;
159 frame = frame % a.frames;
162 if ( frame >= a.frames ){
163 this.animation = this.previous_animation;
164 this.previous_animation = "";
165 this.frametime = performance.now();
166 a = this.info[this.animation];
171 frame = frame % (a.frames * 2 - 2);
172 if ( frame >= a.frames ){
173 frame = a.frames - frame % a.frames - 1;
178 f = a.frame[frame][this.direction];
180 canvas.drawImage(gfx[this.info.image], f[0], f[1], f[2], f[3],
181 this.position[0] - f[4], this.position[1] - f[5],
185 this.block = function() { this.animate("block" ); return this; };
186 this.cast = function() { this.animate("cast" ); return this; };
187 this.die = function() { this.animate("die" ); return this; };
188 this.hit = function() { this.animate("hit" ); return this; };
189 this.run = function() { this.animate("run" ); return this; };
190 this.shoot = function() { this.animate("shoot" ); return this; };
191 this.stance = function() { this.animate("stance"); return this; };
192 this.swing = function() { this.animate("swing" ); return this; };
195 function Hero(gender = "female", hair = "short"){
196 this.position = [0,0]; this.direction = 0;
197 this.hair = (gender == "female")?"long":hair;
198 this.animation = "stance";
200 if (! gamedata["/engine/hero_layers.txt"])
201 gamedata["/engine/hero_layers.txt"] = new Textfile("/engine/hero_layers.txt");
203 head: new Mob("/animations/avatar/"+gender+"/head_"+this.hair+".txt"),
204 chest: new Mob("/animations/avatar/"+gender+"/default_chest.txt"),
205 hands: new Mob("/animations/avatar/"+gender+"/default_hands.txt"),
206 legs : new Mob("/animations/avatar/"+gender+"/default_legs.txt"),
207 feet : new Mob("/animations/avatar/"+gender+"/default_feet.txt"),
208 main : new Mob("/animations/avatar/"+gender+"/dagger.txt"),
209 off : new Mob("/animations/avatar/"+gender+"/shield.txt")
212 this.dress = function(limb, item) {
213 this.limbs[limb] = new Mob("/animations/avatar/"+gender+"/"+item+".txt")
214 this.limbs[limb].place(this.position[0], this.position[1]).direct(this.direction);
215 this.animate(this.animation);
219 this.place = function(x,y) {
220 this.position = [x,y];
221 for (var limb in this.limbs) this.limbs[limb].place(x,y);
224 this.direct = function(d) {
226 for (var limb in this.limbs) this.limbs[limb].direct(d);
229 this.animate = function(anim){
230 this.animation = anim;
231 for (var limb in this.limbs) this.limbs[limb].animate(anim);
234 this.draw = function(){
235 gamedata["/engine/hero_layers.txt"].layer[this.direction].forEach(limb => this.limbs[limb].draw());
239 this.block = function() { this.animate("block" ); return this; };
240 this.cast = function() { this.animate("cast" ); return this; };
241 this.die = function() { this.animate("die" ); return this; };
242 this.hit = function() { this.animate("hit" ); return this; };
243 this.run = function() { this.animate("run" ); return this; };
244 this.shoot = function() { this.animate("shoot" ); return this; };
245 this.stance = function() { this.animate("stance"); return this; };
246 this.swing = function() { this.animate("swing" ); return this; };
249 document.querySelector("body").setAttribute("style", "margin: 0; padding: 0;");
250 canvas = document.createElement("canvas").getContext("2d");
251 canvas.canvas.setAttribute("style", "display: block; border: 0; margin: 0; padding: 0;");
252 document.querySelector("body").appendChild(canvas.canvas);
254 canvas.canvas.width = window.innerWidth;
255 canvas.canvas.height = window.innerHeight;
256 window.addEventListener("resize", function(){
257 canvas.canvas.width = window.innerWidth;
258 canvas.canvas.height = window.innerHeight;
261 player = new Hero().place(20 * 64 , 20 * 32).direct(5);
262 map = new Map("/maps/arrival.txt");
264 setInterval (function() { map.draw(player.position[0], player.position[1], [player]) }, 33.33 );