Files
old-9heroja/main.go
2024-01-04 05:57:21 +01:00

154 lines
3.7 KiB
Go

package main
import (
"fmt"
"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/hero"
"gitlab.com/kbr4/9heroja/input"
"gitlab.com/kbr4/9heroja/terrain"
"gitlab.com/kbr4/9heroja/weapons"
"gitlab.com/kbr4/9heroja/zombie"
_ "image/png"
"log"
)
const (
screenWidth = 640
screenHeight = 480
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 {
// The gopher's position
x16 int
y16 int
vy16 int
keys []ebiten.Key
control *input.Keyboard
hero *hero.Hero
terrain *terrain.Terrain
zombies []*zombie.Zombie
world *collision.World
bullets []*weapons.Handgun
}
func (g *Game) Update() error {
g.keys = inpututil.AppendPressedKeys(g.keys[:0])
GameInstance.hero.ChangeDirection(GameInstance.control.DirectionFromKeys(g.keys))
GameInstance.terrain.ChangeDirection(GameInstance.control.DirectionFromKeys(g.keys))
bullet := GameInstance.hero.Fire(g.terrain.PositionX, g.terrain.PositionY)
if bullet != nil {
GameInstance.world.AddEntity(bullet)
g.bullets = append(g.bullets, bullet)
}
if len(g.keys) <= 0 {
g.hero.Stop()
} else {
g.terrain.Move()
g.hero.Walk()
}
for _, b := range g.bullets {
if b.IsFlying {
b.Move()
}
}
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.hero.DrawHero(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.hero = hero.NewHero()
GameInstance.zombies = []*zombie.Zombie{zombie.NewZombie(), zombie.NewZombie(), zombie.NewZombie(), zombie.NewZombie(), zombie.NewZombie()}
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.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.Walk()
GameInstance.world.AddEntity(z)
}
GameInstance.terrain = terrain.NewTerrain()
GameInstance.hero.ChangeDirection(configuration.North)
GameInstance.terrain.ChangeDirection(configuration.North)
GameInstance.hero.Walk()
}
func main() {
ebiten.SetWindowSize(screenWidth, screenHeight)
ebiten.SetWindowTitle("Bosanski Zombicid")
if err := ebiten.RunGame(GameInstance); err != nil {
log.Fatal(err)
}
}