-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstate.go
714 lines (638 loc) · 20.2 KB
/
state.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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
package main
import (
"embed"
"encoding/json"
"fmt"
"image"
"log"
"math"
"strconv"
"strings"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/inpututil"
"github.com/tinne26/etxt"
)
// Load is a Game State for loading the game
type Load struct {
splash []*ebiten.Image
curr int
}
// Update changes Game State to Title after 200 ticks
func (l *Load) Update(g *Game) error {
if loaded == false {
loadFonts()
g.txtRenderer = newRenderer()
//loadMenuItems = findSaveFiles()
initializeMenus()
initializeTreasures()
t := NewTitle()
g.state["Title"] = t
loaded = true
}
if inpututil.IsKeyJustPressed(ebiten.KeyF) { // developer skip-ahead
g.mode = "Title"
}
if g.count > 200 {
g.mode = "Title"
log.Printf("Changing state to Title")
}
return nil
}
// Draw displays splash screens during game load
func (l *Load) Draw(screen *ebiten.Image, g *Game) {
op := &ebiten.DrawImageOptions{}
screen.DrawImage(l.splash[l.curr], op)
}
// Title is a Game State containing Title Screen and a Menu
type Title struct {
header string
menu *Menu
}
// NewTitle creates a new *Title with default main menu
func NewTitle() *Title {
title := &Title{
header: mainHeader,
menu: mainMenu,
}
return title
}
// Load loads a new Menu into *Title
func (t *Title) Load(m *Menu) {
t.menu = m
}
// Update changes active selection and selects a MenuItem based on user input
func (t *Title) Update(g *Game) error {
if inpututil.IsKeyJustPressed(ebiten.KeyEnter) {
selection := t.menu.Select()
switch {
case selection == "New Game":
log.Printf("Starting New Game")
// prompt for character name
// create character with provided name
playerView = NewViewer()
worldPlayerView = NewViewer()
playerChar = NewCharacter("Mona", spriteSheet, playerView, 100)
worldPlayer = NewWorldChar(spriteSheet, worldPlayerView)
g.score = 0
g.count = 0
// loadLevels()
world := NewWorld()
world.Load(FileSystem)
g.state["World"] = world
g.mode = "World"
// saveData := NewSaveData()
// saveData.Initialize("Mona")
case selection == "Load Game":
log.Printf("Choose a Saved Game")
if len(loadMenuItems) > 1 {
t := NewTitle()
t.Load(loadMenu)
t.header = loadHeader
g.state["Title"] = t
}
g.mode = "Title"
case selection == "How To Play":
//TODO
log.Printf("Display Instructions -- not yet implemented")
g.mode = "Title"
case selection == "Acknowledgements":
c := NewInfo()
c.message = infoCredit
c.previous = "Title"
g.state["Info"] = c
g.mode = "Info"
case selection == "Exit":
log.Printf("Attempting to Exit Game")
return ErrExit
case strings.HasSuffix(selection, ".json"):
gameData := LoadGame(selection)
playerView = NewViewer()
worldPlayerView = NewViewer()
worldPlayerView.xCoord = gameData.WorldViewX
worldPlayerView.yCoord = gameData.WorldViewY
playerChar = NewCharacter(gameData.Name, spriteSheet, playerView, 100)
playerChar.lives = gameData.Lives
worldPlayer = NewWorldChar(spriteSheet, worldPlayerView)
worldPlayer.xCoord = gameData.WorldCharX
worldPlayer.yCoord = gameData.WorldCharY
g.score = gameData.Score
g.count = gameData.Count
world := NewWorld()
world.Load(FileSystem)
for _, level := range world.levels {
if gameData.Complete[level.Name] {
level.Complete = true
}
}
g.state["World"] = world
g.mode = "World"
/*
world.Load()
*/
case selection == "Main Menu":
t := NewTitle()
t.Load(mainMenu)
t.header = mainHeader
g.state["Title"] = t
g.mode = "Title"
}
}
if inpututil.IsKeyJustPressed(ebiten.KeyArrowDown) {
t.menu.Next()
}
if inpututil.IsKeyJustPressed(ebiten.KeyArrowUp) {
t.menu.Prev()
}
return nil
}
// Draw displays Title Screen and Menu, highlighting active selection
func (t *Title) Draw(screen *ebiten.Image, g *Game) {
textColor = menuColorActive
g.txtRenderer.SetAlign(etxt.YCenter, etxt.XCenter) // make sure type is centered (gets changed in Play/Pause)
g.txtRenderer.SetTarget(screen)
locY := 80
g.txtRenderer.SetColor(menuColorInactive)
g.txtRenderer.Draw(t.header, 300, locY)
var menuHead = t.menu.head
locY = 150
for i := t.menu.length; i > 0; i-- {
textColor = menuColorInactive
if menuHead == t.menu.active {
textColor = menuColorActive
}
if menuHead == t.menu.active &&
((t.menu.active.option == "Load Game" && len(loadMenuItems) < 2) || t.menu.active.option == "How To Play") {
textColor = menuColorDisabled
}
g.txtRenderer.SetColor(textColor)
g.txtRenderer.Draw(menuHead.option, 300, locY)
locY += 50
menuHead = menuHead.next
}
}
// World is a Game State that holds all level data for active game
type World struct {
menu *Menu
levels []*LevelData
}
// NewWorld creates a new World with all levels not yet completed
func NewWorld() *World {
world := &World{
menu: worldMenu,
}
world.Load(FileSystem)
return world
}
// Load loads all default level data into World
func (w *World) Load(fs embed.FS) {
var levels []*LevelData
lvlContent, err := fs.ReadFile("levels.json")
if err != nil {
log.Fatal("Error when opening file: ", err)
}
err = json.Unmarshal(lvlContent, &levels)
if err != nil {
log.Fatal("Error during Unmarshalling: ", err)
}
for _, l := range levels {
l.icon = levelImages[l.Name][0]
l.iconComplete = levelImages[l.Name][1]
l.background = levelImages[l.Name][2]
}
w.levels = levels
}
// Update changes player location/worldview offset and changes state to Play based on user input
func (w *World) Update(g *Game) error {
/*
if inpututil.IsKeyJustPressed(ebiten.KeyS) {
saveData := NewSaveData(g)
log.Printf("Name is " + saveData.Name)
log.Printf(strconv.Itoa(saveData.Lives))
saveData.Save(g, playerChar, worldPlayer)
}
*/
// set worldPlayer location and view screen: this should go in Menu->Start New Game
if ebiten.IsKeyPressed(ebiten.KeyEscape) {
// later, ask confirmation if game not saved since entering World
// Options: Save, Quit without Saving
log.Printf("Exiting Game")
return ErrExit
}
// radiusCheck is making sure worldPlayer stays within movement radius of planet
radiusCheck := math.Sqrt(math.Pow(float64(worldPlayer.xCoord-500-worldPlayer.view.xCoord), 2) + math.Pow(float64(worldPlayer.yCoord-500-worldPlayer.view.yCoord), 2))
// 4 directions of worldPlayer movement checks
if ebiten.IsKeyPressed(ebiten.KeyArrowRight) {
worldPlayer.navRight(radiusCheck)
}
if ebiten.IsKeyPressed(ebiten.KeyArrowLeft) {
worldPlayer.navLeft(radiusCheck)
}
if ebiten.IsKeyPressed(ebiten.KeyArrowUp) {
worldPlayer.navUp(radiusCheck)
}
if ebiten.IsKeyPressed(ebiten.KeyArrowDown) {
worldPlayer.navDown(radiusCheck)
}
worldPlayerBox := image.Rect(worldPlayer.xCoord, worldPlayer.yCoord, worldPlayer.xCoord+worldCharWidth, worldPlayer.yCoord+worldCharHeight)
// locations of levels on World, checking whether conditions are met to enter the level
for _, l := range w.levels {
if worldPlayerBox.Overlaps(image.Rect(l.WorldX+worldPlayer.view.xCoord, l.WorldY+worldPlayer.view.yCoord, l.WorldX+worldPlayer.view.xCoord+150, l.WorldY+worldPlayer.view.yCoord+150)) &&
ebiten.IsKeyPressed(ebiten.KeyEnter) &&
l.Complete == false {
levelWidth, levelHeight = l.background.Size()
playerChar.resetView()
playerChar.setLocation(l.PlayerX, l.PlayerY)
playerChar.hpCurrent = playerChar.hpTotal
levelSetup(l, playerChar.view.xCoord, playerChar.view.yCoord)
playLevel := NewPlay(l)
g.state["Play"] = playLevel
pauseEntry := NewPause("message", l.Message[0])
g.state["Pause"] = pauseEntry
g.mode = "Pause"
}
}
return nil
}
// Draw displays player on World map
func (w *World) Draw(screen *ebiten.Image, g *Game) {
op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(float64(worldPlayer.view.xCoord), float64(worldPlayer.view.yCoord))
screen.DrawImage(world, op)
for _, l := range w.levels {
levelIcon := l.icon
if l.Complete == true {
levelIcon = l.iconComplete
}
lop := &ebiten.DrawImageOptions{}
lop.GeoM.Translate(float64(l.WorldX), float64(l.WorldY))
world.DrawImage(levelIcon, lop)
}
op = &ebiten.DrawImageOptions{}
op.GeoM.Translate(float64(worldPlayer.xCoord), float64(worldPlayer.yCoord))
screen.DrawImage(worldPlayer.sprite.SubImage(image.Rect(0, 0, 50, 50)).(*ebiten.Image), op)
}
// Play contains data for active level
type Play struct {
level *LevelData
gem bool
}
// NewPlay creates new Play for a given level on entry
func NewPlay(l *LevelData) *Play {
play := &Play{
level: l,
}
return play
}
// Update is the main gameplay function. Changes score, player health/lives based on user input and collisions
func (p *Play) Update(g *Game) error {
// sprite frames for different things -- handle differently later
portalFrame = (g.count / 5) % portalFrameCount
hazardFrame = (g.count / 5) % hazardFrameCount
creatureFrame = (g.count / 5) % creatureFrameCount
treasureTypeList[3].frame = (g.count / 5) % treasureTypeList[3].frameCt
treasureTypeList[4].frame = (g.count / 5) % treasureTypeList[4].frameCt
// player sprite frame reset
if inpututil.IsKeyJustReleased(ebiten.KeyArrowRight) || inpututil.IsKeyJustReleased(ebiten.KeyArrowLeft) {
currentFrame = defaultFrame
}
baseView := [2]int{playerChar.view.xCoord, playerChar.view.yCoord}
// 2 direction movement
if ebiten.IsKeyPressed(ebiten.KeyArrowRight) {
currentFrame = (g.count / 5) % frameCount
playerChar.moveRight()
}
if ebiten.IsKeyPressed(ebiten.KeyArrowLeft) {
currentFrame = (g.count / 5) % frameCount
playerChar.moveLeft()
}
// if view x changed, update x location of on-screen objects
if baseView[0] != playerChar.view.xCoord {
delta := playerChar.view.xCoord - baseView[0]
for _, h := range hazardList {
h.xCoord += delta
}
for _, c := range creatureList {
c.xCoord += delta
}
for _, t := range treasureList {
t.xCoord += delta
}
}
// jump logic
// if inpututil.IsKeyJustPressed(ebiten.KeySpace) && playerChar.yVelo == gravity { // && a pixel beneath playerChar is enviroBlock
// playerChar.yVelo = -gravity
// }
if ebiten.IsKeyPressed(ebiten.KeySpace) {
log.Printf("KeyPress Duration: %d", inpututil.KeyPressDuration(ebiten.KeySpace))
log.Printf("Character status: %s", playerChar.status)
playerChar.jump(inpututil.KeyPressDuration(ebiten.KeySpace))
}
if playerChar.yVelo < gravity {
// screen movement vs player movement
if (playerChar.yCoord < 160 && playerChar.view.yCoord-playerChar.yVelo < 0 && playerChar.yVelo < 0) ||
(playerChar.yCoord > 160 && playerChar.view.yCoord-playerChar.yVelo > -120 && playerChar.yVelo > 0) {
playerChar.view.yCoord -= playerChar.yVelo
for _, h := range hazardList {
h.yCoord -= playerChar.yVelo
}
for _, c := range creatureList {
c.yCoord -= playerChar.yVelo
}
for _, t := range treasureList {
t.yCoord -= playerChar.yVelo
}
} else {
playerChar.yCoord += playerChar.yVelo
}
playerChar.yVelo++
if playerChar.yVelo >= 0 {
playerCharBase := (playerChar.yCoord - playerChar.view.yCoord + playerCharHeight + 1) / 50 // checks immediately BELOW base of sprite
playerCharLeft := (playerChar.xCoord - playerChar.view.xCoord) / 50
playerCharRight := (playerChar.xCoord - playerChar.view.xCoord + playerCharWidth) / 50
if levelMap[0][(playerCharBase)*tileXCount+playerCharLeft] == 1 || levelMap[0][(playerCharBase)*tileXCount+playerCharRight] == 1 {
playerChar.yCoord = (playerCharBase * 50) - 50 + playerChar.view.yCoord
playerChar.yVelo = gravity
}
}
}
playerCharBase := (playerChar.yCoord - playerChar.view.yCoord + playerCharHeight + 1) / 50 // checks immediately BELOW base of sprite
playerCharLeft := (playerChar.xCoord - playerChar.view.xCoord) / 50
playerCharRight := (playerChar.xCoord - playerChar.view.xCoord + playerCharWidth) / 50
// gravity fixer
if playerChar.status != "ground" && levelMap[0][(playerCharBase*tileXCount)+playerCharLeft] != 1 && levelMap[0][(playerCharBase*tileXCount)+playerCharRight] != 1 {
switch {
case playerChar.view.yCoord > -120 && playerChar.yCoord > 160:
playerChar.view.yCoord -= 3
for _, h := range hazardList {
h.yCoord -= 3
}
for _, c := range creatureList {
c.yCoord -= 3
}
for _, t := range treasureList {
t.yCoord -= 3
}
default:
playerChar.yCoord += 3
}
}
playerCharFreshBase := (playerChar.yCoord - playerChar.view.yCoord + playerCharHeight + 1) / 50 // checks immediately BELOW base of sprite
if levelMap[0][(playerCharFreshBase*tileXCount)+playerCharLeft] == 1 || levelMap[0][(playerCharFreshBase*tileXCount)+playerCharRight] == 1 {
playerChar.status = "ground"
} else if playerChar.yVelo == gravity {
playerChar.status = "fall"
}
creatureMovement()
playerBox := image.Rect(playerChar.xCoord, playerChar.yCoord, playerChar.xCoord+playerCharWidth, playerChar.yCoord+playerCharWidth)
for i, t := range treasureList {
treasureBox := image.Rect(t.xCoord, t.yCoord, t.xCoord+50, t.yCoord+50)
if playerBox.Overlaps(treasureBox) {
g.score += t.value
if t.name == "Portal Gem" {
p.gem = true
}
treasureList = append(treasureList[0:i], treasureList[i+1:]...)
}
}
for _, h := range hazardList {
hazardBox := image.Rect(h.xCoord, h.yCoord, h.xCoord+50, h.yCoord+50)
if playerBox.Overlaps(hazardBox) {
playerChar.death()
g.mode = "Pause"
g.timer = 30
}
}
for _, c := range creatureList {
creatureBox := image.Rect(c.xCoord, c.yCoord, c.xCoord+50, c.yCoord+50)
if playerBox.Overlaps(creatureBox) {
playerChar.death()
g.mode = "Pause"
g.timer = 30
}
}
if p.gem &&
playerBox.Overlaps(image.Rect(p.level.ExitX+playerChar.view.xCoord, p.level.ExitY+playerChar.view.yCoord,
p.level.ExitX+portalWidth+playerChar.view.xCoord, p.level.ExitY+portalHeight+playerChar.view.yCoord)) {
p.level.Complete = true
p.gem = false
clearLevel()
log.Print("Just hit the portal")
//levelComplete()
log.Printf("Level complete")
g.mode = "World"
}
/*
if ebiten.IsKeyPressed(ebiten.KeyQ) {
g.state = "Pause"
}
*/
return nil
}
// Draw displays level game play
func (p *Play) Draw(screen *ebiten.Image, g *Game) {
lvlOp := &ebiten.DrawImageOptions{}
lvlOp.GeoM.Translate(float64(playerChar.view.xCoord), float64(playerChar.view.yCoord))
screen.DrawImage(p.level.background, lvlOp)
switch {
case playerChar.status == "dying":
mOp := &ebiten.DrawImageOptions{}
for i := 0; i < playerCharHeight; i += playerCharHeight / 8 {
wobble := 30 - g.timer
if i%12 == 0 {
wobble *= -1
}
mOp.GeoM.Reset()
mOp.GeoM.Translate(float64(playerChar.xCoord+wobble), float64(playerChar.yCoord+i))
cx, cy := currentFrame*playerCharWidth, playerChar.facing
screen.DrawImage(playerChar.sprite.SubImage(image.Rect(cx, cy+i, cx+playerCharWidth, cy+i+6)).(*ebiten.Image), mOp)
}
default:
mOp := &ebiten.DrawImageOptions{}
mOp.GeoM.Translate(float64(playerChar.xCoord), float64(playerChar.yCoord))
cx, cy := currentFrame*playerCharWidth, playerChar.facing
screen.DrawImage(playerChar.sprite.SubImage(image.Rect(cx, cy, cx+playerCharWidth, cy+playerCharHeight)).(*ebiten.Image), mOp)
}
for _, e := range enviroList {
op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(float64(e.xCoord), float64(e.yCoord))
p.level.background.DrawImage(e.sprite, op)
}
if p.gem == true {
top := &ebiten.DrawImageOptions{}
top.GeoM.Translate(float64(p.level.ExitX+playerChar.view.xCoord), float64(p.level.ExitY+playerChar.view.yCoord))
px := portalFrame * 100
screen.DrawImage(portal.SubImage(image.Rect(px, 0, px+100, 150)).(*ebiten.Image), top)
}
for _, h := range hazardList {
op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(float64(h.xCoord), float64(h.yCoord))
hx := hazardFrame * 50
screen.DrawImage(h.sprite.SubImage(image.Rect(hx, 0, hx+50, 50)).(*ebiten.Image), op)
}
for _, c := range creatureList {
op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(float64(c.xCoord), float64(c.yCoord))
cx, cy := creatureFrame*50, c.facing
screen.DrawImage(c.sprite.SubImage(image.Rect(cx, cy, cx+50, cy+50)).(*ebiten.Image), op)
}
for _, t := range treasureList {
xOffset := (blockHW - t.width) / 2
yOffset := (blockHW - t.height) / 2
op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(float64(t.xCoord+xOffset), float64(t.yCoord+yOffset))
tx := t.frame * t.width
screen.DrawImage(t.sprite.SubImage(image.Rect(tx, 0, tx+t.width, t.height)).(*ebiten.Image), op)
}
gx := 0
if p.gem == true {
gx = 35
}
op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(10.0, 10.0)
screen.DrawImage(statsBox, op)
op = &ebiten.DrawImageOptions{}
op.GeoM.Translate(125.0, 64.0)
screen.DrawImage(gemCt.SubImage(image.Rect(gx, 0, gx+35, 35)).(*ebiten.Image), op)
for lx := 0; lx < playerChar.lives-1; lx++ {
op = &ebiten.DrawImageOptions{}
op.GeoM.Translate(21.0+float64(lx*20), 64.0)
screen.DrawImage(livesCt, op)
}
pointsCt := strconv.Itoa(g.score)
g.txtRenderer.SetTarget(screen)
g.txtRenderer.SetColor(scoreDisplayColor)
g.txtRenderer.SetAlign(etxt.Top, etxt.Right)
g.txtRenderer.Draw(pointsCt, 160, 16)
if playerChar.status == "totally dead" {
overOp := &ebiten.DrawImageOptions{}
screen.DrawImage(gameOverMessage, overOp)
}
}
// Pause is a Game State that halts other game logic
type Pause struct {
mode string
message string
options *Menu
}
// NewPause creates new Pause struct
func NewPause(mod, msg string) *Pause {
p := &Pause{
mode: mod,
message: msg,
}
p.FormatMessage()
return p
}
// FormatMessage adds newlines to Pause.message to fit into messageBox
func (p *Pause) FormatMessage() {
maxLineLen := 20 // adjust based on txt size, msgbox width
if len(p.message) > maxLineLen {
words := strings.Fields(p.message)
lines := []string{""}
curr := 0
for _, w := range words {
if len(lines[curr])+len(w)+1 > maxLineLen {
lines[curr] = lines[curr][:len(lines[curr])-1]
curr++
lines = append(lines, "")
}
lines[curr] += w + " "
}
p.message = ""
for i, l := range lines {
p.message += l
if i != len(lines)-1 {
p.message += "\n"
}
}
}
}
// Update only updates playerChar status for death animation and Game Over screen
func (p *Pause) Update(g *Game) error {
switch {
case p.mode == "message":
if inpututil.IsKeyJustPressed(ebiten.KeyEnter) {
p.mode = ""
g.mode = "Play"
}
case playerChar.status == "totally dead":
if ebiten.IsKeyPressed(ebiten.KeyEnter) {
g.mode = "Title"
clearLevel()
playerChar.status = "ground"
}
case playerChar.status == "dying" && g.timer <= 0:
if playerChar.lives <= 0 {
playerChar.status = "totally dead"
}
if playerChar.lives > 0 {
clearLevel()
g.mode = "World"
}
case playerChar.status == "dying" && g.timer > 0:
g.timer--
default:
if ebiten.IsKeyPressed(ebiten.KeyEscape) {
g.mode = "Play"
}
}
return nil
}
// Draw isn't implemented yet
func (p *Pause) Draw(screen *ebiten.Image, g *Game) {
// TODO
// overlays, based on what the pause message and options are
switch {
case p.mode == "message":
// draw box image
boxW, boxH := messageBox.Size()
op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(float64((winWidth-boxW)/2), float64((winHeight-boxH)/2))
screen.DrawImage(messageBox, op)
// draw etxt story message
g.txtRenderer.SetAlign(etxt.YCenter, etxt.XCenter)
g.txtRenderer.SetSizePx(28)
g.txtRenderer.SetTarget(screen)
g.txtRenderer.SetColor(messageBoxColor)
g.txtRenderer.Draw(p.message, winWidth/2, winHeight/2)
// draw menu buttons (define these in Update)
}
}
// Info is currently for Acknowledgement page information, holds previous game state
type Info struct {
message []string
previous string
}
// NewInfo creates new Info struct
func NewInfo() *Info {
info := &Info{}
return info
}
// Update returns to previous game state on [Enter]
func (i *Info) Update(g *Game) error {
if inpututil.IsKeyJustPressed(ebiten.KeyEnter) {
g.mode = i.previous
}
if inpututil.IsKeyJustPressed(ebiten.KeySpace) {
fmt.Println(i.previous)
}
return nil
}
// Draw draws Acknowledgements to screen
func (i *Info) Draw(screen *ebiten.Image, g *Game) {
locY := 100
g.txtRenderer.SetAlign(etxt.YCenter, etxt.XCenter)
g.txtRenderer.SetTarget(screen)
g.txtRenderer.SetColor(menuColorInactive)
g.txtRenderer.Draw("Acknowledgements", winWidth/2, locY)
locY += 100
g.txtRenderer.SetSizePx(18)
for _, m := range i.message {
g.txtRenderer.Draw(m, winWidth/2, locY)
locY += 75
}
locY += 50
g.txtRenderer.SetSizePx(32)
g.txtRenderer.SetColor(menuColorActive)
g.txtRenderer.Draw("Main Menu", winWidth/2, locY)
}