-
Notifications
You must be signed in to change notification settings - Fork 81
/
game.go
63 lines (53 loc) · 1.07 KB
/
game.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 termloop
import "time"
import "github.com/nsf/termbox-go"
type Game struct {
screen *Screen
input *Input
}
func NewGame() *Game {
g := Game{screen: NewScreen(), input: NewInput()}
return &g
}
func (g *Game) SetScreen(s *Screen) {
g.screen = s
}
func (g *Game) CreateLevel(bg Attr) {
g.screen.level = NewBaseLevel(Cell{Bg: bg})
}
func (g *Game) SetLevel(l Level) {
g.screen.level = l
}
func (g *Game) AddEntity(d Drawable) {
g.screen.entities = append(g.screen.entities, d)
}
func (g *Game) Start() {
// Init Termbox
err := termbox.Init()
if err != nil {
panic(err)
}
defer termbox.Close()
g.screen.Resize(termbox.Size())
// Init input
g.input.Start()
defer g.input.Stop()
clock := time.Now()
mainloop:
for {
select {
case ev := <-g.input.eventQ:
if ev.Key == g.input.endKey {
break mainloop
} else if EventType(ev.Type) == EventResize {
g.screen.Resize(ev.Width, ev.Height)
}
g.screen.Tick(ConvertEvent(ev))
default:
}
update := time.Now()
g.screen.delta = update.Sub(clock).Seconds()
clock = update
g.screen.Draw()
}
}