-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassetmanager.go
151 lines (115 loc) · 3.12 KB
/
assetmanager.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package main
import (
"bytes"
"image"
_ "image/png"
"github.com/faiface/pixel"
)
type AssetManager struct {
Batch *pixel.Batch
spritesheet pixel.Picture
}
func NewAssetManager() *AssetManager {
result := &AssetManager{}
result.spritesheet = result.loadSpritesheet()
result.Batch = result.createBatch(result.spritesheet)
return result
}
func (am *AssetManager) createBatch(spritesheet pixel.Picture) *pixel.Batch {
batch := pixel.NewBatch(&pixel.TrianglesData{}, spritesheet)
return batch
}
/*
loadPicture loads an image file into a picture struct
*/
func (am *AssetManager) loadPicture(path string) (pixel.Picture, error) {
var err error
var img image.Image
var imageBytes []byte
if imageBytes, err = FSByte(false, path); err != nil {
return nil, err
}
reader := bytes.NewReader(imageBytes)
if img, _, err = image.Decode(reader); err != nil {
return nil, err
}
return pixel.PictureDataFromImage(img), nil
}
func (am *AssetManager) loadSpritesheet() pixel.Picture {
var err error
var pic pixel.Picture
if pic, err = am.loadPicture("/assets/spritesheet.png"); err != nil {
panic(err)
}
return pic
}
func (am *AssetManager) GetBulletSprite() *pixel.Sprite {
result := pixel.NewSprite(am.spritesheet, pixel.R(0, 0, 10, 38))
return result
}
func (am *AssetManager) GetShipSprite() *pixel.Sprite {
result := pixel.NewSprite(am.spritesheet, pixel.R(10, 0, 60, 38))
return result
}
func (am *AssetManager) GetBlueInvaderSprite() *pixel.Sprite {
result := pixel.NewSprite(am.spritesheet, pixel.R(61, 0, 110, 38))
return result
}
func (am *AssetManager) GetGreenInvaderSprite() *pixel.Sprite {
result := pixel.NewSprite(am.spritesheet, pixel.R(111, 0, 160, 38))
return result
}
func (am *AssetManager) GetRedInvaderSprite() *pixel.Sprite {
result := pixel.NewSprite(am.spritesheet, pixel.R(161, 0, 210, 38))
return result
}
func (am *AssetManager) LoadMenuNewGameAsset() *pixel.Sprite {
var err error
var pic pixel.Picture
if pic, err = am.loadPicture("/assets/menu-new-game.png"); err != nil {
panic(err)
}
result := pixel.NewSprite(pic, pic.Bounds())
return result
}
func (am *AssetManager) LoadMenuQuitAsset() *pixel.Sprite {
var err error
var pic pixel.Picture
if pic, err = am.loadPicture("/assets/menu-quit.png"); err != nil {
panic(err)
}
result := pixel.NewSprite(pic, pic.Bounds())
return result
}
func (am *AssetManager) LoadYouLoseAsset() *pixel.Sprite {
var err error
var pic pixel.Picture
if pic, err = am.loadPicture("/assets/you-lose.png"); err != nil {
panic(err)
}
result := pixel.NewSprite(pic, pic.Bounds())
return result
}
func (am *AssetManager) LoadYouWinAsset() *pixel.Sprite {
var err error
var pic pixel.Picture
if pic, err = am.loadPicture("/assets/you-win.png"); err != nil {
panic(err)
}
result := pixel.NewSprite(pic, pic.Bounds())
return result
}
func (am *AssetManager) GetStarsAsset() pixel.Picture {
pic, err := am.loadPicture("/assets/stars.png")
if err != nil {
panic(err)
}
return pic
}
func (am *AssetManager) GetCloserStarsAsset() pixel.Picture {
pic, err := am.loadPicture("/assets/closer-stars.png")
if err != nil {
panic(err)
}
return pic
}