Browse Source

Add entity component system

main
Trevor Slocum 3 months ago
parent
commit
65816fd427
  1. 3
      component/once.go
  2. 38
      game/game.go
  3. 5
      go.mod
  4. 2
      go.sum
  5. 46
      system/renderdebug.go
  6. 22
      system/renderenvironment.go

3
component/once.go

@ -0,0 +1,3 @@
package component
type Once struct{}

38
game/game.go

@ -1,29 +1,29 @@
package game
import (
"log"
"os"
"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) {
return &Game{}, nil
}
g := &Game{}
func (g *Game) Update() error {
if ebiten.IsWindowBeingClosed() {
g.Exit()
return nil
}
gohan.AddSystem(&system.RenderEnvironment{})
gohan.AddSystem(&system.RenderDebug{})
return nil
}
func (g *Game) Draw(screen *ebiten.Image) {
// Create singleton entity for systems that run one time each tick.
once := gohan.NewEntity()
once.AddComponent(&component.Once{})
return g, nil
}
func (g *Game) Layout(outsideWidth, outsideHeight int) (screenWidth, screenHeight int) {
@ -35,6 +35,22 @@ func (g *Game) Layout(outsideWidth, outsideHeight int) (screenWidth, screenHeigh
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)
}

5
go.mod

@ -2,7 +2,10 @@ module code.rocketnine.space/tslocum/commandeuropa
go 1.19
require github.com/hajimehoshi/ebiten/v2 v2.4.12
require (
code.rocketnine.space/tslocum/gohan v1.0.0
github.com/hajimehoshi/ebiten/v2 v2.4.12
)
require (
github.com/ebitengine/purego v0.1.0 // indirect

2
go.sum

@ -1,3 +1,5 @@
code.rocketnine.space/tslocum/gohan v1.0.0 h1:WBcJq7nVfmr1EB8bew6xWlB5Q1714yWJ3a9/q6aBBrY=
code.rocketnine.space/tslocum/gohan v1.0.0/go.mod h1:12yOt5Ygl/RVwnnZSVZRuS1W6gCaHJgezcvg8+THk10=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/ebitengine/purego v0.0.0-20220905075623-aeed57cda744/go.mod h1:Eh8I3yvknDYZeCuXH9kRNaPuHEwvXDCk378o9xszmHg=
github.com/ebitengine/purego v0.1.0 h1:vAEo1FvmbjA050QKsbDbcHj03hhMMvh0fmr9LSehpnU=

46
system/renderdebug.go

@ -0,0 +1,46 @@
package system
import (
"fmt"
"image/color"
"code.rocketnine.space/tslocum/commandeuropa/component"
"code.rocketnine.space/tslocum/commandeuropa/world"
"code.rocketnine.space/tslocum/gohan"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
)
type RenderDebug struct {
Once *component.Once
op *ebiten.DrawImageOptions
debugImg *ebiten.Image
initialized bool
}
func (s *RenderDebug) Initialize() {
s.op = &ebiten.DrawImageOptions{}
s.op.GeoM.Scale(2, 2)
s.debugImg = ebiten.NewImage(80, 80)
s.initialized = true
}
func (s *RenderDebug) Update(_ gohan.Entity) error {
return gohan.ErrUnregister
}
func (s *RenderDebug) Draw(e gohan.Entity, screen *ebiten.Image) error {
if world.Debug <= 0 {
return nil
}
if !s.initialized {
s.Initialize()
}
s.debugImg.Fill(color.RGBA{0, 0, 0, 80})
ebitenutil.DebugPrintAt(s.debugImg, fmt.Sprintf("ENT %d\nUPD %d\nDRA %d\nTPS %0.0f\nFPS %0.0f", gohan.CurrentEntities(), gohan.CurrentUpdates(), gohan.CurrentDraws(), ebiten.CurrentTPS(), ebiten.CurrentFPS()), 2, 0)
screen.DrawImage(s.debugImg, s.op)
return nil
}

22
system/renderenvironment.go

@ -0,0 +1,22 @@
package system
import (
"image/color"
"code.rocketnine.space/tslocum/commandeuropa/component"
"code.rocketnine.space/tslocum/gohan"
"github.com/hajimehoshi/ebiten/v2"
)
type RenderEnvironment struct {
Once *component.Once
}
func (r *RenderEnvironment) Update(e gohan.Entity) error {
return gohan.ErrUnregister
}
func (r *RenderEnvironment) Draw(e gohan.Entity, screen *ebiten.Image) error {
screen.Fill(color.RGBA{0, 0, 255, 255})
return nil
}
Loading…
Cancel
Save