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).
This commit is contained in:
Sander Schobers 2020-01-14 19:34:02 +01:00
parent cdb581d0f6
commit 041cdccc3f
2 changed files with 31 additions and 16 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
} }
@ -116,9 +133,12 @@ func (s *playLevelState) TryPlayerMove(dir geom.Point, key allg5.Key) {
if onComplete := s.onComplete; onComplete != nil { if onComplete := s.onComplete; onComplete != nil {
onComplete() onComplete()
} }
} else if s.keysDown[key] && s.keysDown.CountPressed(s.ctx.Settings.Controls.MovementKeys()...) == 1 { } else {
log.Printf("Key %s is still down, moving further", gut.KeyToString(key)) pressed := s.keysDown.ArePressed(s.ctx.Settings.Controls.MovementKeys()...)
s.TryPlayerMove(dir, key) if len(pressed) == 1 {
log.Printf("Movement key %s is down, moving further", gut.KeyToString(key))
s.TryPlayerMove(pressed[0])
}
} }
}) })