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.
121 lines
2.4 KiB
121 lines
2.4 KiB
//go:build example |
|
// +build example |
|
|
|
package game |
|
|
|
import ( |
|
"image/color" |
|
"os" |
|
"sync" |
|
|
|
"code.rocketnine.space/tslocum/gohan/examples/twinstick/world" |
|
|
|
"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 { |
|
if g.w == 0 || g.h == 0 { |
|
position := world.World.Component(g.player, component.PositionComponentID).(*component.PositionComponent) |
|
position.X, position.Y = float64(w)/2-16, float64(h)/2-16 |
|
} |
|
|
|
g.w, g.h = w, h |
|
g.movementSystem.ScreenW, g.movementSystem.ScreenH = float64(w), float64(h) |
|
} |
|
return g.w, g.h |
|
} |
|
|
|
func (g *game) Update() error { |
|
if ebiten.IsWindowBeingClosed() { |
|
g.Exit() |
|
return nil |
|
} |
|
|
|
return world.World.Update() |
|
} |
|
|
|
func (g *game) Draw(screen *ebiten.Image) { |
|
err := world.World.Draw(screen) |
|
if err != nil { |
|
panic(err) |
|
} |
|
} |
|
|
|
func (g *game) addSystems() { |
|
w := world.World |
|
|
|
w.AddSystem(system.NewMovementInputSystem(g.player)) |
|
|
|
g.movementSystem = &system.MovementSystem{ |
|
Player: g.player, |
|
} |
|
w.AddSystem(g.movementSystem) |
|
|
|
w.AddSystem(system.NewFireInputSystem(g.player)) |
|
|
|
renderBullet := system.NewDrawBulletsSystem() |
|
w.AddSystem(renderBullet) |
|
|
|
renderPlayer := system.NewDrawPlayerSystem(g.player) |
|
w.AddSystem(renderPlayer) |
|
|
|
printInfo := system.NewPrintInfoSystem(g.player) |
|
w.AddSystem(printInfo) |
|
|
|
w.AddSystem(system.NewProfileSystem(g.player)) |
|
} |
|
|
|
func (g *game) loadAssets() error { |
|
return nil |
|
} |
|
|
|
func (g *game) Exit() { |
|
os.Exit(0) |
|
}
|
|
|