diff --git a/hero/hero.go b/hero/hero.go index 85b844b..cd3affb 100644 --- a/hero/hero.go +++ b/hero/hero.go @@ -10,6 +10,7 @@ import ( "image" "image/color" "log" + "slices" "time" ) @@ -131,13 +132,9 @@ func (h *Hero) DrawHero(screen *ebiten.Image) { p := movementPositions[h.direction][h.step%8] op := &ebiten.DrawImageOptions{} - screenWidth := screen.Bounds().Max.X - screenHeight := screen.Bounds().Max.Y // ground op.GeoM.Reset() - h.X = float64(screenWidth/2 - 16) - h.Y = float64(screenHeight/2 - 16) op.GeoM.Translate(h.X, h.Y) //op.GeoM.Translate(float64(i*tileSize-floorMod(g.cameraX, tileSize)), // float64((ny-1)*tileSize-floorMod(g.cameraY, tileSize))) @@ -193,6 +190,8 @@ func NewHero() *Hero { Health: 100, gunLoaded: true, } + hero.X = float64(10) + hero.Y = float64(10) go func() { for { @@ -284,3 +283,18 @@ func (h *Hero) Fire(offsetX float64, offsetY float64) (bullet *weapons.Handgun) return nil } } +func (h *Hero) Move() { + if slices.Contains([]configuration.Direction{configuration.North, configuration.NorthEast, configuration.NorthWest}, h.direction) { + h.Y -= 3 + } + if slices.Contains([]configuration.Direction{configuration.South, configuration.SouthEast, configuration.SouthWest}, h.direction) { + h.Y += 3 + } + if slices.Contains([]configuration.Direction{configuration.East, configuration.NorthEast, configuration.SouthEast}, h.direction) { + h.X += 3 + } + if slices.Contains([]configuration.Direction{configuration.West, configuration.NorthWest, configuration.SouthWest}, h.direction) { + h.X -= 3 + } + +} diff --git a/main.go b/main.go index 6203d06..70842c4 100644 --- a/main.go +++ b/main.go @@ -72,7 +72,7 @@ func (g *Game) Update() error { if len(g.keys) <= 0 { g.hero.Stop() } else { - g.terrain.Move() + g.hero.Move() g.hero.Walk() } @@ -81,6 +81,10 @@ func (g *Game) Update() error { b.Move() } } + for _, z := range g.zombies { + z.TargetX = g.hero.X + z.TargetY = g.hero.Y + } g.world.NotifyAboutCollisions() return nil @@ -89,8 +93,6 @@ func (g *Game) Update() error { func (g *Game) Draw(screen *ebiten.Image) { g.terrain.DrawTerrain(screen) for _, z := range g.zombies { - z.OffsetX = g.terrain.PositionX - z.OffsetY = g.terrain.PositionY z.DrawZombie(screen) } @@ -126,6 +128,8 @@ func init() { GameInstance.world.AddEntity(GameInstance.hero) // put zombies in random places on the screen but not too close to the hero or each other for _, z := range GameInstance.zombies { + z.TargetX = GameInstance.hero.X + z.TargetY = GameInstance.hero.Y z.X = float64(configuration.Random(50, screenWidth-50)) z.Y = float64(configuration.Random(50, screenHeight-50)) for z.X > float64(screenWidth/2-64) && z.X < float64(screenWidth/2+64) && z.Y > float64(screenHeight/2-64) && z.Y < float64(screenHeight/2+64) { diff --git a/terrain/terrain.go b/terrain/terrain.go index 9b0d74f..dc7f35d 100644 --- a/terrain/terrain.go +++ b/terrain/terrain.go @@ -7,7 +7,6 @@ import ( "gitlab.com/kbr4/9heroja/resources" "image" "log" - "slices" ) var ( @@ -54,22 +53,6 @@ func NewTerrain() *Terrain { } } -func (t *Terrain) Move() { - if slices.Contains([]configuration.Direction{configuration.North, configuration.NorthEast, configuration.NorthWest}, t.direction) { - t.PositionY += 3 - } - if slices.Contains([]configuration.Direction{configuration.South, configuration.SouthEast, configuration.SouthWest}, t.direction) { - t.PositionY -= 3 - } - if slices.Contains([]configuration.Direction{configuration.East, configuration.NorthEast, configuration.SouthEast}, t.direction) { - t.PositionX -= 3 - } - if slices.Contains([]configuration.Direction{configuration.West, configuration.NorthWest, configuration.SouthWest}, t.direction) { - t.PositionX += 3 - } - -} - func (t *Terrain) ChangeDirection(direction configuration.Direction) { if direction != configuration.PreviouslyHeld { t.direction = direction diff --git a/zombie/zombie.go b/zombie/zombie.go index 7ba9987..f038887 100644 --- a/zombie/zombie.go +++ b/zombie/zombie.go @@ -9,6 +9,7 @@ import ( "gitlab.com/kbr4/9heroja/resources" "image" "log" + "math" "time" ) @@ -28,6 +29,8 @@ type Zombie struct { OffsetX float64 OffsetY float64 IsDead bool + TargetX float64 + TargetY float64 } type spritePosition struct { @@ -94,6 +97,8 @@ func NewZombie() *Zombie { if zombie.step > 3 { zombie.step = 0 } + + zombie.Move() } } } @@ -106,6 +111,7 @@ func (z *Zombie) Walk() { if !z.IsWalking { walkTicker.Reset(WalkSpeedMs * time.Millisecond) z.IsWalking = true + } } @@ -154,3 +160,11 @@ func (z *Zombie) HandleCollisionEvent(other collision.Collidable) { z.IsDead = true } } + +func (z *Zombie) Move() { + fmt.Printf("Zombie target: %f, %f\n", z.TargetX, z.TargetY) + directionX := (z.TargetX - z.X) / math.Abs(z.TargetX+z.X) + directionY := (z.TargetY - z.Y) / math.Abs(z.TargetY+z.Y) + z.X += directionX + z.Y += directionY +}