-
Notifications
You must be signed in to change notification settings - Fork 12
/
game.go
174 lines (142 loc) · 3.2 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
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
package vimman
import (
"time"
"github.com/nsf/termbox-go"
"github.com/pkg/errors"
)
const (
bgColor = termbox.ColorBlack
fgColor = termbox.ColorWhite
)
type Point struct {
x int
y int
}
type Game struct {
Stage *Stage
screenSizeX int
screenSizeY int
VimManEvents chan VimManEvent
}
type GameOptions struct {
fps float64
initialLevel int
bgCell *termbox.Cell
VimManEvents chan VimManEvent
}
type VimManEvent struct {
Content string
}
func NewGame(opts GameOptions) *Game {
bgCell := &termbox.Cell{'░', fgColor, bgColor}
game := &Game{nil, 0, 0, opts.VimManEvents}
stage := NewStage(game, opts.initialLevel, opts.fps, bgCell)
game.Stage = stage
return game
}
type Renderer interface {
Update(*Stage, termbox.Event, time.Duration)
Destroy()
SetCells(*Stage)
GetCells() []*TermBoxCell
GetPosition() (int, int)
GetPositionX() int
GetPositionY() int
SetPositionX(int)
GetScreenOffset() (int, int)
GetDrawPriority() int
GetTags() []Tag
ShouldCenterHorizontally() bool
}
// main game loop
// handles events, updates and renders stage and entities
func gameLoop(termboxEvents chan termbox.Event, vimManEvents chan VimManEvent, game *Game) {
termbox.Clear(fgColor, bgColor)
game.setScreenSize(termbox.Size())
stage := game.Stage
stage.Init()
stage.Render()
lastUpdateTime := time.Now()
for {
termbox.Clear(fgColor, bgColor)
update := time.Now()
select {
case event := <-termboxEvents:
switch {
case event.Key == termbox.KeyCtrlC:
// exit on ctrc + c
return
case event.Type == termbox.EventResize:
game.setScreenSize(termbox.Size())
default:
stage.update(event, update.Sub(lastUpdateTime))
}
default:
stage.update(termbox.Event{}, update.Sub(lastUpdateTime))
}
// handle vim-man events here
select {
case event := <-vimManEvents:
switch {
case event.Content == "exit":
return
}
default:
}
lastUpdateTime = time.Now()
stage.Render()
time.Sleep(time.Duration((update.Sub(time.Now()).Seconds()*1000.0)+1000.0/stage.Fps) * time.Millisecond)
}
}
func termboxEventLoop(e chan termbox.Event) {
for {
e <- termbox.PollEvent()
}
}
func exit(events chan termbox.Event) {
close(events)
termbox.Close()
}
func Init(level int) {
if err := termbox.Init(); err != nil {
panic(errors.Wrap(err, "failed to init termbox"))
}
if level == 0 {
level = 1
}
termbox.SetOutputMode(termbox.Output256)
termbox.Clear(termbox.ColorDefault, bgColor)
termboxEvents := make(chan termbox.Event)
go termboxEventLoop(termboxEvents)
vimManEvents := make(chan VimManEvent)
game := NewGame(GameOptions{
fps: 50,
initialLevel: level,
VimManEvents: vimManEvents,
})
// main game loop, this is blocking
gameLoop(termboxEvents, vimManEvents, game)
// dump logs after the gameLoop stops
if len(lg.logs) > 0 {
lg.DumpLogs()
time.Sleep(2 * time.Second)
}
exit(termboxEvents)
}
func (g *Game) setScreenSize(x, y int) {
if x > 0 {
g.screenSizeX = x
}
if y > 0 {
g.screenSizeY = y
}
}
func (g *Game) getScreenSize() (int, int) {
return g.getScreenSizeX(), g.getScreenSizeY()
}
func (g *Game) getScreenSizeX() int {
return g.screenSizeX
}
func (g *Game) getScreenSizeY() int {
return g.screenSizeY
}