From 9ea4c3beb2dc6accdb180d0062a2eebb598dfd0e Mon Sep 17 00:00:00 2001 From: Senad Uka Date: Tue, 28 Nov 2023 07:29:17 +0100 Subject: [PATCH] Keyboard input for moving --- configuration/config.go | 3 +-- hero/hero.go | 14 ++++++++--- input/keyboard.go | 53 +++++++++++++++++++++++++++++++++++++++++ main.go | 33 +++++++++++++------------ terrain/terrain.go | 4 +++- 5 files changed, 84 insertions(+), 23 deletions(-) create mode 100644 input/keyboard.go diff --git a/configuration/config.go b/configuration/config.go index e2b2843..fa2621f 100644 --- a/configuration/config.go +++ b/configuration/config.go @@ -11,6 +11,5 @@ const ( SouthEast SouthWest NorthWest + PreviouslyHeld ) - -var AllDirections = []Direction{North, East, South, West, NorthEast, SouthEast, SouthWest, NorthWest} diff --git a/hero/hero.go b/hero/hero.go index 70b0ef0..380eab7 100644 --- a/hero/hero.go +++ b/hero/hero.go @@ -20,6 +20,7 @@ var ( type Hero struct { step int direction configuration.Direction + IsWalking bool } type spritePosition struct { @@ -146,7 +147,8 @@ func init() { func NewHero() *Hero { hero := &Hero{ - step: 0, + step: 0, + IsWalking: false, } go func() { @@ -165,15 +167,21 @@ func NewHero() *Hero { func (h *Hero) Walk() { - walkTicker.Reset(WalkSpeedMs * time.Millisecond) + if !h.IsWalking { + walkTicker.Reset(WalkSpeedMs * time.Millisecond) + h.IsWalking = true + } } func (h *Hero) ChangeDirection(d configuration.Direction) { - h.direction = d + if d != configuration.PreviouslyHeld { + h.direction = d + } } func (h *Hero) Stop() { h.step = 2 walkTicker.Stop() + h.IsWalking = false } diff --git a/input/keyboard.go b/input/keyboard.go new file mode 100644 index 0000000..ffae052 --- /dev/null +++ b/input/keyboard.go @@ -0,0 +1,53 @@ +package input + +import ( + "github.com/hajimehoshi/ebiten/v2" + "sort" + "strings" +) +import "gitlab.com/kbr4/9heroja/configuration" + +type Keyboard struct { +} + +func (k *Keyboard) DirectionFromKeys(keys []ebiten.Key) configuration.Direction { + direction := "" + for _, key := range keys { + switch key { + case ebiten.KeyUp: + direction += "N" + case ebiten.KeyRight: + direction += "E" + case ebiten.KeyLeft: + direction += "W" + case ebiten.KeyDown: + direction += "S" + } + } + + // sort direction string + s := strings.Split(direction, "") + sort.Strings(s) + sortedDirection := strings.Join(s, "") + + switch sortedDirection { + case "N": + return configuration.North + case "E": + return configuration.East + case "S": + return configuration.South + case "W": + return configuration.West + case "EN": + return configuration.NorthEast + case "ES": + return configuration.SouthEast + case "SW": + return configuration.SouthWest + case "NW": + return configuration.NorthWest + default: + return configuration.PreviouslyHeld + } +} diff --git a/main.go b/main.go index 1e69b39..19cc49c 100644 --- a/main.go +++ b/main.go @@ -2,13 +2,13 @@ package main import ( "github.com/hajimehoshi/ebiten/v2" + "github.com/hajimehoshi/ebiten/v2/inpututil" "gitlab.com/kbr4/9heroja/configuration" "gitlab.com/kbr4/9heroja/hero" + "gitlab.com/kbr4/9heroja/input" "gitlab.com/kbr4/9heroja/terrain" _ "image/png" "log" - "math/rand" - "time" ) const ( @@ -43,12 +43,24 @@ type Game struct { y16 int vy16 int + keys []ebiten.Key + + control *input.Keyboard + hero *hero.Hero terrain *terrain.Terrain } func (g *Game) Update() error { - g.terrain.Move() + g.keys = inpututil.AppendPressedKeys(g.keys[:0]) + GameInstance.hero.ChangeDirection(GameInstance.control.DirectionFromKeys(g.keys)) + GameInstance.terrain.ChangeDirection(GameInstance.control.DirectionFromKeys(g.keys)) + if len(g.keys) <= 0 { + g.hero.Stop() + } else { + g.terrain.Move() + g.hero.Walk() + } return nil } @@ -67,25 +79,12 @@ var GameInstance *Game func init() { GameInstance = &Game{} + GameInstance.control = &input.Keyboard{} GameInstance.hero = hero.NewHero() GameInstance.terrain = terrain.NewTerrain() GameInstance.hero.ChangeDirection(configuration.North) GameInstance.terrain.ChangeDirection(configuration.North) GameInstance.hero.Walk() - - ticker := time.NewTicker(1500 * time.Millisecond) - go func() { - for { - select { - case <-ticker.C: - // change terrain direction to random direction - randomDirection := configuration.AllDirections[rand.Intn(len(configuration.AllDirections))] - GameInstance.terrain.ChangeDirection(randomDirection) - GameInstance.hero.ChangeDirection(randomDirection) - } - } - - }() } func main() { diff --git a/terrain/terrain.go b/terrain/terrain.go index 5e75f95..418af0e 100644 --- a/terrain/terrain.go +++ b/terrain/terrain.go @@ -69,5 +69,7 @@ func (t *Terrain) Move() { } func (t *Terrain) ChangeDirection(direction configuration.Direction) { - t.direction = direction + if direction != configuration.PreviouslyHeld { + t.direction = direction + } }