Browse Source

Propagate entity changes before each system update and draw

main
Trevor Slocum 6 months ago
parent
commit
d6caa67781
  1. 2
      go.mod
  2. 4
      go.sum
  3. 13
      world.go

2
go.mod

@ -2,7 +2,7 @@ module code.rocketnine.space/tslocum/gohan
go 1.17
require github.com/hajimehoshi/ebiten/v2 v2.3.3
require github.com/hajimehoshi/ebiten/v2 v2.3.4
require (
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20220516021902-eb3e265c7661 // indirect

4
go.sum

@ -5,8 +5,8 @@ github.com/go-gl/glfw/v3.3/glfw v0.0.0-20220516021902-eb3e265c7661/go.mod h1:tQ2
github.com/gofrs/flock v0.8.1 h1:+gYjHKf32LDeiEEFhQaotPbLuUXjY5ZqxKgXy7n59aw=
github.com/gofrs/flock v0.8.1/go.mod h1:F1TvTiK9OcQqauNUHlbJvyl9Qa1QvF/gOUDKA14jxHU=
github.com/hajimehoshi/bitmapfont/v2 v2.2.0/go.mod h1:Llj2wTYXMuCTJEw2ATNIO6HbFPOoBYPs08qLdFAxOsQ=
github.com/hajimehoshi/ebiten/v2 v2.3.3 h1:v72UzprVvWGE+HGcypkLI9Ikd237fqzpio5idPk9KNI=
github.com/hajimehoshi/ebiten/v2 v2.3.3/go.mod h1:vxwpo0q0oSi1cIll0Q3Ui33TVZgeHuFVYzIRk7FwuVk=
github.com/hajimehoshi/ebiten/v2 v2.3.4 h1:PEPbid818lZCScgUReBt13r9THPJir/Wc49DGIWdw2M=
github.com/hajimehoshi/ebiten/v2 v2.3.4/go.mod h1:vxwpo0q0oSi1cIll0Q3Ui33TVZgeHuFVYzIRk7FwuVk=
github.com/hajimehoshi/file2byteslice v0.0.0-20210813153925-5340248a8f41/go.mod h1:CqqAHp7Dk/AqQiwuhV1yT2334qbA/tFWQW0MD2dGqUE=
github.com/hajimehoshi/go-mp3 v0.3.3/go.mod h1:qMJj/CSDxx6CGHiZeCgbiq2DSUkbK0UbtXShQcnfyMM=
github.com/hajimehoshi/oto v0.6.1/go.mod h1:0QXGEkbuJRohbJaxr7ZQSxnju7hEhseiPx2hrh6raOI=

13
world.go

@ -185,6 +185,8 @@ func (w *world) setSystemComponentFields(e Entity, i int) {
}
func (w *world) updateSystem(i int) (int, error) {
w.propagateEntityChanges()
updated := 0
for _, entity := range w.systemEntities[i] {
w.setSystemComponentFields(entity, i)
@ -291,8 +293,6 @@ func Update() error {
w.Lock()
defer w.Unlock()
w.propagateEntityChanges()
var t time.Time
if debug != 0 {
t = time.Now()
@ -303,6 +303,7 @@ func Update() error {
if !registered {
continue
}
updated, err := w.updateSystem(i)
if err != nil {
return err
@ -311,7 +312,7 @@ func Update() error {
entitiesUpdated += updated
if debug != 0 {
log.Printf("- %s: %d updated.", w.systemName(i), updated)
log.Printf("- %s: %d updated in %.2fms.", w.systemName(i), updated, float64(time.Since(t).Microseconds())/1000)
}
}
w.systemUpdatedEntities = entitiesUpdated
@ -334,6 +335,8 @@ func CurrentUpdates() int {
}
func (w *world) drawSystem(i int, screen *ebiten.Image) (int, error) {
w.propagateEntityChanges()
var drawn int
for _, entity := range w.systemEntities[i] {
w.setSystemComponentFields(entity, i)
@ -357,8 +360,6 @@ func Draw(screen *ebiten.Image) error {
w.Lock()
defer w.Unlock()
w.propagateEntityChanges()
var t time.Time
if debug != 0 {
t = time.Now()
@ -378,7 +379,7 @@ func Draw(screen *ebiten.Image) error {
entitiesDrawn += drawn
if debug != 0 {
log.Printf("- %s: %d drawn.", w.systemName(i), drawn)
log.Printf("- %s: %d drawn in %.2fms.", w.systemName(i), drawn, float64(time.Since(t).Microseconds())/1000)
}
}
w.systemDrawnEntities = entitiesDrawn

Loading…
Cancel
Save