Compare commits

..

2 Commits

Author SHA1 Message Date
a19d33cb9f Changed behaviour of player after brick has been sunken.
- Previously the player would stop on the current tile even if the correct key was pressed. Now the movement continues without interruption.
2020-01-14 19:35:52 +01:00
041cdccc3f Changed behaviour of movement.
- Every movement key at any moment is valid if it is the only movement key pressed (when the previous move is finished).
2020-01-14 19:34:02 +01:00
2 changed files with 43 additions and 28 deletions

View File

@ -30,16 +30,18 @@ type playLevel struct {
type keyPressedState map[allg5.Key]bool type keyPressedState map[allg5.Key]bool
func (s keyPressedState) CountPressed(keys ...allg5.Key) int { func (s keyPressedState) ArePressed(keys ...allg5.Key) []allg5.Key {
var cnt int pressed := make([]allg5.Key, 0, len(keys))
for _, k := range keys { for _, k := range keys {
if s[k] { if s[k] {
cnt++ pressed = append(pressed, k)
} }
} }
return cnt return pressed
} }
func (s keyPressedState) CountPressed(keys ...allg5.Key) int { return len(s.ArePressed(keys...)) }
func (l *playLevel) Enter(ctx *Context) error { func (l *playLevel) Enter(ctx *Context) error {
l.ctx = ctx l.ctx = ctx
@ -164,15 +166,8 @@ func (l *playLevel) Handle(e allg5.Event) {
case allg5.KeyEscape: case allg5.KeyEscape:
l.showMenu = true l.showMenu = true
l.menu.Activate(0) l.menu.Activate(0)
case l.ctx.Settings.Controls.MoveUp:
l.state.TryPlayerMove(geom.Pt(0, -1), e.KeyCode)
case l.ctx.Settings.Controls.MoveRight:
l.state.TryPlayerMove(geom.Pt(1, 0), e.KeyCode)
case l.ctx.Settings.Controls.MoveDown:
l.state.TryPlayerMove(geom.Pt(0, 1), e.KeyCode)
case l.ctx.Settings.Controls.MoveLeft:
l.state.TryPlayerMove(geom.Pt(-1, 0), e.KeyCode)
} }
l.state.TryPlayerMove(e.KeyCode)
} }
} }
} }

View File

@ -96,7 +96,24 @@ func (s *playLevelState) Tick(now time.Duration) {
s.ani.Animate(now) s.ani.Animate(now)
} }
func (s *playLevelState) TryPlayerMove(dir geom.Point, key allg5.Key) { func (s *playLevelState) TryPlayerMove(key allg5.Key) {
var dir geom.Point
switch key {
case s.ctx.Settings.Controls.MoveUp:
dir = geom.Pt(0, -1)
case s.ctx.Settings.Controls.MoveRight:
dir = geom.Pt(1, 0)
case s.ctx.Settings.Controls.MoveDown:
dir = geom.Pt(0, 1)
case s.ctx.Settings.Controls.MoveLeft:
dir = geom.Pt(-1, 0)
default:
return
}
s.tryPlayerMove(dir, key)
}
func (s *playLevelState) tryPlayerMove(dir geom.Point, key allg5.Key) {
if s.player.scr.pos != s.player.pos.ToF32() { if s.player.scr.pos != s.player.pos.ToF32() {
return return
} }
@ -107,21 +124,6 @@ func (s *playLevelState) TryPlayerMove(dir geom.Point, key allg5.Key) {
return return
} }
s.steps++
log.Printf("Moving player to %s", to)
s.ani.StartFn(s.ctx.Tick, newMoveAnimation(s.player, to), func() {
log.Printf("Player movement finished")
if s.player.pos == s.egg.pos {
s.complete = true
if onComplete := s.onComplete; onComplete != nil {
onComplete()
}
} else if s.keysDown[key] && s.keysDown.CountPressed(s.ctx.Settings.Controls.MovementKeys()...) == 1 {
log.Printf("Key %s is still down, moving further", gut.KeyToString(key))
s.TryPlayerMove(dir, key)
}
})
if brick := s.bricks.FindEntity(to); brick != nil { if brick := s.bricks.FindEntity(to); brick != nil {
log.Printf("Pushing brick at %s", to) log.Printf("Pushing brick at %s", to)
brickTo := to.Add(dir) brickTo := to.Add(dir)
@ -141,6 +143,24 @@ func (s *playLevelState) TryPlayerMove(dir geom.Point, key allg5.Key) {
} }
}) })
} }
s.steps++
log.Printf("Moving player to %s", to)
s.ani.StartFn(s.ctx.Tick, newMoveAnimation(s.player, to), func() {
log.Printf("Player movement finished")
if s.player.pos == s.egg.pos {
s.complete = true
if onComplete := s.onComplete; onComplete != nil {
onComplete()
}
} else {
pressed := s.keysDown.ArePressed(s.ctx.Settings.Controls.MovementKeys()...)
if len(pressed) == 1 {
log.Printf("Movement key %s is down, moving further", gut.KeyToString(key))
s.TryPlayerMove(pressed[0])
}
}
})
} }
func (s *playLevelState) canMove(from, dir geom.Point) bool { func (s *playLevelState) canMove(from, dir geom.Point) bool {