Files
old-9heroja/main.go
2023-11-21 20:12:56 +01:00

99 lines
1.9 KiB
Go

package main
import (
"github.com/hajimehoshi/ebiten/v2"
"gitlab.com/kbr4/9heroja/configuration"
"gitlab.com/kbr4/9heroja/hero"
"gitlab.com/kbr4/9heroja/terrain"
_ "image/png"
"log"
"math/rand"
"time"
)
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
hero *hero.Hero
terrain *terrain.Terrain
}
func (g *Game) Update() error {
g.terrain.Move()
return nil
}
func (g *Game) Draw(screen *ebiten.Image) {
g.terrain.DrawTerrain(screen)
g.hero.DrawHero(screen)
}
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.hero = hero.NewHero()
GameInstance.terrain = terrain.NewTerrain()
GameInstance.hero.ChangeDirection(configuration.North)
GameInstance.terrain.ChangeDirection(configuration.North)
GameInstance.hero.Walk()
ticker := time.NewTicker(1500 * time.Millisecond)
go func() {
for {
select {
case <-ticker.C:
// change terrain direction to random direction
randomDirection := configuration.AllDirections[rand.Intn(len(configuration.AllDirections))]
GameInstance.terrain.ChangeDirection(randomDirection)
GameInstance.hero.ChangeDirection(randomDirection)
}
}
}()
}
func main() {
ebiten.SetWindowSize(screenWidth, screenHeight)
ebiten.SetWindowTitle("Bosanski Zombicid")
if err := ebiten.RunGame(GameInstance); err != nil {
log.Fatal(err)
}
}