]> git.plutz.net Git - flarejs/blob - engine.js
prevent 404 for favicon
[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   if (!this.image) {
10     this.image = document.createElement("img");
11     this.image.setAttribute("src", this.info.image);
12     this.image.setAttribute("style", "display: none;");
13     document.querySelector("body").appendChild(this.image);
14   }
15
16   this.place = function(x, y) { this.position = [x, y]; return this; }
17   this.direct = function(d) { this.direction = d % 8; return this; }
18   this.animate = function(a) {
19     this.previous_animation = this.animation;
20     this.animation = a;
21     this.frametime = performance.now();
22     return this;
23   }
24
25   this.draw = function(){
26     var f, a = this.info[this.animation];
27     var frame = ( performance.now() - this.frametime ) * a.frames.length / a.duration | 0;
28
29     switch(a.type){
30       case "looped":
31         frame = frame % a.frames.length;
32         break;
33       case "play_once":
34         if ( frame >= a.frames.length ){
35           this.animation = this.previous_animation;
36           this.previous_animation = "";
37           this.frametime = performance.now();
38           a = this.info[this.animation];
39           frame = 0;
40         }
41         break;
42       case "back_forth":
43         frame = frame % (a.frames.length * 2 - 2);
44         if ( frame >= a.frames.length ){
45          frame = a.frames.length - frame % a.frames.length - 1;
46         }
47         break;
48       default: break;
49     }
50     f = a.frames[frame][this.direction];
51
52     canvas.drawImage(this.image, f[0], f[1], f[2], f[3],
53                      this.position[0] - f[4], this.position[1] - f[5],
54                      f[2], f[3]);
55
56   // var fetch = new XMLHttpRequest();
57   // fetch.open("GET", textdef, false); fetch.send();
58   // this.description = fetch.responseText.split('\n');
59   }
60 }
61
62 function Player(gender = "female", hair = "short"){
63   this.x=0; this.y=0; this.direction=0;
64   this.limbs = {
65     head : (gender == "female")?new Mob("/animations/avatar/female/head_long.txt")
66                                :new Mob("/animations/avatar/male/head_"+hair+".txt"),
67     chest: new Mob("/animations/avatar/"+gender+"/default_chest.txt"),
68     hands: new Mob("/animations/avatar/"+gender+"/default_hands.txt"),
69     legs : new Mob("/animations/avatar/"+gender+"/default_legs.txt"),
70     feet : new Mob("/animations/avatar/"+gender+"/default_feet.txt"),
71     main : new Mob("/animations/avatar/"+gender+"/dagger.txt"),
72     off  : new Mob("/animations/avatar/"+gender+"/shield.txt")
73   }
74
75   this.place   = function(x,y) {
76     this.x = x; this.y = y;
77     for (var limb in this.limbs) this.limbs[limb].place(x,y);
78     return this;
79   }
80   this.direct  = function(d)   {
81     this.direction = d;
82     for (var limb in this.limbs) this.limbs[limb].direct(d);
83     return this;
84   }
85   this.animate = function(anim){
86     for (var limb in this.limbs) this.limbs[limb].animate(anim);
87     return this;
88   }
89   this.draw    = function(){
90     gamedata["/engine/hero_layers.txt"].layers[this.direction].forEach(limb => this.limbs[limb].draw());
91     return this;
92   }
93 }
94
95 canvas = document.getElementById("view").getContext("2d");
96 // player = new Mob("/animations/avatar/male/clothes.txt");
97 player = new Player().place(240, 160).direct(5);
98
99 setInterval( function() { canvas.clearRect(0,0, 480, 320); player.draw(); }, 50);