Spawning radi

This commit is contained in:
2025-09-21 08:24:28 +02:00
parent e8d99dd002
commit 0e89b8be70
3 changed files with 63 additions and 43 deletions

52
main.go
View File

@@ -4,12 +4,12 @@ import (
"fmt"
_ "image/png"
"log"
"time"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
"github.com/hajimehoshi/ebiten/v2/inpututil"
"gitlab.com/kbr4/9heroja/collision"
"gitlab.com/kbr4/9heroja/configuration"
"gitlab.com/kbr4/9heroja/input"
"gitlab.com/kbr4/9heroja/terrain"
"gitlab.com/kbr4/9heroja/tiles"
@@ -43,12 +43,6 @@ func floorMod(x, y int) int {
}
type Game struct {
// The gopher's position
x16 int
y16 int
vy16 int
keys []ebiten.Key
control *input.Keyboard
@@ -59,12 +53,16 @@ type Game struct {
bullets []*weapons.Handgun
ending *tiles.Ending
starting *tiles.Starting
gameSpeed time.Duration
tickCount int
zombiesToSpawn int
zombieSpawnTicker *time.Ticker
}
func (g *Game) Update() error {
g.keys = inpututil.AppendPressedKeys(g.keys[:0])
GameInstance.terrain.ChangeDirection(GameInstance.control.DirectionFromKeys(g.keys))
g.world.NotifyAboutCollisions()
return nil
}
@@ -105,28 +103,38 @@ func init() {
GameInstance = &Game{}
GameInstance.world = collision.NewWorld()
GameInstance.control = &input.Keyboard{}
GameInstance.zombies = []*zombie.Zombie{zombie.NewZombie(), zombie.NewZombie(), zombie.NewZombie(), zombie.NewZombie(), zombie.NewZombie()}
GameInstance.zombies = []*zombie.Zombie{}
GameInstance.ending = tiles.NewEnding()
GameInstance.starting = tiles.NewStarting()
GameInstance.world.AddEntity(GameInstance.starting)
GameInstance.world.AddEntity(GameInstance.ending)
// put zombies in random places on the screen but not too close to the hero or each other
for _, z := range GameInstance.zombies {
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) {
z.X = float64(configuration.Random(0, screenWidth))
z.Y = float64(configuration.Random(0, screenHeight))
}
z.WhereToGoX = GameInstance.ending.X
z.WhereToGoY = GameInstance.ending.Y
z.Walk()
GameInstance.world.AddEntity(z)
}
GameInstance.gameSpeed = 100 * time.Millisecond
GameInstance.zombieSpawnTicker = time.NewTicker(GameInstance.gameSpeed)
GameInstance.zombiesToSpawn = 20
GameInstance.terrain = terrain.NewTerrain()
go func() {
log.Println("Starting zombie spawn ticker")
for {
select {
case <-GameInstance.zombieSpawnTicker.C:
GameInstance.tickCount++
if GameInstance.tickCount%10 == 0 && GameInstance.zombiesToSpawn > 0 {
z := GameInstance.starting.SpawnZombie(GameInstance.ending.X, GameInstance.ending.Y)
GameInstance.zombies = append(GameInstance.zombies, z)
GameInstance.world.AddEntity(z)
GameInstance.zombiesToSpawn--
}
}
if GameInstance.zombiesToSpawn <= 0 {
GameInstance.zombieSpawnTicker.Stop()
break
}
}
}()
}
func main() {