Entity Component System framework for Ebitengine
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

115 lines
2.3 KiB

//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.Entity
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.player = entity.NewPlayer()
g.addSystems()
err := g.loadAssets()
if err != nil {
return nil, err
}
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 := component.Position(g.player)
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)
gohan.AddSystem(system.NewProfileSystem(g.player))
}
func (g *game) loadAssets() error {
return nil
}
func (g *game) Exit() {
os.Exit(0)
}