Compare commits

..

No commits in common. "a19d33cb9fd8f7dc0081afe5e6265c33b75bd2f0" and "cdb581d0f674aab96eb27389ead179277df66801" have entirely different histories.

2 changed files with 28 additions and 43 deletions

View File

@ -30,18 +30,16 @@ type playLevel struct {
type keyPressedState map[allg5.Key]bool type keyPressedState map[allg5.Key]bool
func (s keyPressedState) ArePressed(keys ...allg5.Key) []allg5.Key { func (s keyPressedState) CountPressed(keys ...allg5.Key) int {
pressed := make([]allg5.Key, 0, len(keys)) var cnt int
for _, k := range keys { for _, k := range keys {
if s[k] { if s[k] {
pressed = append(pressed, k) cnt++
} }
} }
return pressed return cnt
} }
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
@ -166,8 +164,15 @@ 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,24 +96,7 @@ func (s *playLevelState) Tick(now time.Duration) {
s.ani.Animate(now) s.ani.Animate(now)
} }
func (s *playLevelState) TryPlayerMove(key allg5.Key) { func (s *playLevelState) TryPlayerMove(dir geom.Point, 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
} }
@ -124,6 +107,21 @@ 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)
@ -143,24 +141,6 @@ 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 {