Files
old-9heroja/main.go

140 lines
3.4 KiB
Go

package main
import (
"fmt"
_ "image/png"
"log"
"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"
"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 {
// The gopher's position
x16 int
y16 int
vy16 int
keys []ebiten.Key
control *input.Keyboard
terrain *terrain.Terrain
zombies []*zombie.Zombie
world *collision.World
bullets []*weapons.Handgun
ending *tiles.Ending
starting *tiles.Starting
}
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{zombie.NewZombie(), zombie.NewZombie(), zombie.NewZombie(), zombie.NewZombie(), zombie.NewZombie()}
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.terrain = terrain.NewTerrain()
}
func main() {
ebiten.SetWindowSize(screenWidth, screenHeight)
ebiten.SetWindowTitle("Bosanski Zombicid")
if err := ebiten.RunGame(GameInstance); err != nil {
log.Fatal(err)
}
}