From b4d81fbcfb75cfc287d053b30c2104b39781c015 Mon Sep 17 00:00:00 2001 From: Senad Uka Date: Tue, 21 Nov 2023 20:12:56 +0100 Subject: [PATCH] Direction --- configuration/config.go | 17 +++++++++++++++++ hero/hero.go | 38 ++++++++++++++++++++++++++++++++++---- main.go | 21 ++++++++++++++++++++- terrain/terrain.go | 23 +++++++++++++++++++++-- 4 files changed, 92 insertions(+), 7 deletions(-) create mode 100644 configuration/config.go diff --git a/configuration/config.go b/configuration/config.go new file mode 100644 index 0000000..eeca76f --- /dev/null +++ b/configuration/config.go @@ -0,0 +1,17 @@ +package configuration + +type Direction int + +const ( + North Direction = iota + East + South + West + NorthEast + SouthEast + SouthWest + NorthWest +) + +// var AllDirections = []Direction{North, East, South, West, NorthEast, SouthEast, SouthWest, NorthWest} +var AllDirections = []Direction{South, North, NorthEast} diff --git a/hero/hero.go b/hero/hero.go index f0b2cb5..5f7e5ab 100644 --- a/hero/hero.go +++ b/hero/hero.go @@ -3,13 +3,14 @@ package hero import ( "bytes" "github.com/hajimehoshi/ebiten/v2" + "gitlab.com/kbr4/9heroja/configuration" "gitlab.com/kbr4/9heroja/resources" "image" "log" "time" ) -const WalkSpeedMs = 400 +const WalkSpeedMs = 300 var ( heroImage *ebiten.Image @@ -17,7 +18,8 @@ var ( ) type Hero struct { - step int + step int + direction configuration.Direction } type spritePosition struct { @@ -27,7 +29,10 @@ type spritePosition struct { func (h *Hero) DrawHero(screen *ebiten.Image) { - positions := []spritePosition{ + // set movement positions to be hashmap of sprite positions for each direction + + movementPositions := map[configuration.Direction][]spritePosition{} + movementPositions[configuration.North] = []spritePosition{ {0, 166}, {33, 166}, {66, 166}, @@ -37,7 +42,28 @@ func (h *Hero) DrawHero(screen *ebiten.Image) { {66, 166}, {33, 166}, } - p := positions[h.step%5] + movementPositions[configuration.NorthEast] = []spritePosition{ + {168, 0}, + {201, 0}, + {0, 33}, + {33, 33}, + {66, 33}, + {33, 33}, + {0, 33}, + {201, 0}, + } + + movementPositions[configuration.South] = []spritePosition{ + {66, 132}, + {0, 99}, + {33, 99}, + {66, 99}, + {99, 99}, + {66, 99}, + {33, 99}, + {0, 99}, + } + p := movementPositions[h.direction][h.step%8] op := &ebiten.DrawImageOptions{} screenWidth := screen.Bounds().Max.X @@ -88,6 +114,10 @@ func (h *Hero) Walk() { } +func (h *Hero) ChangeDirection(d configuration.Direction) { + h.direction = d +} + func (h *Hero) Stop() { h.step = 2 walkTicker.Stop() diff --git a/main.go b/main.go index 9e0b8b5..1e69b39 100644 --- a/main.go +++ b/main.go @@ -2,10 +2,13 @@ package main import ( "github.com/hajimehoshi/ebiten/v2" + "gitlab.com/kbr4/9heroja/configuration" "gitlab.com/kbr4/9heroja/hero" "gitlab.com/kbr4/9heroja/terrain" _ "image/png" "log" + "math/rand" + "time" ) const ( @@ -45,7 +48,7 @@ type Game struct { } func (g *Game) Update() error { - g.terrain.GoDown() + g.terrain.Move() return nil } @@ -66,7 +69,23 @@ func init() { GameInstance = &Game{} 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 6c1c74c..5e75f95 100644 --- a/terrain/terrain.go +++ b/terrain/terrain.go @@ -3,9 +3,11 @@ package terrain import ( "bytes" "github.com/hajimehoshi/ebiten/v2" + "gitlab.com/kbr4/9heroja/configuration" "gitlab.com/kbr4/9heroja/resources" "image" "log" + "slices" ) var ( @@ -15,6 +17,7 @@ var ( type Terrain struct { positionX float64 positionY float64 + direction configuration.Direction } func (t *Terrain) DrawTerrain(screen *ebiten.Image) { @@ -49,6 +52,22 @@ func NewTerrain() *Terrain { } } -func (t *Terrain) GoDown() { - t.positionY += 1 +func (t *Terrain) Move() { + if slices.Contains([]configuration.Direction{configuration.North, configuration.NorthEast, configuration.NorthWest}, t.direction) { + t.positionY += 1 + } + if slices.Contains([]configuration.Direction{configuration.South, configuration.SouthEast, configuration.SouthWest}, t.direction) { + t.positionY -= 1 + } + if slices.Contains([]configuration.Direction{configuration.East, configuration.NorthEast, configuration.SouthEast}, t.direction) { + t.positionX -= 1 + } + if slices.Contains([]configuration.Direction{configuration.West, configuration.NorthWest, configuration.SouthWest}, t.direction) { + t.positionX += 1 + } + +} + +func (t *Terrain) ChangeDirection(direction configuration.Direction) { + t.direction = direction }