]> git.plutz.net Git - flarejs/blob - engine.js
c1b7a9d454b19e610bc9edf172480ff01a3efbc8
[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 /^[^#].*=.+$/.test(line):
56       key = line.split(/[=]/)[0];  value = line.split(/=/).slice(1).join("=");
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   }
62 }
63
64 function Map() {
65   this.tileset = new Textfile("/tilesetdefs/tileset_grassland.txt");
66   this.frametime = performance.now();
67   if (! gfx[this.tileset.img]) {
68     gfx[this.tileset.img] = document.createElement("img");
69     gfx[this.tileset.img].setAttribute("src", this.tileset.img);
70   }
71
72   this.draw_tile = function(tile, x, y) {
73     t = this.tileset.tile[tile]
74
75     if (this.tileset.animation[tile]) {
76       f = this.tileset.animation[tile];
77       frame = ((performance.now() - this.frametime) / f[0][2] |0) % f.length;
78       canvas.drawImage(gfx[this.tileset.img], f[frame][0], f[frame][1], t[2], t[3],
79                                                     x - t[4], y - t[5], t[2], t[3]);
80     } else {
81       canvas.drawImage(gfx[this.tileset.img], t[0], t[1], t[2], t[3],
82                                       x - t[4], y - t[5], t[2], t[3]);
83     }
84   }
85 }
86
87 function Mob(textdef) {
88   if (! gamedata[textdef]) gamedata[textdef] = new Textfile(textdef); 
89   this.info = gamedata[textdef];
90   this.direction = 0;
91   this.position = [0, 0];
92   this.animation = "stance";
93   this.previous_animation = "";
94   this.frametime = performance.now();
95   if (! gfx[this.info.image]) {
96     gfx[this.info.image] = document.createElement("img");
97     gfx[this.info.image].setAttribute("src", this.info.image);
98   }
99
100   this.place = function(x, y) { this.position = [x, y]; return this; }
101   this.direct = function(d) { this.direction = d % 8; return this; }
102   this.animate = function(a) {
103     this.previous_animation = this.animation;
104     this.animation = a;
105     this.frametime = performance.now();
106     return this;
107   }
108
109   this.draw = function(){
110     var f, a = this.info[this.animation];
111     var frame = ( performance.now() - this.frametime ) * a.frames / a.duration | 0;
112
113     switch(a.type){
114       case "looped":
115         frame = frame % a.frames;
116         break;
117       case "play_once":
118         if ( frame >= a.frames ){
119           this.animation = this.previous_animation;
120           this.previous_animation = "";
121           this.frametime = performance.now();
122           a = this.info[this.animation];
123           frame = 0;
124         }
125         break;
126       case "back_forth":
127         frame = frame % (a.frames * 2 - 2);
128         if ( frame >= a.frames ){
129          frame = a.frames - frame % a.frames - 1;
130         }
131         break;
132       default: break;
133     }
134     f = a.frame[frame][this.direction];
135
136     canvas.drawImage(gfx[this.info.image], f[0], f[1], f[2], f[3],
137                      this.position[0] - f[4], this.position[1] - f[5],
138                      f[2], f[3]);
139   }
140
141   this.block  = function() { this.animate("block" ); return this; };
142   this.cast   = function() { this.animate("cast"  ); return this; };
143   this.die    = function() { this.animate("die"   ); return this; };
144   this.hit    = function() { this.animate("hit"   ); return this; };
145   this.run    = function() { this.animate("run"   ); return this; };
146   this.shoot  = function() { this.animate("shoot" ); return this; };
147   this.stance = function() { this.animate("stance"); return this; };
148   this.swing  = function() { this.animate("swing" ); return this; };
149 }
150
151 function Hero(gender = "female", hair = "short"){
152   this.position = [0,0]; this.direction = 0;
153   this.hair = (gender == "female")?"long":hair;
154   this.animation = "stance";
155
156   if (! gamedata["/engine/hero_layers.txt"])
157     gamedata["/engine/hero_layers.txt"] = new Textfile("/engine/hero_layers.txt"); 
158   this.limbs = {
159     head:  new Mob("/animations/avatar/"+gender+"/head_"+this.hair+".txt"),
160     chest: new Mob("/animations/avatar/"+gender+"/default_chest.txt"),
161     hands: new Mob("/animations/avatar/"+gender+"/default_hands.txt"),
162     legs : new Mob("/animations/avatar/"+gender+"/default_legs.txt"),
163     feet : new Mob("/animations/avatar/"+gender+"/default_feet.txt"),
164     main : new Mob("/animations/avatar/"+gender+"/dagger.txt"),
165     off  : new Mob("/animations/avatar/"+gender+"/shield.txt")
166   }
167
168   this.dress = function(limb, item) {
169     this.limbs[limb] = new Mob("/animations/avatar/"+gender+"/"+item+".txt")
170     this.limbs[limb].place(this.position[0], this.position[1]).direct(this.direction);
171     this.animate(this.animation);
172     return this;
173   }
174
175   this.place   = function(x,y) {
176     this.position = [x,y];
177     for (var limb in this.limbs) this.limbs[limb].place(x,y);
178     return this;
179   }
180   this.direct  = function(d)   {
181     this.direction = d;
182     for (var limb in this.limbs) this.limbs[limb].direct(d);
183     return this;
184   }
185   this.animate = function(anim){
186     this.animation = anim;
187     for (var limb in this.limbs) this.limbs[limb].animate(anim);
188     return this;
189   }
190   this.draw    = function(){
191     gamedata["/engine/hero_layers.txt"].layer[this.direction].forEach(limb => this.limbs[limb].draw());
192     return this;
193   }
194
195   this.block  = function() { this.animate("block" ); return this; };
196   this.cast   = function() { this.animate("cast"  ); return this; };
197   this.die    = function() { this.animate("die"   ); return this; };
198   this.hit    = function() { this.animate("hit"   ); return this; };
199   this.run    = function() { this.animate("run"   ); return this; };
200   this.shoot  = function() { this.animate("shoot" ); return this; };
201   this.stance = function() { this.animate("stance"); return this; };
202   this.swing  = function() { this.animate("swing" ); return this; };
203 }
204
205 canvas = document.getElementById("view").getContext("2d");
206 player = new Hero().place(240, 160).direct(5);
207 map = new Map();
208
209 setInterval( function() { canvas.clearRect(0,0, 480, 320); map.draw_tile(265, 240, 160); player.draw(); }, 33.33);
210