Files
old-9heroja/main.go

98 lines
2.0 KiB
Go
Raw Normal View History

2023-11-17 22:02:40 +01:00
package main
import (
"github.com/hajimehoshi/ebiten/v2"
2023-11-28 07:29:17 +01:00
"github.com/hajimehoshi/ebiten/v2/inpututil"
2023-11-21 20:12:56 +01:00
"gitlab.com/kbr4/9heroja/configuration"
2023-11-19 08:06:40 +01:00
"gitlab.com/kbr4/9heroja/hero"
2023-11-28 07:29:17 +01:00
"gitlab.com/kbr4/9heroja/input"
2023-11-19 08:06:40 +01:00
"gitlab.com/kbr4/9heroja/terrain"
2023-11-17 22:02:40 +01:00
_ "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
2023-11-28 07:29:17 +01:00
keys []ebiten.Key
control *input.Keyboard
2023-11-19 08:06:40 +01:00
hero *hero.Hero
terrain *terrain.Terrain
2023-11-17 22:02:40 +01:00
}
func (g *Game) Update() error {
2023-11-28 07:29:17 +01:00
g.keys = inpututil.AppendPressedKeys(g.keys[:0])
GameInstance.hero.ChangeDirection(GameInstance.control.DirectionFromKeys(g.keys))
GameInstance.terrain.ChangeDirection(GameInstance.control.DirectionFromKeys(g.keys))
if len(g.keys) <= 0 {
g.hero.Stop()
} else {
g.terrain.Move()
g.hero.Walk()
}
2023-11-17 22:02:40 +01:00
return nil
}
func (g *Game) Draw(screen *ebiten.Image) {
2023-11-19 08:06:40 +01:00
g.terrain.DrawTerrain(screen)
g.hero.DrawHero(screen)
2023-11-17 22:02:40 +01:00
}
func (g *Game) Layout(outsideWidth, outsideHeight int) (screenWidth, screenHeight int) {
s := ebiten.DeviceScaleFactor()
return int(float64(outsideWidth) * s), int(float64(outsideHeight) * s)
}
2023-11-19 08:06:40 +01:00
var GameInstance *Game
2023-11-17 22:02:40 +01:00
func init() {
2023-11-19 08:06:40 +01:00
GameInstance = &Game{}
2023-11-28 07:29:17 +01:00
GameInstance.control = &input.Keyboard{}
2023-11-19 14:19:34 +01:00
GameInstance.hero = hero.NewHero()
GameInstance.terrain = terrain.NewTerrain()
2023-11-21 20:12:56 +01:00
GameInstance.hero.ChangeDirection(configuration.North)
GameInstance.terrain.ChangeDirection(configuration.North)
2023-11-19 14:19:34 +01:00
GameInstance.hero.Walk()
2023-11-17 22:02:40 +01:00
}
func main() {
ebiten.SetWindowSize(screenWidth, screenHeight)
ebiten.SetWindowTitle("Bosanski Zombicid")
2023-11-19 08:06:40 +01:00
if err := ebiten.RunGame(GameInstance); err != nil {
2023-11-17 22:02:40 +01:00
log.Fatal(err)
}
}