1 function Mob(textdef) {
3 this.position = [0, 0];
4 this.info = gamedata[textdef];
5 this.animation = "stance";
6 this.previous_animation = "";
7 this.frametime = performance.now();
8 this.image = document.querySelector("img[src='"+ this.info.image +"']");
10 this.place = function(x, y) { this.position = [x, y]; }
11 this.direct = function(d) { this.direction = d % 8; }
12 this.animate = function(a) {
13 this.previous_animation = this.animation;
15 this.frametime = performance.now();
18 this.draw = function(){
19 var f, a = this.info[this.animation];
20 var frame = ( performance.now() - this.frametime ) * a.frames.length / a.duration | 0;
24 frame = frame % a.frames.length;
27 if ( frame >= a.frames.length ){
28 this.animation = this.previous_animation;
29 this.previous_animation = "";
30 this.frametime = performance.now();
31 a = this.info[this.animation];
36 frame = frame % (a.frames.length * 2 - 2);
37 if ( frame >= a.frames.length ){
38 frame = a.frames.length - frame % a.frames.length - 1;
43 f = a.frames[frame][this.direction];
45 canvas.clearRect(0,0, 640, 480);
46 canvas.drawImage(this.image, f[0], f[1], f[2], f[3],
47 this.position[0] - f[4], this.position[1] - f[5],
50 // var fetch = new XMLHttpRequest();
51 // fetch.open("GET", textdef, false); fetch.send();
52 // this.description = fetch.responseText.split('\n');
56 canvas = document.getElementById("view").getContext("2d");
57 player = new Mob("/animations/avatar/male/clothes.txt");
58 player.place(320, 240);
60 setInterval( function() { player.draw(); }, 33);