-
Notifications
You must be signed in to change notification settings - Fork 11
/
background.go
63 lines (54 loc) · 1.21 KB
/
background.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
package sprite
import (
"strings"
tm "github.com/pdevine/go-asciisprite/termbox"
)
// A Background interface provides methods for initializing, updating, and rendering sprites.
type Background interface {
Init()
Update()
Render()
AddBackground(t string)
}
// A BaseBackground is a 2D background which sits behind Sprites.
type BaseBackground struct {
X int
Y int
Height int
Width int
Tiled bool
Background []Block
}
// Init provides a hook for initializing a background.
func (s *BaseBackground) Init() {
// Init things
}
// Update provides a hook for updating a background during the main loop.
func (s *BaseBackground) Update() {
// Do things
}
// Render draws background to the buffer.
func (s *BaseBackground) Render() {
for _, b := range s.Background {
tm.SetCell(b.X+s.X, b.Y+s.Y, b.Char, b.Fg, b.Bg)
}
}
func (s *BaseBackground) AddBackground(t string) {
s.Background = []Block{}
var width int
var height int
for y, line := range strings.Split(t, "\n") {
for x, ch := range line {
b := Block{
Char: ch,
X: x,
Y: y,
}
s.Background = append(s.Background, b)
width = max(x, width)
}
height = y
}
s.Width = width
s.Height = height
}