-
Notifications
You must be signed in to change notification settings - Fork 11
/
sprite.go
306 lines (274 loc) · 7.24 KB
/
sprite.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
// Package sprite provides a framework for creating ASCII and Unicode based animations and games.
package sprite
import (
tm "github.com/pdevine/go-asciisprite/termbox"
)
// A Sprite interface provides methods for initializing, updating, and rendering sprites.
type Sprite interface {
Init()
Update()
Render()
BlockRender(*Surface)
AddCostume(Costume)
SetCostume(int)
NextCostume()
PrevCostume()
TriggerEvent(string) bool
IsDead() bool
}
type Event struct {
Callback func()
Count int
}
// A BaseSprite is a 2D sprite primitive.
type BaseSprite struct {
X int
Y int
Height int
Width int
Costumes []*Costume
BlockCostumes []*Surface
Alpha rune
Visible bool
CurrentCostume int
Dead bool
Events map[string]*Event
}
// A SpriteGroup is a convenience method for holding groups of sprites.
type SpriteGroup struct {
Sprites []Sprite
Events chan string
BlockMode bool
Background tm.Attribute
bg *Surface
}
// NewBaseSprite creates a new BaseSprite from X and Y coordinates and a costume.
func NewBaseSprite(x, y int, costume Costume) *BaseSprite {
s := &BaseSprite{
X: x,
Y: y,
Height: 0,
Width: 0,
Visible: true,
Costumes: []*Costume{},
BlockCostumes: []*Surface{},
CurrentCostume: 0,
Dead: false,
}
s.AddCostume(costume)
return s
}
// AddCostume adds a costume to a BaseSprite.
func (s *BaseSprite) AddCostume(costume Costume) {
s.Costumes = append(s.Costumes, &costume)
if len(s.Costumes) == 1 {
s.SetCostume(0)
}
}
// SetCostume sets the current costume of a BaseSprite.
func (s *BaseSprite) SetCostume(c int) {
// XXX - check for bounds here and return an error
s.CurrentCostume = c
if len(s.BlockCostumes) > 0 {
s.Height = s.BlockCostumes[s.CurrentCostume].Height
s.Width = s.BlockCostumes[s.CurrentCostume].Width
} else if len(s.Costumes) > 0 {
s.Height = s.Costumes[s.CurrentCostume].Height
s.Width = s.Costumes[s.CurrentCostume].Width
}
}
// Render draws the sprite to the screen buffer.
func (s *BaseSprite) Render() {
if s.Visible {
for _, b := range s.Costumes[s.CurrentCostume].Blocks {
// call tcell screen.GetContent(b.X+s.X, b.Y+s.Y) here to see if we've already
// written to the same location
tm.SetCell(b.X+s.X, b.Y+s.Y, b.Char, tm.Attribute(b.Fg), tm.Attribute(b.Bg))
}
}
}
// BlockRender draws the sprite to the background surface
func (s *BaseSprite) BlockRender(bg *Surface) {
if s.Visible {
if len(s.BlockCostumes) > s.CurrentCostume {
surf := s.BlockCostumes[s.CurrentCostume]
bg.Blit(*surf, s.X, s.Y)
}
}
}
// NextCostume changes a sprite's costume to the next costume.
func (s *BaseSprite) NextCostume() {
s.CurrentCostume++
if len(s.BlockCostumes) > 0 {
if s.CurrentCostume >= len(s.BlockCostumes) {
s.CurrentCostume = 0
}
} else if len(s.Costumes) > 0 {
if s.CurrentCostume >= len(s.Costumes) {
s.CurrentCostume = 0
}
}
// Set the Width/Height
s.SetCostume(s.CurrentCostume)
}
// PrevCostume changes a sprite's costume to the previous costume.
func (s *BaseSprite) PrevCostume() {
s.CurrentCostume--
if s.CurrentCostume < 0 {
if len(s.BlockCostumes) > 0 {
s.CurrentCostume = len(s.BlockCostumes) - 1
} else {
s.CurrentCostume = len(s.Costumes) - 1
}
}
// Set the Width/Height
s.SetCostume(s.CurrentCostume)
}
// Init provides a hook for initializing a sprite.
func (s *BaseSprite) Init() {
// Init things
s.Events = make(map[string]*Event)
if len(s.BlockCostumes) > 0 {
s.Height = s.BlockCostumes[s.CurrentCostume].Height
s.Width = s.BlockCostumes[s.CurrentCostume].Width
} else if len(s.Costumes) > 0 {
s.Height = s.Costumes[s.CurrentCostume].Height
s.Width = s.Costumes[s.CurrentCostume].Width
}
}
// Update provides a hook for updating a sprite during the main loop.
func (s *BaseSprite) Update() {
// Do things
}
// HitAtPoint reports whether a point on the screen intersects with this sprite.
func (s *BaseSprite) HitAtPoint(x, y int) bool {
c := s.Costumes[s.CurrentCostume]
if x >= s.X+c.LeftEdge() && x <= s.X+c.RightEdge() && y >= s.Y+c.TopEdge() && y <= s.Y+c.BottomEdge() {
return true
}
return false
}
// HitAtPointSurface reports whether a point on the surface intersects with this sprite.
func (s *BaseSprite) HitAtPointSurface(x, y int) bool {
surf := s.BlockCostumes[s.CurrentCostume]
if x >= s.X && x <= s.X+surf.Width && y >= s.Y && y <= s.Y+surf.Height {
return true
}
return false
}
// RegisterEvent registers a callback function with a name in this sprite.
func (s *BaseSprite) RegisterEvent(name string, fn func()) {
e := &Event{
Callback: fn,
}
s.Events[name] = e
}
// TriggerEvent causes a previously registered callback function to be called.
func (s *BaseSprite) TriggerEvent(name string) bool {
e, ok := s.Events[name]
if !ok {
return false
}
e.Callback()
return true
}
// RemoveEvent removes an event with a given name from this sprite.
func (s *BaseSprite) RemoveEvent(name string) bool {
_, ok := s.Events[name]
if !ok {
return false
}
s.Events[name] = nil
return true
}
// IsDead returns if the sprite is dead and should be reaped.
func (s *BaseSprite) IsDead() bool {
return s.Dead
}
// Init initializes the event channel and sets the BlockMode
func (sg *SpriteGroup) Init(width, height int, blockMode bool) {
if blockMode {
sg.BlockMode = blockMode
sg.Background = tm.ColorDefault
surf := NewSurface(width, height, false)
sg.bg = &surf
}
sg.Events = make(chan string)
go func() {
for {
v := <-sg.Events
var dl []Sprite
for _, s := range sg.Sprites {
if !s.IsDead() {
s.TriggerEvent(v)
} else {
dl = append(dl, s)
}
}
for _, s := range dl {
sg.Remove(s)
}
}
}()
}
// Resize changes the size of the drawing buffer in BlockMode
func (sg *SpriteGroup) Resize(width, height int) {
if sg.BlockMode {
surf := NewSurface(width, height, false)
sg.bg = &surf
}
}
// TriggerEvent causes all events of this name to be called.
func (sg *SpriteGroup) TriggerEvent(name string) {
sg.Events <- name
}
// Render draws each sprite in the SpriteGroup to the buffer.
func (sg *SpriteGroup) Render() {
if sg.BlockMode {
sg.bg.Clear()
for _, s := range sg.Sprites {
s.BlockRender(sg.bg)
}
c := sg.bg.ConvertToColorCostume(sg.Background)
for _, b := range c.Blocks {
tm.SetCell(b.X, b.Y, b.Char, tm.Attribute(b.Fg), tm.Attribute(b.Bg))
}
} else {
for _, s := range sg.Sprites {
s.Render()
}
}
tm.Flush()
}
// Update updates each sprite in the SpriteGroup.
func (sg *SpriteGroup) Update() {
for _, s := range sg.Sprites {
s.Update()
}
}
// Remove removes a given sprite from the SpriteGroup.
func (sg *SpriteGroup) Remove(s Sprite) {
var idx int
var found bool
for cnt, tSprite := range sg.Sprites {
if s == tSprite {
idx = cnt
found = true
break
}
}
if found {
copy(sg.Sprites[idx:], sg.Sprites[idx+1:])
sg.Sprites[len(sg.Sprites)-1] = nil
sg.Sprites = sg.Sprites[:len(sg.Sprites)-1]
}
}
// RemoveAll removes all sprites from the SpriteGroup.
func (sg *SpriteGroup) RemoveAll() {
sg.Sprites = []Sprite{}
}
// MoveToTop renders a sprite over all other sprites in the SpriteGroup.
func (sg *SpriteGroup) MoveToTop(s Sprite) {
sg.Remove(s)
sg.Sprites = append(sg.Sprites, s)
}