-
Notifications
You must be signed in to change notification settings - Fork 8
/
game.go
253 lines (224 loc) · 4.91 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
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
// Copyright 2016 Garrett D'Amore
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use file except in compliance with the License.
// You may obtain a copy of the license at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"errors"
"fmt"
"github.com/gdamore/tcell"
"github.com/gdamore/tcell/views"
"sync"
"time"
)
// go-proxima (Escape from Proxima Five) is a terminal-oriented game,
// utilizing Unicode characters and text terminal display capabilties,
// demonstrating the capabilities of tcell. It needs at leasst an 80x24
// column terminal to run well.
type Game struct {
quitq chan struct{}
screen tcell.Screen
eventq chan tcell.Event
errmsg string
quitone sync.Once
level *Level
lview *views.ViewPort
sview *views.ViewPort
sbar *views.TextBar
lives int
gameover bool
started bool
sync.Mutex
}
func (g *Game) Init() error {
g.lives = 5
if screen, err := tcell.NewScreen(); err != nil {
return err
} else if err = screen.Init(); err != nil {
return err
} else {
screen.SetStyle(tcell.StyleDefault.
Background(tcell.ColorBlack).
Foreground(tcell.ColorWhite))
g.screen = screen
}
// XXX: Add a main screen
g.screen.EnableMouse()
g.level = GetLevel("level1")
if g.level == nil {
g.screen.Fini()
return errors.New("Cannot find data (did you run rebuild.sh?)")
}
g.lview = views.NewViewPort(g.screen, 0, 1, -1, -1)
g.level.SetView(g.lview)
g.level.SetGame(g)
g.sview = views.NewViewPort(g.screen, 0, 0, -1, 1)
g.sbar = views.NewTextBar()
g.sbar.SetView(g.sview)
g.quitq = make(chan struct{})
g.eventq = make(chan tcell.Event)
g.level.Reset()
g.level.ShowPress()
RegisterFallbacks(g.screen)
return nil
}
func (g *Game) Quit() {
g.quitone.Do(func() {
close(g.quitq)
})
}
func (g *Game) Draw() {
g.Lock()
sbnorm := tcell.StyleDefault.
Background(tcell.ColorAqua).
Foreground(tcell.ColorBlack)
sbwarn := tcell.StyleDefault.
Background(tcell.ColorAqua).
Foreground(tcell.ColorRed)
sbalert := tcell.StyleDefault.
Background(tcell.ColorRed).
Foreground(tcell.ColorWhite)
g.sbar.SetStyle(sbnorm)
timer := g.level.GetTimer()
times := fmt.Sprintf("%02d:%02d.%1d",
timer/time.Minute,
(timer%time.Minute)/time.Second,
timer%time.Second/(100*time.Millisecond))
g.sbar.SetCenter(g.level.Title(), sbnorm)
if timer < 30*time.Second {
g.sbar.SetLeft(times, sbwarn)
if timer < 10*time.Second {
g.sbar.SetCenter(
fmt.Sprintf("-=: BLASTWAVE IN %s :=-",
times), sbalert)
}
} else {
g.sbar.SetLeft(times, sbnorm)
}
if g.gameover {
g.sbar.SetCenter("-=: G A M E O V E R :=- ", sbwarn)
}
right := ""
if g.lives > 5 {
right += fmt.Sprintf("%d x ♥", g.lives)
} else {
for i := 0; i < g.lives; i++ {
right += " ♥"
}
}
right += " "
g.sbar.SetRight(right, sbwarn)
g.level.Draw(g.lview)
g.sbar.Draw()
g.screen.Show()
g.Unlock()
}
func (g *Game) Run() error {
go g.EventPoller()
go g.Updater()
loop:
for {
g.Draw()
select {
case <-g.quitq:
break loop
case <-time.After(time.Millisecond * 10):
case ev := <-g.eventq:
g.HandleEvent(ev)
}
}
// Inject a wakeup interrupt
iev := tcell.NewEventInterrupt(nil)
g.screen.PostEvent(iev)
g.screen.Fini()
// wait for updaters to finish
if g.errmsg != "" {
return errors.New(g.errmsg)
}
return nil
}
func (g *Game) Error(msg string) {
g.errmsg = msg
g.Quit()
}
func (g *Game) HandleEvent(ev tcell.Event) bool {
switch ev := ev.(type) {
case *tcell.EventResize:
g.lview.Resize(0, 1, -1, -1)
g.sview.Resize(0, 0, -1, 1)
g.level.HandleEvent(ev)
case *tcell.EventKey:
if ev.Key() == tcell.KeyEscape {
g.Quit()
return true
}
if !g.started {
if ev.Key() == tcell.KeyEnter {
if g.gameover {
g.lives = 5
}
g.level.Reset()
g.level.Start()
g.started = true
g.gameover = false
}
// eat all keys until level starts
return true
}
case *EventPlayerDeath:
g.lives--
g.started = false
if g.lives == 0 {
g.gameover = true
g.level.HandleEvent(&EventGameOver{})
}
return true
case *EventLevelComplete:
g.lives++ // bonus life (for now)
g.started = false
return true
}
if !g.level.HandleEvent(ev) {
return true
}
return true
}
func (g *Game) Updater() {
for {
select {
case <-g.quitq:
return
case <-time.After(time.Millisecond * 10):
g.Lock()
g.level.Update(time.Now())
g.Unlock()
}
}
}
func (g *Game) EventPoller() {
for {
select {
case <-g.quitq:
return
default:
}
ev := g.screen.PollEvent()
if ev == nil {
return
}
select {
case <-g.quitq:
return
case g.eventq <- ev:
}
}
}