80 lines
1.9 KiB
Go
80 lines
1.9 KiB
Go
package game
|
|
|
|
import (
|
|
"image/color"
|
|
"log"
|
|
"math"
|
|
"os"
|
|
"time"
|
|
|
|
"code.rocketnine.space/tslocum/commandeuropa/entity"
|
|
|
|
"code.rocketnine.space/tslocum/commandeuropa/component"
|
|
"code.rocketnine.space/tslocum/commandeuropa/system"
|
|
"code.rocketnine.space/tslocum/commandeuropa/world"
|
|
"code.rocketnine.space/tslocum/gohan"
|
|
"github.com/hajimehoshi/ebiten/v2"
|
|
)
|
|
|
|
type Game struct{}
|
|
|
|
func NewGame() (*Game, error) {
|
|
g := &Game{}
|
|
|
|
redSprite := ebiten.NewImage(world.TileSize, world.TileSize)
|
|
redSprite.Fill(color.RGBA{255, 0, 0, 255})
|
|
|
|
greenSprite := ebiten.NewImage(world.TileSize, world.TileSize)
|
|
greenSprite.Fill(color.RGBA{0, 255, 0, 255})
|
|
|
|
gohan.AddSystem(&system.HandleInput{})
|
|
gohan.AddSystem(&system.RenderEnvironment{})
|
|
gohan.AddSystem(&system.RenderUnit{})
|
|
gohan.AddSystem(&system.RenderDebug{})
|
|
|
|
// Create singleton entity for systems that run one time each tick.
|
|
once := gohan.NewEntity()
|
|
once.AddComponent(&component.Once{})
|
|
|
|
world.Map = world.NewGameMap(time.Now().UnixNano())
|
|
|
|
_ = entity.NewUnit(component.UnitTypeWorker, 4, 4)
|
|
_ = entity.NewUnit(component.UnitTypeBuildingBarracks, 8, 8)
|
|
|
|
return g, nil
|
|
}
|
|
|
|
func (g *Game) Layout(outsideWidth, outsideHeight int) (screenWidth, screenHeight int) {
|
|
// Handle window resize.
|
|
if float64(outsideWidth) != world.ScreenWidth || float64(outsideHeight) != world.ScreenHeight {
|
|
world.ScreenWidth, world.ScreenHeight = float64(outsideWidth), float64(outsideHeight)
|
|
|
|
fullSize := float64(world.TileSize * world.MapSize)
|
|
minZoomWidth := float64(world.ScreenWidth) / fullSize
|
|
minZoomHeight := float64(world.ScreenHeight) / fullSize
|
|
world.MinCamScale = math.Ceil(math.Max(minZoomWidth, minZoomHeight))
|
|
}
|
|
|
|
return outsideWidth, outsideHeight
|
|
}
|
|
|
|
func (g *Game) Update() error {
|
|
if ebiten.IsWindowBeingClosed() {
|
|
g.Exit()
|
|
return nil
|
|
}
|
|
|
|
return gohan.Update()
|
|
}
|
|
|
|
func (g *Game) Draw(screen *ebiten.Image) {
|
|
err := gohan.Draw(screen)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func (g *Game) Exit() {
|
|
os.Exit(0)
|
|
}
|