-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
370 lines (291 loc) · 6.2 KB
/
main.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
package main
import (
"fmt"
"log"
"os"
"strings"
"time"
tea "github.com/charmbracelet/bubbletea"
)
type position struct {
x int
y int
}
type tiles struct {
player string
tombstone string
ghost string
door string
}
// model is the game model use with bubbletea.
type model struct {
gameTiles tiles
direction string
username string
levels []level
playerPos position
levelIdx int
termHeight int
termWidth int
playerCoolDown int
windowTooSmall bool
gameWon bool
isGameOver bool
isPaused bool
hasStarted bool
playerHasKey bool
useNerdFont bool
isDemo bool
}
func (m model) level() level {
// don't panic if this is called
// when it should not be
if m.levelIdx >= len(m.levels) {
return m.levels[0]
}
return m.levels[m.levelIdx]
}
type tombstone struct {
hasKey bool
checked bool
}
type nothing struct{}
type tickMsg nothing
func main() {
// flags? no.
// any args, show help and quit
if len(os.Args) > 1 {
help()
return
}
username := strings.TrimSpace(os.Getenv("USER"))
if username == "" {
username = "You"
}
isDemo := false
demoEnvVar := strings.TrimSpace(os.Getenv("DEMO"))
if demoEnvVar != "" {
isDemo = true
}
levels := makeLevels(isDemo)
useNerdFont := true
initialModel := model{
username: username,
levels: levels,
levelIdx: 0,
playerPos: levels[0].playerStartPos,
gameTiles: loadTiles(useNerdFont),
useNerdFont: useNerdFont,
isDemo: isDemo,
}
p := tea.NewProgram(initialModel, tea.WithAltScreen())
if _, err := p.Run(); err != nil {
log.Fatal(err)
}
insanelyCleverFarewellMessage := "Ghoul-bye!"
fmt.Print("\n" + insanelyCleverFarewellMessage + "\n")
}
func doTick() tea.Cmd {
cmd := func(t time.Time) tea.Msg {
return tickMsg(nothing{})
}
return tea.Tick(time.Millisecond*10, cmd)
}
func (m model) Init() tea.Cmd {
return nil
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
if !m.hasStarted {
switch msg.String() {
case "q", "ctrl+c", "esc":
return m, tea.Quit
case "f":
m = switchTiles(m)
return m, nil
}
m.hasStarted = true
return m, doTick()
}
if m.gameWon {
switch msg.String() {
case "q", "ctrl+c", "esc":
return m, tea.Quit
}
return m, nil
}
if m.isGameOver {
switch msg.String() {
case "q", "ctrl+c", "esc":
return m, tea.Quit
case "a":
m = m.restartLevel()
return m, doTick()
}
return m, nil
}
switch msg.String() {
case "q", "ctrl+c", "esc":
return m, tea.Quit
case "f":
m = switchTiles(m)
return m, nil
case "up", "left", "down", "right", " ":
m = setDirection(msg.String(), m)
return m, nil
case "p":
m.isPaused = !m.isPaused
if !m.isPaused {
return m, doTick()
}
return m, nil
default:
return m, nil
}
case tickMsg:
if m.gameWon || m.isGameOver || m.isPaused || !m.hasStarted {
return m, nil
}
m = onTick(m)
return m, doTick()
case tea.WindowSizeMsg:
m.termWidth = msg.Width
m.termHeight = msg.Height
m.windowTooSmall = msg.Height < minWindowHeight || msg.Width < minWindowWidth
if m.hasStarted && m.windowTooSmall {
m.isPaused = true
}
return m, nil
}
return m, nil
}
func (m model) restartLevel() model {
m.isGameOver = false
m.isPaused = false
m.hasStarted = true
m.levels = makeLevels(m.isDemo)
m.playerPos = m.level().playerStartPos
m.direction = ""
return m
}
func playerCanMove(direction string, m model) bool {
if m.playerCoolDown > 0 || m.gameWon || m.isGameOver || m.isPaused {
return false
}
return !isBlocked(direction, m)
}
func afterPlayerMove(m model) model {
m.playerCoolDown = 7
isNextToDoor := isAdjacent(m.level().door.x, m.level().door.y, m.playerPos.x, m.playerPos.y)
if m.playerHasKey && isNextToDoor {
m.levelIdx++
if m.levelIdx == len(m.levels) {
m.gameWon = true
return m
}
m.playerPos = m.level().playerStartPos
m.playerHasKey = false
m.direction = ""
return m
}
for stonePos, stone := range m.level().tombstoneMap {
if isAdjacent(stonePos.x, stonePos.y, m.playerPos.x, m.playerPos.y) {
stone.checked = true
if stone.hasKey {
m.playerHasKey = true
}
}
}
return m
}
func onTick(m model) model {
if isBlocked(m.direction, m) {
m.direction = ""
}
if m.direction != "" && playerCanMove(m.direction, m) {
switch m.direction {
case "up":
m.playerPos.y--
case "down":
m.playerPos.y++
case "left":
m.playerPos.x--
case "right":
m.playerPos.x++
}
m = afterPlayerMove(m)
}
m.playerCoolDown--
// create a new map so we don't wipe out an existing ghost
// when moving another ghost
newGhostMap := map[position]*ghost{}
for currPoint, g := range m.level().ghostMap {
if g.cooldown > 0 {
g.cooldown--
newGhostMap[currPoint] = g
continue
}
if len(g.path) == 0 {
g.path = findPathForGhost(currPoint, *g, m)
g.cooldown = 50
if g.kind == "hunt" {
g.cooldown = 0
}
newGhostMap[currPoint] = g
continue
}
nextPoint := g.path[len(g.path)-1]
g.path = g.path[:len(g.path)-1]
if !ghostCanMove(nextPoint, g, newGhostMap, m) {
g.path = []position{}
newGhostMap[currPoint] = g
continue
}
g.cooldown = 12
if g.kind == "hunt" {
g.cooldown = 8
}
newGhostMap[nextPoint] = g
}
if m.levelIdx < len(m.levels) {
m.levels[m.levelIdx].ghostMap = newGhostMap
}
playerOnGhost := m.level().ghostMap[m.playerPos] != nil
if playerOnGhost {
m.isGameOver = true
}
return m
}
func setDirection(direction string, m model) model {
if m.direction == "space" {
m.direction = ""
return m
}
m.direction = direction
return m
}
func isBlocked(direction string, m model) bool {
switch direction {
case "up":
m.playerPos.y--
case "down":
m.playerPos.y++
case "left":
m.playerPos.x--
case "right":
m.playerPos.x++
}
if t := m.level().tombstoneMap[m.playerPos]; t != nil {
return true
}
playerInBounds := m.playerPos.y > 0 && m.playerPos.y < m.level().height-1 &&
m.playerPos.x > 0 &&
m.playerPos.x < m.level().width-1
return !playerInBounds
}
func switchTiles(m model) model {
m.useNerdFont = !m.useNerdFont
m.gameTiles = loadTiles(m.useNerdFont)
return m
}