]> git.plutz.net Git - flarejs/blob - engine.js
ignore cache file
[flarejs] / engine.js
1 function Mob(textdef) {
2   this.direction = 0;
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 +"']");
9
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;
14     this.animation = a;
15     this.frametime = performance.now();
16   }
17
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;
21
22     switch(a.type){
23       case "looped":
24         frame = frame % a.frames.length;
25         break;
26       case "play_once":
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];
32           frame = 0;
33         }
34         break;
35       case "back_forth":
36         frame = frame % (a.frames.length * 2 - 2);
37         if ( frame >= a.frames.length ){
38          frame = a.frames.length - frame % a.frames.length - 1;
39         }
40         break;
41       default: break;
42     }
43     f = a.frames[frame][this.direction];
44
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],
48                      f[2], f[3]);
49
50   // var fetch = new XMLHttpRequest();
51   // fetch.open("GET", textdef, false); fetch.send();
52   // this.description = fetch.responseText.split('\n');
53   }
54 }
55
56 canvas = document.getElementById("view").getContext("2d");
57 player = new Mob("/animations/avatar/male/clothes.txt");
58 player.place(320, 240);
59
60 setInterval( function() { player.draw(); }, 33);