doodle/pkg/modal/end_level.go
Noah Petherbridge 672ee9641a Savegame and High Scores
* Adds pkg/savegame to store user progress thru Level Packs.
* The savegame.json is mildly tamper resistant by including a checksum
  along with the JSON body.
* The checksum combines the JSON string + an app secret (in savegame.go)
  + user specific entropy (stored in their settings.json). If the user
  modifies their save file and the checksum becomes invalid the game
  will not load the save file, acting like it didn't exist, resetting
  all their high scores.

Updates to the Story Mode window:

* On the LevelPacks list: shows e.g. "[completed 0 of 3 levels]" showing
  a user's progress thru the level pack.
* Below the levels on the Detail screen:
  * Shows an indicator whether the level is completed or not.
  * Shows high scores (fastest times beating the level)
  * Shows a padlock icon if levels are locked and the player hasn't
    reached them yet. Pops up an Alert modal if a locked level is
    clicked on.

Scoring is based around your fastest time elapsed to finish the level.

* Perfect Time (gold coin): player has not died during the level.
* Best Time (silver coin): player has continued from a checkpoint.

In-game an elapsed timer is shown in the top left corner along with the
gold or silver coin indicating if your run has been Perfect.

If the user enters any Cheat Codes during gameplay they are not eligible
to win a high score, but the level will still be marked as completed.
The icon next to the in-game timer disappears when a cheat code has been
entered.
2022-01-02 16:28:43 -08:00

213 lines
4.2 KiB
Go

package modal
import (
"fmt"
"time"
"git.kirsle.net/apps/doodle/pkg/balance"
"git.kirsle.net/apps/doodle/pkg/log"
"git.kirsle.net/apps/doodle/pkg/savegame"
"git.kirsle.net/apps/doodle/pkg/sprites"
"git.kirsle.net/go/render"
"git.kirsle.net/go/ui"
)
// ConfigEndLevel sets options for the EndLevel modal.
type ConfigEndLevel struct {
Engine render.Engine
Success bool // false = failure condition
// Handler functions - what you don't define will not
// show as buttons in the modal.
OnRestartLevel func() // Restart Level
OnRetryCheckpoint func() // Continue from checkpoint
OnEditLevel func()
OnNextLevel func() // Next Level
OnExitToMenu func() // Exit to Menu
// Set these values to show the "New Record!" part of the modal.
NewRecord bool
IsPerfect bool
TimeElapsed time.Duration
}
// EndLevel shows the End Level modal.
func EndLevel(cfg ConfigEndLevel, title, message string, args ...interface{}) *Modal {
if !ready {
panic("modal.EndLevel(): not ready")
} else if current != nil {
return current
}
// Reset the supervisor.
supervisor = ui.NewSupervisor()
m := &Modal{
title: title,
message: fmt.Sprintf(message, args...),
}
m.window = makeEndLevel(m, cfg)
center(m.window)
current = m
return m
}
// makeEndLevel creates the ui.Window for the Confirm modal.
func makeEndLevel(m *Modal, cfg ConfigEndLevel) *ui.Window {
win := ui.NewWindow("EndLevel")
_, title := win.TitleBar()
title.TextVariable = &m.title
msgFrame := ui.NewFrame("Confirm Message")
win.Pack(msgFrame, ui.Pack{
Side: ui.N,
})
msg := ui.NewLabel(ui.Label{
TextVariable: &m.message,
Font: balance.UIFont,
})
msgFrame.Pack(msg, ui.Pack{
Side: ui.N,
})
// New Record frame.
if cfg.NewRecord {
// Get the gold or silver sprite.
var (
coin = balance.SilverCoin
recordFont = balance.NewRecordFont
)
if cfg.IsPerfect {
coin = balance.GoldCoin
recordFont = balance.NewRecordPerfectFont
}
recordFrame := ui.NewFrame("New Record")
msgFrame.Pack(recordFrame, ui.Pack{
Side: ui.N,
})
header := ui.NewLabel(ui.Label{
Text: "A New Record!",
Font: recordFont,
})
recordFrame.Pack(header, ui.Pack{
Side: ui.N,
FillX: true,
})
// A frame to hold the icon and duration elapsed.
timeFrame := ui.NewFrame("Time Frame")
recordFrame.Pack(timeFrame, ui.Pack{
Side: ui.N,
})
// Show the coin image.
if cfg.Engine != nil {
img, err := sprites.LoadImage(cfg.Engine, coin)
if err != nil {
log.Error("Couldn't load %s: %s", coin, err)
} else {
timeFrame.Pack(img, ui.Pack{
Side: ui.W,
})
}
}
// Show the time duration label.
dur := ui.NewLabel(ui.Label{
Text: savegame.FormatDuration(cfg.TimeElapsed),
Font: balance.MenuFont,
})
timeFrame.Pack(dur, ui.Pack{
Side: ui.W,
})
}
// Ok/Cancel button bar.
btnBar := ui.NewFrame("Button Bar")
msgFrame.Pack(btnBar, ui.Pack{
Side: ui.N,
PadY: 4,
})
var buttons []*ui.Button
var primaryFunc func()
for _, btn := range []struct {
Label string
F func()
}{
{
Label: "Next Level",
F: cfg.OnNextLevel,
},
{
Label: "Retry from Checkpoint",
F: cfg.OnRetryCheckpoint,
},
{
Label: "Restart Level",
F: cfg.OnRestartLevel,
},
{
Label: "Edit Level",
F: cfg.OnEditLevel,
},
{
Label: "Exit to Menu",
F: cfg.OnExitToMenu,
},
} {
btn := btn
if btn.F == nil {
continue
}
if primaryFunc == nil {
primaryFunc = btn.F
}
button := ui.NewButton(btn.Label+"Button", ui.NewLabel(ui.Label{
Text: btn.Label,
Font: balance.MenuFont,
}))
button.Handle(ui.Click, func(ed ui.EventData) error {
btn.F()
m.Dismiss(false)
return nil
})
button.Compute(engine)
buttons = append(buttons, button)
supervisor.Add(button)
btnBar.Pack(button, ui.Pack{
Side: ui.N,
PadY: 2,
FillX: true,
})
// // Make a new row of buttons?
// if i > 0 && i%3 == 0 {
// btnBar = ui.NewFrame("Button Bar")
// msgFrame.Pack(btnBar, ui.Pack{
// Side: ui.N,
// PadY: 0,
// })
// }
}
// Mark the first button the primary button.
if primaryFunc != nil {
m.Then(primaryFunc)
}
buttons[0].SetStyle(&balance.ButtonPrimary)
win.Compute(engine)
win.Supervise(supervisor)
return win
}