-
Notifications
You must be signed in to change notification settings - Fork 7
/
Animation.js
executable file
·113 lines (110 loc) · 3.07 KB
/
Animation.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
define(['./jo'], function(jo){
/**
* @class defines an animation, needs a sprite as frame set,
* define the frames' index and length in milliseconds and call run every frame.
* For now it only takes frames in vertical direction
*
*/
jo.Animation = jo.Class.extend({
/**
* @constructor
* @param {jo.Sprite} sprite the frame set
* @param {array} frames as {i: {number} frameIndex, t: {number} time in ms}
* or just time in milliseconds
* @param {number} width width of one frame
* @param {number} height height of one frame
* @methodOf jo.Animation
*/
init: function(frames, width, height, sprite, startCallback, finishCallback){
console.log(frames);
this.sprite = (sprite) ? sprite : null;
this.width = (width) ? width : 0;
this.height = (height) ? height : 0;
this.xoff = 0;
this.yoff = 0;
this.frames = this.readFrames(frames);
this.frame = 0;
this.tickCount = 0;
this.startCallback = startCallback;
this.finishCallback = finishCallback;
this.start = true;
console.log(this.frames);
},
/**
* reads and transforms an array of frames
* @param frames
* @returns the transformed frames array
*/
readFrames: function(frames){
if(typeof frames === 'undefined'){
frames = [100];
}
if(typeof frames.length !== 'undefined'){
for(var i=0; i< frames.length; i++){
frames[i] = typeof frames[i] === 'object'? frames[i] : {i: i, t: frames[i]};
frames[i].drawFrame = this.calcFrame(frames[i].i);
}
}
return frames;
},
/**
* @description call to advance the animation
* @param {number} ticks milliseconds to advance
*/
update: function(ticks){
if(this.start === true ){
if(typeof this.startCallback === 'function'){
this.startCallback(this);
}
this.frame = this.frame % this.frames.length;
}
this.tickCount += ticks;
if(this.frames[this.frame].t - this.tickCount <= 0 ){
this.tickCount -= this.frames[this.frame].t;
this.frame +=1;
}
if(this.frame >= this.frames.length ){
if(typeof this.finishCallback === 'function'){
this.finishCallback(this);
}
this.start = true;
this.frame = this.frame % this.frames.length;
}
},
/**
* @description draw the current frame, same parameters as jo.Sprite
* @param surface
* @param position
* @param options
* @see jo.Sprite
*/
draw: function(options, position, surface){
if(this.sprite){
options.frame = this.frames[this.frame].drawFrame;
this.sprite.draw(options, position, surface);
}
},
/**
* returns a frame rectangle for use with jo.Sprite.draw
* @param frame
* @returns {Object}
* @see jo.Sprite.draw
*/
calcFrame: function(frame){
var cols = Math.floor(this.sprite.width / this.width);
return {
x: this.xoff + ((frame % cols)) * this.width,
y: this.yoff + Math.floor(frame / cols) * this.height,
width: this.width,
height: this.height
};
},
getDrawFrame: function(frame){
if(!frame){
frame=0;
}
return this.frames[frame].drawFrame;
}
});
return jo.Animation;
});