-
Notifications
You must be signed in to change notification settings - Fork 1
/
animator.js
75 lines (73 loc) · 1.55 KB
/
animator.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
class Animator {
constructor(
spritesheet,
xStart,
yStart,
width,
height,
frameCount,
frameDuration,
framePadding,
reverse,
loop
) {
Object.assign(this, {
spritesheet,
xStart,
yStart,
width,
height,
frameCount,
frameDuration,
framePadding,
reverse,
loop,
});
this.elapsedTime = 0;
this.totalTime = frameCount * frameDuration;
}
//TODO ADD SCALE COMMENTS!
/**
*
* @param {*} tick
* @param {*} ctx
* @param {*} x
* @param {*} y
* @param {*} scale <-- THIS IS NEEDED!
*/
drawFrame(tick, ctx, x, y, scale) {
this.elapsedTime += tick;
//add looping functionality
if (this.isDone()) {
if (this.loop) {
this.elapsedTime -= this.totalTime;
} else {
//TODO This was changed to show the lat frame of the image rather than nothing and;
}
}
let frame = this.currentFrame();
if (this.reverse) frame = this.frameCount - frame - 1;
//update to the last frame if it does not loop
if (this.isDone()) {
frame = this.frameCount - 1;
if (this.reverse) frame = 0;
}
ctx.drawImage(
this.spritesheet,
this.xStart + frame * (this.width + this.framePadding),
this.yStart, //source from sheet
this.width,
this.height,
x,
y,
this.width * scale,
this.height * scale
);
}
currentFrame() {
return Math.floor(this.elapsedTime / this.frameDuration);
}
isDone() {
return this.elapsedTime >= this.totalTime;
}
}