-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspritesheet.go
71 lines (62 loc) · 1.82 KB
/
spritesheet.go
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
package main
import (
"image"
"github.com/hajimehoshi/ebiten"
)
// Sprite stores information on where a sprite subimage is located
type Sprite struct {
startPosition Vec2i
endPosition Vec2i
size Vec2i
}
func createSprite(startPosition Vec2i, endPosition Vec2i, size Vec2i, image *ebiten.Image) Sprite {
return Sprite{startPosition, endPosition, size}
}
func (s *Sprite) getBounds() image.Rectangle {
return image.Rect(s.startPosition.x, s.startPosition.y, s.endPosition.x, s.endPosition.y)
}
// Spritesheet is a collection of Sprite's
type Spritesheet struct {
sprites []Sprite
numberOfSprites int
startPosition Vec2i
size Vec2i
}
func createSpritesheet(startPosition Vec2i, endPosition Vec2i, numberOfSprites int, image *ebiten.Image) Spritesheet {
// Create sprite slice
sprites := make([]Sprite, numberOfSprites)
// Calculate size of entire sheet
size := newVec2i(endPosition.x-startPosition.x, endPosition.y-startPosition.y)
// Calculate size of single sprite
spriteSize := newVec2i(size.x/numberOfSprites, size.y)
for i := 0; i < numberOfSprites; i++ {
// Calculate start and end positions of current sprite
spriteStartPosition := newVec2i(
i*spriteSize.x+startPosition.x,
startPosition.y,
)
spriteEndPosition := newVec2i(
i*spriteSize.x+startPosition.x+spriteSize.x,
spriteSize.y+startPosition.y,
)
// Add sprite to slice
sprites[i] = Sprite{spriteStartPosition, spriteEndPosition, spriteSize}
}
return Spritesheet{
sprites,
numberOfSprites,
startPosition,
size,
}
}
func (s *Spritesheet) endPosition() Vec2i {
return newVec2i(s.startPosition.x+s.size.x, s.startPosition.y+s.size.y)
}
func (s *Spritesheet) getBounds() image.Rectangle {
return image.Rect(
s.startPosition.x,
s.startPosition.y,
s.startPosition.x+s.size.x,
s.startPosition.y+s.size.y,
)
}