114 lines
2.3 KiB
Go
114 lines
2.3 KiB
Go
|
//go:build example
|
||
|
// +build example
|
||
|
|
||
|
package game
|
||
|
|
||
|
import (
|
||
|
"image/color"
|
||
|
"os"
|
||
|
"sync"
|
||
|
|
||
|
"code.rocketnine.space/tslocum/gohan"
|
||
|
"code.rocketnine.space/tslocum/gohan/_examples/twinstick/asset"
|
||
|
"code.rocketnine.space/tslocum/gohan/_examples/twinstick/component"
|
||
|
"code.rocketnine.space/tslocum/gohan/_examples/twinstick/entity"
|
||
|
"code.rocketnine.space/tslocum/gohan/_examples/twinstick/system"
|
||
|
"github.com/hajimehoshi/ebiten/v2"
|
||
|
)
|
||
|
|
||
|
// game is an isometric demo game.
|
||
|
type game struct {
|
||
|
w, h int
|
||
|
|
||
|
player gohan.EntityID
|
||
|
|
||
|
op *ebiten.DrawImageOptions
|
||
|
|
||
|
disableEsc bool
|
||
|
|
||
|
debugMode bool
|
||
|
cpuProfile *os.File
|
||
|
|
||
|
movementSystem *system.MovementSystem
|
||
|
|
||
|
sync.Mutex
|
||
|
}
|
||
|
|
||
|
// NewGame returns a new isometric demo game.
|
||
|
func NewGame() (*game, error) {
|
||
|
g := &game{
|
||
|
op: &ebiten.DrawImageOptions{},
|
||
|
}
|
||
|
|
||
|
g.addSystems()
|
||
|
|
||
|
err := g.loadAssets()
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
g.player = entity.NewPlayer()
|
||
|
|
||
|
asset.ImgWhiteSquare.Fill(color.White)
|
||
|
|
||
|
return g, nil
|
||
|
}
|
||
|
|
||
|
// Layout is called when the game's layout changes.
|
||
|
func (g *game) Layout(outsideWidth, outsideHeight int) (int, int) {
|
||
|
s := ebiten.DeviceScaleFactor()
|
||
|
w, h := int(s*float64(outsideWidth)), int(s*float64(outsideHeight))
|
||
|
if w != g.w || h != g.h {
|
||
|
g.w, g.h = w, h
|
||
|
g.movementSystem.ScreenW, g.movementSystem.ScreenH = float64(w), float64(h)
|
||
|
|
||
|
position := g.player.Component(component.PositionComponentID).(*component.PositionComponent)
|
||
|
if position.X == -1 && position.Y == -1 {
|
||
|
position.X, position.Y = float64(g.w)/2-16, float64(g.h)/2-16
|
||
|
}
|
||
|
}
|
||
|
return g.w, g.h
|
||
|
}
|
||
|
|
||
|
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 {
|
||
|
panic(err)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (g *game) addSystems() {
|
||
|
gohan.AddSystem(system.NewMovementInputSystem(g.player))
|
||
|
|
||
|
g.movementSystem = &system.MovementSystem{}
|
||
|
gohan.AddSystem(g.movementSystem)
|
||
|
|
||
|
gohan.AddSystem(system.NewFireInputSystem(g.player))
|
||
|
|
||
|
renderBullet := system.NewDrawBulletsSystem()
|
||
|
gohan.AddSystem(renderBullet)
|
||
|
|
||
|
renderPlayer := system.NewDrawPlayerSystem(g.player)
|
||
|
gohan.AddSystem(renderPlayer)
|
||
|
|
||
|
printInfo := system.NewPrintInfoSystem(g.player)
|
||
|
gohan.AddSystem(printInfo)
|
||
|
}
|
||
|
|
||
|
func (g *game) loadAssets() error {
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func (g *game) Exit() {
|
||
|
os.Exit(0)
|
||
|
}
|