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 /^[^#].*=.+$/.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);
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);
72 this.draw_tile = function(tile, x, y) {
73 t = this.tileset.tile[tile]
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]);
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]);
87 function Mob(textdef) {
88 if (! gamedata[textdef]) gamedata[textdef] = new Textfile(textdef);
89 this.info = gamedata[textdef];
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);
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;
105 this.frametime = performance.now();
109 this.draw = function(){
110 var f, a = this.info[this.animation];
111 var frame = ( performance.now() - this.frametime ) * a.frames / a.duration | 0;
115 frame = frame % a.frames;
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];
127 frame = frame % (a.frames * 2 - 2);
128 if ( frame >= a.frames ){
129 frame = a.frames - frame % a.frames - 1;
134 f = a.frame[frame][this.direction];
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],
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; };
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";
156 if (! gamedata["/engine/hero_layers.txt"])
157 gamedata["/engine/hero_layers.txt"] = new Textfile("/engine/hero_layers.txt");
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")
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);
175 this.place = function(x,y) {
176 this.position = [x,y];
177 for (var limb in this.limbs) this.limbs[limb].place(x,y);
180 this.direct = function(d) {
182 for (var limb in this.limbs) this.limbs[limb].direct(d);
185 this.animate = function(anim){
186 this.animation = anim;
187 for (var limb in this.limbs) this.limbs[limb].animate(anim);
190 this.draw = function(){
191 gamedata["/engine/hero_layers.txt"].layer[this.direction].forEach(limb => this.limbs[limb].draw());
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; };
205 canvas = document.getElementById("view").getContext("2d");
206 player = new Hero().place(240, 160).direct(5);
209 setInterval( function() { canvas.clearRect(0,0, 480, 320); map.draw_tile(265, 240, 160); player.draw(); }, 33.33);