Added game over.

This commit is contained in:
Sander Schobers 2021-08-09 11:29:10 +02:00
parent 801eefec52
commit 96ab55e62a
2 changed files with 56 additions and 6 deletions

View File

@ -10,6 +10,7 @@ import (
"opslag.de/schobers/geom" "opslag.de/schobers/geom"
"opslag.de/schobers/tins2021" "opslag.de/schobers/tins2021"
"opslag.de/schobers/zntg"
"opslag.de/schobers/zntg/ui" "opslag.de/schobers/zntg/ui"
) )
@ -89,6 +90,18 @@ func IsModifierPressed(modifiers ui.KeyModifier, pressed ui.KeyModifier) bool {
} }
func (r levelController) Handle(ctx ui.Context, e ui.Event) bool { func (r levelController) Handle(ctx ui.Context, e ui.Event) bool {
switch e := e.(type) {
case *ui.KeyDownEvent:
switch e.Key {
case ui.KeyEscape:
r.app.ShowMainMenu(ctx)
}
}
if r.Level.GameOver {
return false
}
switch e := e.(type) { switch e := e.(type) {
case *ui.KeyDownEvent: case *ui.KeyDownEvent:
switch e.Key { switch e.Key {
@ -100,10 +113,9 @@ func (r levelController) Handle(ctx ui.Context, e ui.Event) bool {
r.Level.MovePlayer(tins2021.DirectionDownRight) r.Level.MovePlayer(tins2021.DirectionDownRight)
case ui.KeyA: case ui.KeyA:
r.Level.MovePlayer(tins2021.DirectionDownLeft) r.Level.MovePlayer(tins2021.DirectionDownLeft)
case ui.KeyEscape:
r.app.ShowMainMenu(ctx)
} }
} }
for _, animations := range r.Animations { for _, animations := range r.Animations {
animations.Update() animations.Update()
} }
@ -111,14 +123,22 @@ func (r levelController) Handle(ctx ui.Context, e ui.Event) bool {
r.MovingMonsters.Update() r.MovingMonsters.Update()
var jumped []geom.Point var jumped []geom.Point
for pos, animation := range r.MovingMonsters.Values { for pos, animation := range r.MovingMonsters.Values {
if animation.Frame < 20 { if animation.Frame < 15 {
continue continue
} }
target := r.Level.MonsterTargets[pos] target := r.Level.MonsterTargets[pos]
r.Level.Monsters[target] = r.Level.Monsters[pos] if target == r.Level.Player { // player is hit
delete(r.Level.MonsterTargets, pos) r.Level.DestroyMonster(pos)
delete(r.Level.Monsters, pos) jumped = append(jumped, pos)
r.Level.DecrementLive()
continue
}
if animation.Frame < 20 {
continue
}
r.Level.MoveMonster(target, pos)
jumped = append(jumped, pos) jumped = append(jumped, pos)
r.IdleMonsters.Frame(target) r.IdleMonsters.Frame(target)
} }
for _, pos := range jumped { for _, pos := range jumped {
@ -261,4 +281,12 @@ func (r levelController) Render(ctx ui.Context) {
scoreTopLeft.Y += textureWidth scoreTopLeft.Y += textureWidth
heart.Draw(renderer, scoreTopLeft, 0) heart.Draw(renderer, scoreTopLeft, 0)
renderer.Text(scoreFont, scoreTopLeft.Add2D(textureWidth, fontOffsetY), textColor, fmt.Sprintf("x %d", r.Level.Lives)) renderer.Text(scoreFont, scoreTopLeft.Add2D(textureWidth, fontOffsetY), textColor, fmt.Sprintf("x %d", r.Level.Lives))
if r.Level.GameOver {
bounds := r.Bounds()
titleFont := ctx.Fonts().Font("title")
renderer.FillRectangle(bounds, zntg.MustHexColor(`#0000007F`))
offsetY := .5*bounds.Dy() - titleFont.Height()
renderer.TextAlign(titleFont, geom.PtF32(.5*bounds.Dx(), offsetY), textColor, "GAME OVER", ui.AlignCenter)
}
} }

View File

@ -14,6 +14,7 @@ type Level struct {
Tiles Tiles Tiles Tiles
Monsters Monsters Monsters Monsters
MonsterTargets map[geom.Point]geom.Point MonsterTargets map[geom.Point]geom.Point
GameOver bool
Bounds geom.Rectangle Bounds geom.Rectangle
} }
@ -71,6 +72,23 @@ func (l Level) CanMonsterMoveTo(p geom.Point) bool {
return true return true
} }
func (l *Level) DecrementLive() {
l.Lives--
if l.Lives == 0 {
l.GameOver = true
}
}
func (l *Level) DestroyMonster(pos geom.Point) {
delete(l.MonsterTargets, pos)
delete(l.Monsters, pos)
}
func (l *Level) MoveMonster(target, source geom.Point) {
l.Monsters[target] = l.Monsters[source]
l.DestroyMonster(source)
}
func (l *Level) MovePlayer(dir Direction) bool { func (l *Level) MovePlayer(dir Direction) bool {
towards, allowed := l.CanPlayerMove(dir) towards, allowed := l.CanPlayerMove(dir)
if !allowed { if !allowed {
@ -86,6 +104,10 @@ func (l *Level) MovePlayer(dir Direction) bool {
l.StarsCollected++ l.StarsCollected++
tile.Star = false tile.Star = false
} }
if l.Monsters[towards] != nil {
l.DecrementLive()
l.DestroyMonster(towards)
}
return true return true
} }