]> git.plutz.net Git - flarejs/blob - engine.js
code shortening
[flarejs] / engine.js
1 var gamedata = {
2   load: def => this[def] ? this[def] : this[def] = new Textfile(def)
3 }
4 var gfx = {
5   load: function(def) {
6     if (!this[def]) {
7       this[def] = document.createElement("img");
8       this[def].setAttribute("src", def);
9     }
10     return this[def];
11   }
12 };
13
14 function Textfile(txtfile) {
15   var lines, fetch = new XMLHttpRequest();
16   fetch.open("GET", txtfile, false);
17   fetch.send();
18
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);
26       break;
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));
32       break;
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;
36       break;
37     case /^data=$/.test(line):
38       ref.data = [];
39       break;
40     case /^[0-9,]+,$/.test(line):
41       value = line.split(/,/).slice(0,-1);
42       ref.data = ref.data.concat(value);
43       break;
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));
48       break;
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));
52       break;
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;
58       break;
59     case /^duration=[0-9]+ms$/.test(line):
60       ref["duration"] = line.split(/=|ms$/)[1];
61       break;
62     case /^duration=[0-9]+s$/.test(line):
63       ref["duration"] = line.split(/=|s$/)[1] * 1000;
64       break;
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);
70       break;
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);
76       break;
77   }
78 }
79
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);
85
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;
89
90   canvas.fillStyle = "rgba("+this.info.header.background_color+")";
91
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;
95     return ny * w + nx;
96   }
97
98   this.center = function(x, y) {
99     posx = canvas.canvas.width  / 2 - x;
100     posy = canvas.canvas.height / 2 - y;
101     return this;
102   }
103
104   this.draw = function(mobs) {
105     var x, y, dx, dy, i;
106     var bg = this.info.layer.find(l => l.type == "background").data;
107     var ob = this.info.layer.find(l => l.type == "object").data;
108     var mm = [];
109     mobs.forEach(m => {
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]));
112     });
113     canvas.fillRect(0,0, canvas.canvas.width, canvas.canvas.height);
114
115     for ( y = 0; y < h; y++ ) for ( x = 0; x < w; x++ ) {
116       i = y * h + 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));
122     }
123   }
124
125   this.draw_tile = function(tile, x, y) {
126     x = x |0; y = y |0;
127     var t = this.tileset.tile[tile];
128     var f = this.tileset.animation[tile];
129
130     if (t && f) {
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]);
134     } else if (t) {
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]);
137     }
138   }
139 }
140
141 function Mob(textdef) {
142   this.position = [0, 0];
143   var info = gamedata.load(textdef);
144   var direction = 0;
145   var animation = "stance";
146   var previous_animation = "";
147   var frametime = performance.now();
148   gfx.load(info.image)
149
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;
155       animation = a;
156       frametime = performance.now();
157     }
158     return this;
159   }
160
161   this.draw = function(x, y){
162     var f, a = info[animation];
163     var frame = ( performance.now() - frametime ) * a.frames / a.duration | 0;
164
165     switch(a.type){
166       case "looped":
167         frame = frame % a.frames;
168         break;
169       case "play_once":
170         if ( frame >= a.frames ){
171           animation = previous_animation;
172           previous_animation = "";
173           frametime = performance.now();
174           a = info[animation];
175           frame = 0;
176         }
177         break;
178       case "back_forth":
179         frame = frame % (a.frames * 2 - 2);
180         if ( frame >= a.frames ){
181          frame = a.frames - frame % a.frames - 1;
182         }
183         break;
184       default: break;
185     }
186     f = a.frame[frame][direction];
187
188     canvas.drawImage(gfx[info.image], f[0], f[1], f[2], f[3],
189                      x - f[4], y - f[5],
190                      f[2], f[3]);
191   }
192
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" );
201 }
202
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";
209
210   limbs = {
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")
218   }
219
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);
224     return this;
225   }
226
227   this.place   = function(x,y) {
228     this.position = [x,y];
229     for (var limb in limbs) limbs[limb].place(x,y);
230     return this;
231   }
232   this.direct  = function(d)   {
233     direction = d;
234     for (var limb in limbs) limbs[limb].direct(d);
235     return this;
236   }
237   this.animate = function(anim){
238     animation = anim;
239     for (var limb in limbs) limbs[limb].animate(anim);
240     return this;
241   }
242   this.draw    = function(x, y){
243     layers.layer[direction].forEach(limb => limbs[limb].draw(x, y));
244     return this;
245   }
246
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" );
255 }
256
257 function Controls(hero, map){
258   var keys = [];
259
260   function input(){
261     var d = {};
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();
271   }
272   window.addEventListener("keydown", function(e){ keys[e.keyCode] = true ; input();} );
273   window.addEventListener("keyup"  , function(e){ keys[e.keyCode] = false; input();} );
274 }
275
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);
280
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;
286 });
287
288 player = new Hero();
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);
294
295 setInterval (function() { map.center(player.position[0], player.position[1]).draw([player]);}, 33.33 );