-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sprite.js
46 lines (42 loc) · 1.12 KB
/
Sprite.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
class Sprite {
constructor({position, image, frames = {max: 1,}, sprites}) {
this.position = position;
this.image = image;
this.frames = {...frames,current: 0, elapsed: 0};
this.image.onload = () => {
this.width = this.image.width / this.frames.max;
this.height = this.image.height;
};
this.sprites = sprites;
this.moving = false;
}
draw() {
c.drawImage(
this.image,
this.frames.current * this.width,
0,
this.width,
this.height,
this.position.x,
this.position.y,
this.width,
this.height
);
this.updateFrames();
}
updateFrames() {
if (!this.moving) {
return
}
if (this.frames.max > 1) {
this.frames.elapsed++;
}
if (this.frames.elapsed % 10 === 0) {
if (this.frames.current < this.frames.max - 1) {
this.frames.current++;
} else {
this.frames.current = 0;
}
}
}
}