Compare commits
2 Commits
cdb581d0f6
...
a19d33cb9f
Author | SHA1 | Date | |
---|---|---|---|
a19d33cb9f | |||
041cdccc3f |
@ -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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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 {
|
||||||
|
Loading…
Reference in New Issue
Block a user