forked from amanva/The-Last-Magus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanimator.js
65 lines (51 loc) · 2.12 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
class Animator {
constructor(spritesheet, xStart, yStart, width, height, frameCount, frameDuration, framePaddingX, framePaddingY, reverse, loop, verticalSprite) {
Object.assign(this, {spritesheet, xStart, yStart, width, height, frameCount, frameDuration, framePaddingX, framePaddingY, reverse, loop, verticalSprite});
this.elapsedTime = 0;
this.totalTime = this.frameCount * this.frameDuration;
};
drawFrame(tick, ctx, x, y, scale) {
this.elapsedTime += tick;
if (this.isDone()) {
if (this.loop) {
this.elapsedTime -= this.totalTime;
} else {
return;
}
}
if(this.elapsedTime > this.totalTime) this.elapsedTime -= this.totalTime;
let frame = this.currentFrame();
if (this.reverse) {
frame = this.frameCount - frame - 1;
}
/*
verticalSprite = true for sprites that are verical.
Horizontals are by default.
added by UH.
*/
if (this.verticalSprite){
// Vertical sprites drawn here.
ctx.drawImage(this.spritesheet,
this.xStart , this.yStart + frame * (this.height + this.framePaddingY), //source x and y (for example, use "0, 0" on call of this function)
this.width, this.height, //source width and hight
x, y, //destination x and y, where to draw this frame
this.width * scale, this.height * scale); //destination width and hight
} else {
// Horizontal sprites drawn here
ctx.drawImage(this.spritesheet,
this.xStart + frame * (this.width + this.framePaddingX), this.yStart,
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);
};
isAlmostDone(TICK) {
return ((this.elapsedTime + TICK) >= this.totalTime);
}
}