]> git.plutz.net Git - flarejs/blob - engine.js
improved map drawing, draw mobs
[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   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;
84
85   canvas.fillStyle = "rgba("+this.info.header.background_color+")";
86
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;
90     return ny * w + nx;
91   }
92
93   this.center = function(x, y) {
94     posx = canvas.canvas.width  / 2 - x;
95     posy = canvas.canvas.height / 2 - y;
96     return this;
97   }
98
99   this.draw = function(mobs) {
100     var x, y, dx, dy, i;
101     var bg = this.info.layer.find(l => l.type == "background").data;
102     var ob = this.info.layer.find(l => l.type == "object").data;
103     var mm = [];
104     mobs.forEach(m => {
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]));
107     });
108     canvas.fillRect(0,0, canvas.canvas.width, canvas.canvas.height);
109
110     for ( y = 0; y < h; y++ ) for ( x = 0; x < w; x++ ) {
111       i = y * h + 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));
117     }
118   }
119
120   this.draw_tile = function(tile, x, y) {
121     var t = this.tileset.tile[tile];
122     var f = this.tileset.animation[tile];
123
124     if (t && f) {
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]);
128     } else if (t) {
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]);
131     }
132   }
133 }
134
135 function Mob(textdef) {
136   if (! gamedata[textdef]) gamedata[textdef] = new Textfile(textdef); 
137   this.info = gamedata[textdef];
138   this.direction = 0;
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);
146   }
147
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;
152     this.animation = a;
153     this.frametime = performance.now();
154     return this;
155   }
156
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;
160
161     switch(a.type){
162       case "looped":
163         frame = frame % a.frames;
164         break;
165       case "play_once":
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];
171           frame = 0;
172         }
173         break;
174       case "back_forth":
175         frame = frame % (a.frames * 2 - 2);
176         if ( frame >= a.frames ){
177          frame = a.frames - frame % a.frames - 1;
178         }
179         break;
180       default: break;
181     }
182     f = a.frame[frame][this.direction];
183
184     canvas.drawImage(gfx[this.info.image], f[0], f[1], f[2], f[3],
185                      x - f[4], y - f[5],
186                      f[2], f[3]);
187   }
188
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; };
197 }
198
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";
203
204   if (! gamedata["/engine/hero_layers.txt"])
205     gamedata["/engine/hero_layers.txt"] = new Textfile("/engine/hero_layers.txt"); 
206   this.limbs = {
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")
214   }
215
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);
220     return this;
221   }
222
223   this.place   = function(x,y) {
224     this.position = [x,y];
225     for (var limb in this.limbs) this.limbs[limb].place(x,y);
226     return this;
227   }
228   this.direct  = function(d)   {
229     this.direction = d;
230     for (var limb in this.limbs) this.limbs[limb].direct(d);
231     return this;
232   }
233   this.animate = function(anim){
234     this.animation = anim;
235     for (var limb in this.limbs) this.limbs[limb].animate(anim);
236     return this;
237   }
238   this.draw    = function(x, y){
239     gamedata["/engine/hero_layers.txt"].layer[this.direction].forEach(limb => this.limbs[limb].draw(x, y));
240     return this;
241   }
242
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; };
251 }
252
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);
257
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;
263 });
264
265 player = new Hero().place(20 * 64 , 20 * 32).direct(5);
266 map = new Map("/maps/arrival.txt");
267
268 setInterval (function() { map.center(player.position[0], player.position[1]).draw([player]);}, 33.33 );
269
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) + " )" );
274 // });
275 // 
276 // mark = [17,17];
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)];
280 // });