-
Notifications
You must be signed in to change notification settings - Fork 0
/
sprite.go
executable file
·78 lines (67 loc) · 1.38 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
package main
import (
"math/rand"
tcell "github.com/gdamore/tcell/v2"
)
type Sprite struct {
PosX int
PosY int
Rotation int
Shape *Shape
Style tcell.Style
}
var (
foreground = tcell.ColorBlack
styles = []tcell.Style{
tcell.StyleDefault.
Foreground(foreground).
Background(tcell.ColorLightBlue),
tcell.StyleDefault.
Foreground(foreground).
Background(tcell.ColorLightGreen),
tcell.StyleDefault.
Foreground(foreground).
Background(tcell.ColorLightSalmon),
tcell.StyleDefault.
Foreground(foreground).
Background(tcell.ColorLightCoral),
}
)
func NewSprite() *Sprite {
shape := &shapes[rand.Int()%len(shapes)]
style := styles[rand.Int()%len(styles)]
return &Sprite{
PosX: MainWidth / 2,
PosY: 0,
Rotation: rand.Int() % len(shape.Frames),
Shape: shape,
Style: style,
}
}
func (s *Sprite) Frame() *Frame {
return &s.Shape.Frames[s.Rotation]
}
func (s *Sprite) NextFrame() {
frames := len(s.Shape.Frames)
s.Rotation += 1
if s.Rotation >= frames {
s.Rotation = 0
}
}
func (s *Sprite) Width() int {
return s.Frame().Width
}
func (s *Sprite) Height() int {
return s.Frame().Height
}
func (s *Sprite) Draw(grid *Grid) {
for y := 0; y < s.Height(); y++ {
for x := 0; x < s.Width(); x++ {
i := y*s.Width() + x
if s.Frame().Data[i] == 0 {
continue
}
grid.SetOccupied(s.PosX+x, s.PosY+y, s.Style)
}
}
}