diff --git a/cmd/krampus19/playlevel.go b/cmd/krampus19/playlevel.go index 6609bea..311e529 100644 --- a/cmd/krampus19/playlevel.go +++ b/cmd/krampus19/playlevel.go @@ -30,16 +30,18 @@ type playLevel struct { type keyPressedState map[allg5.Key]bool -func (s keyPressedState) CountPressed(keys ...allg5.Key) int { - var cnt int +func (s keyPressedState) ArePressed(keys ...allg5.Key) []allg5.Key { + pressed := make([]allg5.Key, 0, len(keys)) for _, k := range keys { 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 { l.ctx = ctx @@ -164,15 +166,8 @@ func (l *playLevel) Handle(e allg5.Event) { case allg5.KeyEscape: l.showMenu = true 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) } } } diff --git a/cmd/krampus19/playlevelstate.go b/cmd/krampus19/playlevelstate.go index 314b34a..2220cf6 100644 --- a/cmd/krampus19/playlevelstate.go +++ b/cmd/krampus19/playlevelstate.go @@ -96,7 +96,24 @@ func (s *playLevelState) Tick(now time.Duration) { 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() { return } @@ -116,9 +133,12 @@ func (s *playLevelState) TryPlayerMove(dir geom.Point, key allg5.Key) { 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) + } 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]) + } } })