Saving lives left for resume as well.

This commit is contained in:
Sander Schobers 2021-08-12 22:35:33 +02:00
parent e3527eb580
commit 3c99e5881b
4 changed files with 19 additions and 7 deletions

View File

@ -11,4 +11,4 @@
- [ ] Fix z-fighting of monsters.
- [ ] Add exploding animation of monsters.
- [ ] Add audio settings (music & sound volume).
- [ ] Hearts must be saved as well for resume.
- [X] Hearts must be saved as well for resume.

View File

@ -54,7 +54,7 @@ func (app *appContext) Play(ctx ui.Context) {
level := tins2021.NewLevel()
level.Randomize(0, numberOfStars)
app.Score.Current = tins2021.Score{}
app.ResetCurrentScore()
app.show(ctx, newLevelControl(app, ctx, level))
}
@ -74,11 +74,22 @@ func (app *appContext) PlayNext(ctx ui.Context, controller *levelController) {
func (app *appContext) PlayResume(ctx ui.Context) {
level := tins2021.NewLevel()
level.Score = app.Score.Current.Score
level.Lives = app.Score.CurrentLives
level.Randomize(app.Score.Current.Difficulty, numberOfStars)
app.show(ctx, newLevelControl(app, ctx, level))
}
func (app *appContext) ResetCurrentScore() {
app.Score.Current = tins2021.Score{}
app.Score.CurrentLives = 0
}
func (app *appContext) SetCurrentScore(level *tins2021.Level) {
app.Score.Current = tins2021.NewScore(level.Score, level.Difficulty+1)
app.Score.CurrentLives = level.Lives
}
func (app *appContext) show(ctx ui.Context, control ui.Control) {
app.setView(control)
if _, ok := control.(*levelController); ok {

View File

@ -91,7 +91,7 @@ func (r *levelController) updateHighscore() bool {
if highscore {
r.app.Score.Highscores = highscores
}
r.app.Score.Current = tins2021.Score{} // reset score
r.app.ResetCurrentScore()
return highscore
}
@ -101,7 +101,7 @@ func (r *levelController) Handle(ctx ui.Context, e ui.Event) bool {
switch e.Key {
case ui.KeyEscape:
if r.Level.StarsCollected == r.Level.Stars {
r.app.Score.Current = tins2021.NewScore(r.Level.Score, r.Level.Difficulty+1)
r.app.SetCurrentScore(r.Level)
}
r.app.ShowMainMenu(ctx)
}
@ -116,7 +116,7 @@ func (r *levelController) Handle(ctx ui.Context, e ui.Event) bool {
case *ui.KeyDownEvent:
switch e.Key {
case ui.KeyEnter:
r.app.Score.Current = tins2021.NewScore(r.Level.Score, r.Level.Difficulty+1)
r.app.SetCurrentScore(r.Level)
r.app.PlayNext(ctx, r)
}
}

View File

@ -57,8 +57,9 @@ func (s *Score) Validate() bool {
}
type ScoreState struct {
Current Score
Highscores Highscores
Current Score
CurrentLives int
Highscores Highscores
}
func LoadScores() (ScoreState, error) {