]> git.plutz.net Git - flarejs/blob - engine.js
map loading and drawing, adapt canvas size to window
[flarejs] / engine.js
1 var gamedata = {};
2 var gfx = {};
3
4 function Textfile(txtfile) {
5   var lines, fetch = new XMLHttpRequest();
6   fetch.open("GET", txtfile, false);
7   fetch.send();
8
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);
16       break;
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));
22       break;
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;
26       break;
27     case /^data=$/.test(line):
28       ref.data = [];
29       break;
30     case /^[0-9,]+,$/.test(line):
31       value = line.split(/,/).slice(0,-1);
32       ref.data = ref.data.concat(value);
33       break;
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));
38       break;
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));
42       break;
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;
48       break;
49     case /^duration=[0-9]+ms$/.test(line):
50       ref["duration"] = line.split(/=|ms$/)[1];
51       break;
52     case /^duration=[0-9]+s$/.test(line):
53       ref["duration"] = line.split(/=|s$/)[1] * 1000;
54       break;
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);
60       break;
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);
66       break;
67   }
68 }
69
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);
79   }
80
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;
85   }
86
87   this.draw = function(posx, posy, mobs) {
88     var x, y, i, layer;
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;
93
94     posx = canvas.canvas.width  / 2 - posx + w * tw / 2;
95     posy = canvas.canvas.height / 2 - posy;
96
97     canvas.fillStyle = "rgba("+this.info.header.background_color+")";
98     canvas.fillRect(0,0, canvas.canvas.width, canvas.canvas.height);
99
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++ ){
103         i = (y - x) * w + x;
104         if (layer[i]) this.draw_tile( layer[i],
105                                           posx - (y / 2 - x) * tw,
106                                           posy + y / 2 * th);
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,
111                                           posy + y / 2 * th);
112       }
113     })
114   }
115
116   this.draw_tile = function(tile, x, y) {
117     t = this.tileset.tile[tile], x = x|0, y = y|0;
118
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]);
124     } else {
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]);
127     }
128   }
129 }
130
131 function Mob(textdef) {
132   if (! gamedata[textdef]) gamedata[textdef] = new Textfile(textdef); 
133   this.info = gamedata[textdef];
134   this.direction = 0;
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);
142   }
143
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;
148     this.animation = a;
149     this.frametime = performance.now();
150     return this;
151   }
152
153   this.draw = function(){
154     var f, a = this.info[this.animation];
155     var frame = ( performance.now() - this.frametime ) * a.frames / a.duration | 0;
156
157     switch(a.type){
158       case "looped":
159         frame = frame % a.frames;
160         break;
161       case "play_once":
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];
167           frame = 0;
168         }
169         break;
170       case "back_forth":
171         frame = frame % (a.frames * 2 - 2);
172         if ( frame >= a.frames ){
173          frame = a.frames - frame % a.frames - 1;
174         }
175         break;
176       default: break;
177     }
178     f = a.frame[frame][this.direction];
179
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],
182                      f[2], f[3]);
183   }
184
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; };
193 }
194
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";
199
200   if (! gamedata["/engine/hero_layers.txt"])
201     gamedata["/engine/hero_layers.txt"] = new Textfile("/engine/hero_layers.txt"); 
202   this.limbs = {
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")
210   }
211
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);
216     return this;
217   }
218
219   this.place   = function(x,y) {
220     this.position = [x,y];
221     for (var limb in this.limbs) this.limbs[limb].place(x,y);
222     return this;
223   }
224   this.direct  = function(d)   {
225     this.direction = d;
226     for (var limb in this.limbs) this.limbs[limb].direct(d);
227     return this;
228   }
229   this.animate = function(anim){
230     this.animation = anim;
231     for (var limb in this.limbs) this.limbs[limb].animate(anim);
232     return this;
233   }
234   this.draw    = function(){
235     gamedata["/engine/hero_layers.txt"].layer[this.direction].forEach(limb => this.limbs[limb].draw());
236     return this;
237   }
238
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; };
247 }
248
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);
253
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;
259 });
260
261 player = new Hero().place(20 * 64 , 20 * 32).direct(5);
262 map = new Map("/maps/arrival.txt");
263
264 setInterval (function() { map.draw(player.position[0], player.position[1], [player]) }, 33.33 );
265