-
Notifications
You must be signed in to change notification settings - Fork 0
/
Animation.js
72 lines (50 loc) · 1.43 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
class Animation {
constructor(image, length, timeDilation, speedIsFluid) {
this.images = [];
this.length = length;
for (let i = 0; i < this.length; i++) {
this.images[i] = loadImage(image + (i+1) + ".png");
}
if (validParameter(timeDilation)) {
this.timeDilation = timeDilation;
} else {
this.timeDilation = 1;
}
this.index = 0;
this.speedIsFluid = speedIsFluid;
this.assignDimension();
}
draw(x, y) {
this.incrementIndex();
let index = floor(this.index);
image(this.images[index], x, y);
}
playThrough(x, y) {
this.index += 1/this.timeDilation;
if (this.index >= this.images.length) {
this.playedThrough = true;
}
if (this.playedThrough) {
return true;
}
let index = floor(this.index);
image(this.images[index], x, y);
}
incrementIndex() {
this.index += 1/this.timeDilation;
if (this.index >= this.length) {
this.index = 0;
}
}
// width and height don't load correctly when called in preload function
setup() {
this.assignDimension();
}
assignDimension() {
this.width = this.images[0].width;
this.height = this.images[0].height;
}
setDilation(timeDilation) {
this.timeDilation = timeDilation;
}
}