2021-11-19 04:13:28 +00:00
|
|
|
//go:build example
|
|
|
|
// +build example
|
|
|
|
|
|
|
|
package game
|
|
|
|
|
|
|
|
import (
|
|
|
|
"image/color"
|
|
|
|
"os"
|
|
|
|
"sync"
|
|
|
|
|
2021-12-06 01:17:15 +00:00
|
|
|
"code.rocketnine.space/tslocum/gohan/examples/twinstick/world"
|
|
|
|
|
2021-11-19 04:13:28 +00:00
|
|
|
"code.rocketnine.space/tslocum/gohan"
|
2021-11-22 02:48:54 +00:00
|
|
|
"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"
|
2021-11-19 04:13:28 +00:00
|
|
|
"github.com/hajimehoshi/ebiten/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
// game is an isometric demo game.
|
|
|
|
type game struct {
|
|
|
|
w, h int
|
|
|
|
|
2021-11-22 19:18:28 +00:00
|
|
|
player gohan.Entity
|
2021-11-19 04:13:28 +00:00
|
|
|
|
|
|
|
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{},
|
|
|
|
}
|
|
|
|
|
2021-11-22 02:48:54 +00:00
|
|
|
g.player = entity.NewPlayer()
|
|
|
|
|
2021-11-19 04:13:28 +00:00
|
|
|
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 {
|
2021-12-06 01:17:15 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-11-19 04:13:28 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-12-06 01:17:15 +00:00
|
|
|
return world.World.Update()
|
2021-11-19 04:13:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (g *game) Draw(screen *ebiten.Image) {
|
2021-12-06 01:17:15 +00:00
|
|
|
err := world.World.Draw(screen)
|
2021-11-19 04:13:28 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *game) addSystems() {
|
2021-12-06 01:17:15 +00:00
|
|
|
w := world.World
|
|
|
|
|
|
|
|
w.AddSystem(system.NewMovementInputSystem(g.player))
|
2021-11-19 04:13:28 +00:00
|
|
|
|
2021-12-06 01:17:15 +00:00
|
|
|
g.movementSystem = &system.MovementSystem{
|
|
|
|
Player: g.player,
|
|
|
|
}
|
|
|
|
w.AddSystem(g.movementSystem)
|
2021-11-19 04:13:28 +00:00
|
|
|
|
2021-12-06 01:17:15 +00:00
|
|
|
w.AddSystem(system.NewFireInputSystem(g.player))
|
2021-11-19 04:13:28 +00:00
|
|
|
|
|
|
|
renderBullet := system.NewDrawBulletsSystem()
|
2021-12-06 01:17:15 +00:00
|
|
|
w.AddSystem(renderBullet)
|
2021-11-19 04:13:28 +00:00
|
|
|
|
|
|
|
renderPlayer := system.NewDrawPlayerSystem(g.player)
|
2021-12-06 01:17:15 +00:00
|
|
|
w.AddSystem(renderPlayer)
|
2021-11-19 04:13:28 +00:00
|
|
|
|
|
|
|
printInfo := system.NewPrintInfoSystem(g.player)
|
2021-12-06 01:17:15 +00:00
|
|
|
w.AddSystem(printInfo)
|
2021-11-22 02:48:54 +00:00
|
|
|
|
2021-12-06 01:17:15 +00:00
|
|
|
w.AddSystem(system.NewProfileSystem(g.player))
|
2021-11-19 04:13:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (g *game) loadAssets() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *game) Exit() {
|
|
|
|
os.Exit(0)
|
|
|
|
}
|