package main 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/input" "gitlab.com/kbr4/9heroja/terrain" "gitlab.com/kbr4/9heroja/tiles" "gitlab.com/kbr4/9heroja/weapons" "gitlab.com/kbr4/9heroja/zombie" ) const ( screenWidth = 720 screenHeight = 1280 tileSize = 33 titleFontSize = fontSize * 1.5 fontSize = 24 smallFontSize = fontSize / 2 pipeWidth = tileSize * 2 pipeStartOffsetX = 8 pipeIntervalX = 8 pipeGapY = 5 ) func floorDiv(x, y int) int { d := x / y if d*y == x || x >= 0 { return d } return d - 1 } func floorMod(x, y int) int { return x - floorDiv(x, y)*y } type Game struct { keys []ebiten.Key control *input.Keyboard terrain *terrain.Terrain zombies []*zombie.Zombie world *collision.World 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 } 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) } for _, b := range g.bullets { b.OffsetX = g.terrain.PositionX b.OffsetY = g.terrain.PositionY if b.IsFlying { b.DrawHandgunBullet(screen) } } g.ending.DrawEnding(screen) g.starting.DrawStarting(screen) msg := fmt.Sprintf(`TPS: %0.2f FPS: %0.2f`, ebiten.ActualTPS(), ebiten.ActualFPS()) ebitenutil.DebugPrint(screen, msg) } func (g *Game) Layout(outsideWidth, outsideHeight int) (screenWidth, screenHeight int) { s := ebiten.DeviceScaleFactor() return int(float64(outsideWidth) * s), int(float64(outsideHeight) * s) } var GameInstance *Game func init() { GameInstance = &Game{} GameInstance.world = collision.NewWorld() GameInstance.control = &input.Keyboard{} GameInstance.zombies = []*zombie.Zombie{} GameInstance.ending = tiles.NewEnding() GameInstance.starting = tiles.NewStarting() GameInstance.world.AddEntity(GameInstance.starting) GameInstance.world.AddEntity(GameInstance.ending) 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() { ebiten.SetWindowSize(screenWidth, screenHeight) ebiten.SetWindowTitle("Bosanski Zombicid") if err := ebiten.RunGame(GameInstance); err != nil { log.Fatal(err) } }