No scrolling

This commit is contained in:
2024-01-08 06:19:39 +01:00
parent 156e3ed43c
commit a838fcec48
4 changed files with 39 additions and 24 deletions

View File

@@ -10,6 +10,7 @@ import (
"image" "image"
"image/color" "image/color"
"log" "log"
"slices"
"time" "time"
) )
@@ -131,13 +132,9 @@ func (h *Hero) DrawHero(screen *ebiten.Image) {
p := movementPositions[h.direction][h.step%8] p := movementPositions[h.direction][h.step%8]
op := &ebiten.DrawImageOptions{} op := &ebiten.DrawImageOptions{}
screenWidth := screen.Bounds().Max.X
screenHeight := screen.Bounds().Max.Y
// ground // ground
op.GeoM.Reset() 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(h.X, h.Y)
//op.GeoM.Translate(float64(i*tileSize-floorMod(g.cameraX, tileSize)), //op.GeoM.Translate(float64(i*tileSize-floorMod(g.cameraX, tileSize)),
// float64((ny-1)*tileSize-floorMod(g.cameraY, tileSize))) // float64((ny-1)*tileSize-floorMod(g.cameraY, tileSize)))
@@ -193,6 +190,8 @@ func NewHero() *Hero {
Health: 100, Health: 100,
gunLoaded: true, gunLoaded: true,
} }
hero.X = float64(10)
hero.Y = float64(10)
go func() { go func() {
for { for {
@@ -284,3 +283,18 @@ func (h *Hero) Fire(offsetX float64, offsetY float64) (bullet *weapons.Handgun)
return nil 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
}
}

10
main.go
View File

@@ -72,7 +72,7 @@ func (g *Game) Update() error {
if len(g.keys) <= 0 { if len(g.keys) <= 0 {
g.hero.Stop() g.hero.Stop()
} else { } else {
g.terrain.Move() g.hero.Move()
g.hero.Walk() g.hero.Walk()
} }
@@ -81,6 +81,10 @@ func (g *Game) Update() error {
b.Move() b.Move()
} }
} }
for _, z := range g.zombies {
z.TargetX = g.hero.X
z.TargetY = g.hero.Y
}
g.world.NotifyAboutCollisions() g.world.NotifyAboutCollisions()
return nil return nil
@@ -89,8 +93,6 @@ func (g *Game) Update() error {
func (g *Game) Draw(screen *ebiten.Image) { func (g *Game) Draw(screen *ebiten.Image) {
g.terrain.DrawTerrain(screen) g.terrain.DrawTerrain(screen)
for _, z := range g.zombies { for _, z := range g.zombies {
z.OffsetX = g.terrain.PositionX
z.OffsetY = g.terrain.PositionY
z.DrawZombie(screen) z.DrawZombie(screen)
} }
@@ -126,6 +128,8 @@ func init() {
GameInstance.world.AddEntity(GameInstance.hero) GameInstance.world.AddEntity(GameInstance.hero)
// put zombies in random places on the screen but not too close to the hero or each other // put zombies in random places on the screen but not too close to the hero or each other
for _, z := range GameInstance.zombies { for _, z := range GameInstance.zombies {
z.TargetX = GameInstance.hero.X
z.TargetY = GameInstance.hero.Y
z.X = float64(configuration.Random(50, screenWidth-50)) z.X = float64(configuration.Random(50, screenWidth-50))
z.Y = float64(configuration.Random(50, screenHeight-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) { 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) {

View File

@@ -7,7 +7,6 @@ import (
"gitlab.com/kbr4/9heroja/resources" "gitlab.com/kbr4/9heroja/resources"
"image" "image"
"log" "log"
"slices"
) )
var ( 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) { func (t *Terrain) ChangeDirection(direction configuration.Direction) {
if direction != configuration.PreviouslyHeld { if direction != configuration.PreviouslyHeld {
t.direction = direction t.direction = direction

View File

@@ -9,6 +9,7 @@ import (
"gitlab.com/kbr4/9heroja/resources" "gitlab.com/kbr4/9heroja/resources"
"image" "image"
"log" "log"
"math"
"time" "time"
) )
@@ -28,6 +29,8 @@ type Zombie struct {
OffsetX float64 OffsetX float64
OffsetY float64 OffsetY float64
IsDead bool IsDead bool
TargetX float64
TargetY float64
} }
type spritePosition struct { type spritePosition struct {
@@ -94,6 +97,8 @@ func NewZombie() *Zombie {
if zombie.step > 3 { if zombie.step > 3 {
zombie.step = 0 zombie.step = 0
} }
zombie.Move()
} }
} }
} }
@@ -106,6 +111,7 @@ func (z *Zombie) Walk() {
if !z.IsWalking { if !z.IsWalking {
walkTicker.Reset(WalkSpeedMs * time.Millisecond) walkTicker.Reset(WalkSpeedMs * time.Millisecond)
z.IsWalking = true z.IsWalking = true
} }
} }
@@ -154,3 +160,11 @@ func (z *Zombie) HandleCollisionEvent(other collision.Collidable) {
z.IsDead = true 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
}