-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsprite.js
More file actions
46 lines (40 loc) · 1.09 KB
/
Copy pathsprite.js
File metadata and controls
46 lines (40 loc) · 1.09 KB
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(spritesheet, data) {
this.spritesheet = spritesheet
this.width = data.width
this.height = data.height
this.images = data.tiles
this.currentFrame = 0
this.speed = data.speed || 0.1
this.reverse = data.reverse || false
this.reversing = false
this.lastUpdate = frameCount
}
draw(x, y) {
let i = Math.floor(this.currentFrame)
let tile = this.images[i]
let img = this.spritesheet.get(tile.x, tile.y, this.width, this.height)
image(img, x, y)
if (this.images.length <= 1)
return
if (this.lastUpdate == frameCount)
return
if (!this.reversing)
this.currentFrame += this.speed
else
this.currentFrame += -this.speed
if (Math.floor(this.currentFrame) >= this.images.length) {
if (!this.reverse) {
this.currentFrame = 0
}
else {
this.reversing = true
this.currentFrame -= this.speed
}
} else if (this.currentFrame < 0) {
this.reversing = false
this.currentFrame += this.speed
}
this.lastUpdate = frameCount
}
}