From f22852ba2ea6b0f3fbadd1381ca8871ead827867 Mon Sep 17 00:00:00 2001 From: bitver Date: Wed, 14 May 2025 09:55:31 +0500 Subject: [PATCH] revert to old iterators --- examples/new-api/config/config.go | 5 ++ examples/new-api/main.go | 3 +- examples/new-api/systems/asterodd.go | 6 +-- examples/new-api/systems/camera-main.go | 2 +- examples/new-api/systems/camera-minimap.go | 2 +- examples/new-api/systems/collision-handler.go | 2 +- examples/new-api/systems/damping.go | 9 ++-- examples/new-api/systems/debug-info.go | 8 +-- examples/new-api/systems/hp.go | 4 +- examples/new-api/systems/player.go | 6 +-- examples/new-api/systems/render-bogdan.go | 18 +++---- examples/new-api/systems/render-overlay.go | 16 +++--- examples/new-api/systems/space-spawner.go | 2 +- examples/new-api/systems/spaceship-intents.go | 2 +- examples/new-api/systems/spatial-audio.go | 2 +- pkg/ecs/component-manager-shared.go | 8 +-- pkg/ecs/component-manager.go | 10 ++-- pkg/ecs/component-table_bench_test.go | 13 +++++ pkg/ecs/paged-array.go | 51 +++++++++---------- stdcomponents/camera.go | 2 +- stdsystems/animation-spritematrix.go | 2 +- stdsystems/collision-detection-bvh.go | 4 +- stdsystems/collision-detection.go | 6 +-- stdsystems/collision-reslution.go | 8 +-- stdsystems/collision-setup.go | 6 +-- stdsystems/culling.go | 2 +- stdsystems/render-2d-cameras.go | 4 +- stdsystems/render.go | 2 +- stdsystems/sprite-matrix.go | 2 +- stdsystems/ysort.go | 5 +- 30 files changed, 113 insertions(+), 99 deletions(-) diff --git a/examples/new-api/config/config.go b/examples/new-api/config/config.go index abf32ad5..0113c4db 100644 --- a/examples/new-api/config/config.go +++ b/examples/new-api/config/config.go @@ -16,6 +16,11 @@ package config import "gomp/stdcomponents" +const ( + TickRate = 20 + FrameRate = 0 +) + const ( DefaultCollisionLayer stdcomponents.CollisionLayer = iota PlayerCollisionLayer diff --git a/examples/new-api/main.go b/examples/new-api/main.go index 9d28c331..c07cedb3 100644 --- a/examples/new-api/main.go +++ b/examples/new-api/main.go @@ -18,6 +18,7 @@ import ( "github.com/hajimehoshi/go-steamworks" "golang.org/x/text/language" "gomp" + "gomp/examples/new-api/config" "gomp/examples/new-api/scenes" "os" ) @@ -53,5 +54,5 @@ func main() { game := NewGame(&initialScene) engine := gomp.NewEngine(&game) - engine.Run(20, 0) + engine.Run(config.TickRate, config.FrameRate) } diff --git a/examples/new-api/systems/asterodd.go b/examples/new-api/systems/asterodd.go index 24c356f2..68bfdf84 100644 --- a/examples/new-api/systems/asterodd.go +++ b/examples/new-api/systems/asterodd.go @@ -181,7 +181,7 @@ func (s *AssteroddSystem) Init() { s.SceneManager.Create(manager, components.AsteroidSceneManager{}) } func (s *AssteroddSystem) Run(dt time.Duration) { - s.PlayerTags.EachEntity()(func(e ecs.Entity) bool { + s.PlayerTags.EachEntity(func(e ecs.Entity) bool { intents := s.SpaceshipIntents.GetUnsafe(e) intents.MoveUp = false @@ -211,9 +211,9 @@ func (s *AssteroddSystem) Run(dt time.Duration) { return true }) - s.SceneManager.EachEntity()(func(e ecs.Entity) bool { + s.SceneManager.EachEntity(func(e ecs.Entity) bool { sceneManager := s.SceneManager.GetUnsafe(e) - s.PlayerTags.EachEntity()(func(e ecs.Entity) bool { + s.PlayerTags.EachEntity(func(e ecs.Entity) bool { playerHp := s.Hps.GetUnsafe(e) if playerHp == nil { return true diff --git a/examples/new-api/systems/camera-main.go b/examples/new-api/systems/camera-main.go index ca067983..65bd8f9d 100644 --- a/examples/new-api/systems/camera-main.go +++ b/examples/new-api/systems/camera-main.go @@ -95,7 +95,7 @@ func (s *MainCameraSystem) Run(dt time.Duration) { if rl.IsKeyPressed(rl.KeyR) { s.shouldRotate = !s.shouldRotate } - s.Player.EachEntity()(func(entity ecs.Entity) bool { + s.Player.EachEntity(func(entity ecs.Entity) bool { playerPosition := s.Position.GetUnsafe(entity) c := s.Cameras.GetUnsafe(s.mainCamera) //decay := 40.0 // DECAY IS TICKRATE DEPENDENT diff --git a/examples/new-api/systems/camera-minimap.go b/examples/new-api/systems/camera-minimap.go index fc5cc07f..6ca1c197 100644 --- a/examples/new-api/systems/camera-minimap.go +++ b/examples/new-api/systems/camera-minimap.go @@ -81,7 +81,7 @@ func (s *MinimapSystem) Run(dt time.Duration) bool { if s.disabled { return false } - s.Player.EachEntity()(func(entity ecs.Entity) bool { + s.Player.EachEntity(func(entity ecs.Entity) bool { playerPosition := s.Position.GetUnsafe(entity) rotation := s.Rotation.GetUnsafe(entity) diff --git a/examples/new-api/systems/collision-handler.go b/examples/new-api/systems/collision-handler.go index 0db83b12..f1cf620a 100644 --- a/examples/new-api/systems/collision-handler.go +++ b/examples/new-api/systems/collision-handler.go @@ -49,7 +49,7 @@ type CollisionHandlerSystem struct { func (s *CollisionHandlerSystem) Init() {} func (s *CollisionHandlerSystem) Run(dt time.Duration) { - s.Collisions.EachComponent()(func(collision *stdcomponents.Collision) bool { + s.Collisions.EachComponent(func(collision *stdcomponents.Collision) bool { switch collision.State { case stdcomponents.CollisionStateEnter: if s.checkBulletCollisionEnter(collision.E1, collision.E2) { diff --git a/examples/new-api/systems/damping.go b/examples/new-api/systems/damping.go index 7b9e8a9b..662aaf4c 100644 --- a/examples/new-api/systems/damping.go +++ b/examples/new-api/systems/damping.go @@ -7,7 +7,9 @@ with this file, You can obtain one at http://mozilla.org/MPL/2.0/. package systems import ( + "gomp/examples/new-api/config" "gomp/pkg/ecs" + "gomp/pkg/worker" "gomp/stdcomponents" "time" ) @@ -29,13 +31,13 @@ const ( func (s *DampingSystem) Init() {} func (s *DampingSystem) Run(dt time.Duration) { - s.Velocities.EachEntity()(func(e ecs.Entity) bool { + s.Velocities.ProcessEntities(func(e ecs.Entity, _ worker.WorkerId) { velocity := s.Velocities.GetUnsafe(e) rigidbody := s.RigidBodies.GetUnsafe(e) if rigidbody != nil && !rigidbody.IsStatic { - velocity.X *= dampingFactor - velocity.Y *= dampingFactor + velocity.X *= dampingFactor / (config.TickRate * float32(dt.Seconds())) + velocity.Y *= dampingFactor / (config.TickRate * float32(dt.Seconds())) if velocity.X < 0.1 && velocity.X > -0.1 { velocity.X = 0 } @@ -43,7 +45,6 @@ func (s *DampingSystem) Run(dt time.Duration) { velocity.Y = 0 } } - return true }) } diff --git a/examples/new-api/systems/debug-info.go b/examples/new-api/systems/debug-info.go index b2651356..126ab105 100644 --- a/examples/new-api/systems/debug-info.go +++ b/examples/new-api/systems/debug-info.go @@ -60,7 +60,7 @@ func (s *DebugInfoSystem) Init() { func (s *DebugInfoSystem) Run(dt time.Duration) bool { if rl.IsKeyPressed(rl.KeyF6) { if !s.debug { - s.BoxColliders.EachEntity()(func(e ecs.Entity) bool { + s.BoxColliders.EachEntity(func(e ecs.Entity) bool { col := s.BoxColliders.GetUnsafe(e) scale := s.Scales.GetUnsafe(e) position := s.Positions.GetUnsafe(e) @@ -76,7 +76,7 @@ func (s *DebugInfoSystem) Run(dt time.Duration) bool { }, float32(rotation.Degrees()), rl.DarkGreen, e) return true }) - s.CircleColliders.EachEntity()(func(e ecs.Entity) bool { + s.CircleColliders.EachEntity(func(e ecs.Entity) bool { col := s.CircleColliders.GetUnsafe(e) scale := s.Scales.GetUnsafe(e) pos := s.Positions.GetUnsafe(e) @@ -103,7 +103,7 @@ func (s *DebugInfoSystem) Run(dt time.Duration) bool { // TODO: Parallelize this with future batches feature // Follow child to texture of parent box collider - s.BoxColliders.EachEntity()(func(e ecs.Entity) bool { + s.BoxColliders.EachEntity(func(e ecs.Entity) bool { parentAABB := s.AABBs.GetUnsafe(e) parentPosition := s.Positions.GetUnsafe(e) col := s.BoxColliders.GetUnsafe(e) @@ -145,7 +145,7 @@ func (s *DebugInfoSystem) Run(dt time.Duration) bool { }) // TODO: Parallelize this with future batches feature // Follow child to texture of parent circle collider - s.CircleColliders.EachEntity()(func(e ecs.Entity) bool { + s.CircleColliders.EachEntity(func(e ecs.Entity) bool { parentAABB := s.AABBs.GetUnsafe(e) pos := s.Positions.GetUnsafe(e) col := s.CircleColliders.GetUnsafe(e) diff --git a/examples/new-api/systems/hp.go b/examples/new-api/systems/hp.go index a6fe547e..b6907ccb 100644 --- a/examples/new-api/systems/hp.go +++ b/examples/new-api/systems/hp.go @@ -35,13 +35,13 @@ type HpSystem struct { func (s *HpSystem) Init() {} func (s *HpSystem) Run(dt time.Duration) { - s.Hps.EachEntity()(func(e ecs.Entity) bool { + s.Hps.EachEntity(func(e ecs.Entity) bool { hp := s.Hps.GetUnsafe(e) if hp.Hp <= 0 { asteroid := s.Asteroids.GetUnsafe(e) player := s.Players.GetUnsafe(e) - s.AsteroidSceneManager.EachComponent()(func(a *components.AsteroidSceneManager) bool { + s.AsteroidSceneManager.EachComponent(func(a *components.AsteroidSceneManager) bool { if asteroid != nil { a.PlayerScore += hp.MaxHp } diff --git a/examples/new-api/systems/player.go b/examples/new-api/systems/player.go index 9c8875a7..984c5688 100644 --- a/examples/new-api/systems/player.go +++ b/examples/new-api/systems/player.go @@ -75,7 +75,7 @@ func (s *PlayerSystem) Run() { var speed float32 = 300 - for e := range s.Controllers.EachEntity() { + s.Controllers.EachEntity(func(e ecs.Entity) bool { velocity := s.Velocities.GetUnsafe(e) flip := s.Flips.GetUnsafe(e) animationState := s.AnimationStates.GetUnsafe(e) @@ -110,7 +110,7 @@ func (s *PlayerSystem) Run() { if rl.IsKeyPressed(rl.KeyK) { s.EntityManager.Delete(e) } - } - + return true + }) } func (s *PlayerSystem) Destroy() {} diff --git a/examples/new-api/systems/render-bogdan.go b/examples/new-api/systems/render-bogdan.go index 591cf93b..38cd1389 100644 --- a/examples/new-api/systems/render-bogdan.go +++ b/examples/new-api/systems/render-bogdan.go @@ -81,14 +81,14 @@ func (s *RenderBogdanSystem) Run(dt time.Duration) bool { rl.DrawLine(0, i*gridSize, 1024, i*gridSize, rl.Green) } s.render() - s.ColliderBoxes.EachEntity()(func(e ecs.Entity) bool { + s.ColliderBoxes.EachEntity(func(e ecs.Entity) bool { box := s.ColliderBoxes.GetUnsafe(e) pos := s.Positions.GetUnsafe(e) rl.DrawRectangleLines(int32(pos.XY.X), int32(pos.XY.Y), int32(box.WH.X), int32(box.WH.Y), rl.Red) return true }) - s.Collisions.EachEntity()(func(entity ecs.Entity) bool { + s.Collisions.EachEntity(func(entity ecs.Entity) bool { pos := s.Positions.GetUnsafe(entity) rl.DrawRectangle(int32(pos.XY.X), int32(pos.XY.X), 16, 16, rl.Red) return true @@ -108,7 +108,7 @@ func (s *RenderBogdanSystem) render() { if cap(s.renderList) < s.Renderables.Len() { s.renderList = append(s.renderList, make([]renderEntry, 0, s.Renderables.Len()-cap(s.renderList))...) } - s.Renderables.EachEntity()(func(e ecs.Entity) bool { + s.Renderables.EachEntity(func(e ecs.Entity) bool { sprite := s.SpriteMatrixes.Get(e) renderOrder := s.RenderOrders.GetUnsafe(e) s.renderList = append(s.renderList, renderEntry{ @@ -162,7 +162,7 @@ func (s *RenderBogdanSystem) prepareRender(dt time.Duration) { func (s *RenderBogdanSystem) prepareAnimations(wg *sync.WaitGroup) { defer wg.Done() - s.RlTexturePros.EachEntity()(func(entity ecs.Entity) bool { + s.RlTexturePros.EachEntity(func(entity ecs.Entity) bool { texturePro := s.RlTexturePros.GetUnsafe(entity) animation := s.AnimationPlayers.GetUnsafe(entity) if animation == nil { @@ -180,7 +180,7 @@ func (s *RenderBogdanSystem) prepareAnimations(wg *sync.WaitGroup) { func (s *RenderBogdanSystem) prepareFlips(wg *sync.WaitGroup) { defer wg.Done() - s.RlTexturePros.EachEntity()(func(entity ecs.Entity) bool { + s.RlTexturePros.EachEntity(func(entity ecs.Entity) bool { texturePro := s.RlTexturePros.GetUnsafe(entity) mirrored := s.Flips.GetUnsafe(entity) if mirrored == nil { @@ -199,7 +199,7 @@ func (s *RenderBogdanSystem) prepareFlips(wg *sync.WaitGroup) { func (s *RenderBogdanSystem) preparePositions(wg *sync.WaitGroup, dt time.Duration) { defer wg.Done() //dts := dt.Seconds() - s.RlTexturePros.EachEntity()(func(entity ecs.Entity) bool { + s.RlTexturePros.EachEntity(func(entity ecs.Entity) bool { texturePro := s.RlTexturePros.GetUnsafe(entity) position := s.Positions.GetUnsafe(entity) if position == nil { @@ -217,7 +217,7 @@ func (s *RenderBogdanSystem) preparePositions(wg *sync.WaitGroup, dt time.Durati func (s *RenderBogdanSystem) prepareRotations(wg *sync.WaitGroup) { defer wg.Done() - s.RlTexturePros.EachEntity()(func(entity ecs.Entity) bool { + s.RlTexturePros.EachEntity(func(entity ecs.Entity) bool { texturePro := s.RlTexturePros.GetUnsafe(entity) rotation := s.Rotations.GetUnsafe(entity) if rotation == nil { @@ -230,7 +230,7 @@ func (s *RenderBogdanSystem) prepareRotations(wg *sync.WaitGroup) { func (s *RenderBogdanSystem) prepareScales(wg *sync.WaitGroup) { defer wg.Done() - s.RlTexturePros.EachEntity()(func(entity ecs.Entity) bool { + s.RlTexturePros.EachEntity(func(entity ecs.Entity) bool { texturePro := s.RlTexturePros.GetUnsafe(entity) scale := s.Scales.GetUnsafe(entity) if scale == nil { @@ -244,7 +244,7 @@ func (s *RenderBogdanSystem) prepareScales(wg *sync.WaitGroup) { func (s *RenderBogdanSystem) prepareTints(wg *sync.WaitGroup) { defer wg.Done() - s.RlTexturePros.EachEntity()(func(entity ecs.Entity) bool { + s.RlTexturePros.EachEntity(func(entity ecs.Entity) bool { tr := s.RlTexturePros.GetUnsafe(entity) tint := s.Tints.GetUnsafe(entity) if tint == nil { diff --git a/examples/new-api/systems/render-overlay.go b/examples/new-api/systems/render-overlay.go index a7049979..25e3ac1a 100644 --- a/examples/new-api/systems/render-overlay.go +++ b/examples/new-api/systems/render-overlay.go @@ -154,7 +154,7 @@ func (s *RenderOverlaySystem) Run(dt time.Duration) bool { } } - s.Cameras.EachEntity()(func(entity ecs.Entity) bool { + s.Cameras.EachEntity(func(entity ecs.Entity) bool { camera := s.Cameras.GetUnsafe(entity) fb := s.FrameBuffer2D.GetUnsafe(entity) switch fb.Layer { @@ -168,7 +168,7 @@ func (s *RenderOverlaySystem) Run(dt time.Duration) bool { rl.BeginMode2D(camera.Camera2D) cameraRect := camera.Rect() - s.CollisionCells.EachEntity()(func(e ecs.Entity) bool { + s.CollisionCells.EachEntity(func(e ecs.Entity) bool { cell := s.CollisionCells.GetUnsafe(e) assert.NotNil(cell) @@ -200,7 +200,7 @@ func (s *RenderOverlaySystem) Run(dt time.Duration) bool { } return true }) - s.CollisionChunks.EachEntity()(func(e ecs.Entity) bool { + s.CollisionChunks.EachEntity(func(e ecs.Entity) bool { chunk := s.CollisionChunks.GetUnsafe(e) assert.NotNil(chunk) @@ -217,7 +217,7 @@ func (s *RenderOverlaySystem) Run(dt time.Duration) bool { tree := s.BvhTrees.GetUnsafe(e) assert.NotNil(tree) - tree.AabbNodes.EachData()(func(a *stdcomponents.AABB) bool { + tree.AabbNodes.EachData(func(a *stdcomponents.AABB) bool { // Simple AABB culling if s.intersects(cameraRect, a.Rect()) { rl.DrawRectangleRec(rl.Rectangle{ @@ -248,7 +248,7 @@ func (s *RenderOverlaySystem) Run(dt time.Duration) bool { } return true }) - s.AABBs.EachEntity()(func(e ecs.Entity) bool { + s.AABBs.EachEntity(func(e ecs.Entity) bool { aabb := s.AABBs.GetUnsafe(e) clr := rl.Green isSleeping := s.ColliderSleepStateComponentManager.GetUnsafe(e) @@ -265,7 +265,7 @@ func (s *RenderOverlaySystem) Run(dt time.Duration) bool { } return true }) - s.Collisions.EachEntity()(func(entity ecs.Entity) bool { + s.Collisions.EachEntity(func(entity ecs.Entity) bool { pos := s.Positions.GetUnsafe(entity) rec := vectors.Rectangle{ X: pos.XY.X - 8, @@ -278,7 +278,7 @@ func (s *RenderOverlaySystem) Run(dt time.Duration) bool { } return true }) - s.Textures.EachComponent()(func(r *stdcomponents.RLTexturePro) bool { + s.Textures.EachComponent(func(r *stdcomponents.RLTexturePro) bool { rec := vectors.Rectangle{ X: r.Dest.X - 2, Y: r.Dest.Y - 2, @@ -304,7 +304,7 @@ func (s *RenderOverlaySystem) Run(dt time.Duration) bool { rl.DrawText(fmt.Sprintf("%d entities", s.EntityManager.Size()), x, y+fontSize*6, fontSize, rl.RayWhite) rl.DrawText(fmt.Sprintf("%d debugLvl", s.debugLvl), x, y+fontSize*7, 20, rl.RayWhite) // Game over - s.SceneManager.EachComponent()(func(a *components.AsteroidSceneManager) bool { + s.SceneManager.EachComponent(func(a *components.AsteroidSceneManager) bool { rl.DrawText(fmt.Sprintf("Player HP: %d", a.PlayerHp), x, y+fontSize*4, 20, rl.RayWhite) rl.DrawText(fmt.Sprintf("Score: %d", a.PlayerScore), x, y+fontSize*5, 20, rl.RayWhite) if a.PlayerHp <= 0 { diff --git a/examples/new-api/systems/space-spawner.go b/examples/new-api/systems/space-spawner.go index 9d63916d..36163858 100644 --- a/examples/new-api/systems/space-spawner.go +++ b/examples/new-api/systems/space-spawner.go @@ -47,7 +47,7 @@ type SpaceSpawnerSystem struct { func (s *SpaceSpawnerSystem) Init() {} func (s *SpaceSpawnerSystem) Run(dt time.Duration) { - s.SpaceSpawners.EachEntity()(func(e ecs.Entity) bool { + s.SpaceSpawners.EachEntity(func(e ecs.Entity) bool { position := s.Positions.GetUnsafe(e) velocity := s.Velocities.GetUnsafe(e) diff --git a/examples/new-api/systems/spaceship-intents.go b/examples/new-api/systems/spaceship-intents.go index 0ededb21..ee95d448 100644 --- a/examples/new-api/systems/spaceship-intents.go +++ b/examples/new-api/systems/spaceship-intents.go @@ -62,7 +62,7 @@ func (s *SpaceshipIntentsSystem) Run(dt time.Duration) { dtSec := float32(dt.Seconds()) - s.SpaceshipIntents.EachEntity()(func(entity ecs.Entity) bool { + s.SpaceshipIntents.EachEntity(func(entity ecs.Entity) bool { intent := s.SpaceshipIntents.GetUnsafe(entity) vel := s.Velocities.GetUnsafe(entity) rot := s.Rotations.GetUnsafe(entity) diff --git a/examples/new-api/systems/spatial-audio.go b/examples/new-api/systems/spatial-audio.go index 469a7651..b03baa15 100644 --- a/examples/new-api/systems/spatial-audio.go +++ b/examples/new-api/systems/spatial-audio.go @@ -58,7 +58,7 @@ func (s *SpatialAudioSystem) Run(dt time.Duration) { var mainCamera ecs.Entity // TODO: Add listener component? Then we need position component on it... - s.Cameras.EachEntity()(func(entity ecs.Entity) bool { + s.Cameras.EachEntity(func(entity ecs.Entity) bool { camera := s.Cameras.GetUnsafe(entity) assert.NotNil(camera) if camera.Layer == config.MainCameraLayer { diff --git a/pkg/ecs/component-manager-shared.go b/pkg/ecs/component-manager-shared.go index 28ad937d..1e7399c1 100644 --- a/pkg/ecs/component-manager-shared.go +++ b/pkg/ecs/component-manager-shared.go @@ -221,16 +221,16 @@ func (c *SharedComponentManager[T]) Clean() { // Iterators // ======================================================== -func (c *SharedComponentManager[T]) EachComponent() func(yield func(*T) bool) { +func (c *SharedComponentManager[T]) EachComponent(yield func(*T) bool) { c.assertBegin() defer c.assertEnd() - return c.components.EachData() + c.components.EachData(yield) } -func (c *SharedComponentManager[T]) EachEntity() func(yield func(Entity) bool) { +func (c *SharedComponentManager[T]) EachEntity(yield func(Entity) bool) { c.assertBegin() defer c.assertEnd() - return c.entities.EachDataValue() + c.entities.EachDataValue(yield) } func (c *SharedComponentManager[T]) Each() func(yield func(Entity, *T) bool) { diff --git a/pkg/ecs/component-manager.go b/pkg/ecs/component-manager.go index 57c0ae43..09785e38 100644 --- a/pkg/ecs/component-manager.go +++ b/pkg/ecs/component-manager.go @@ -244,16 +244,16 @@ func (c *ComponentManager[T]) Clean() { // Iterators // ======================================================== -func (c *ComponentManager[T]) EachComponent() func(yield func(entity *T) bool) { +func (c *ComponentManager[T]) EachComponent(yield func(entity *T) bool) { c.assertBegin() defer c.assertEnd() - return c.components.EachData() + c.components.EachData(yield) } -func (c *ComponentManager[T]) EachEntity() func(yield func(entity Entity) bool) { +func (c *ComponentManager[T]) EachEntity(yield func(entity Entity) bool) { c.assertBegin() defer c.assertEnd() - return c.entities.EachDataValue() + c.entities.EachDataValue(yield) } func (c *ComponentManager[T]) Each() func(yield func(entity Entity, component *T) bool) { @@ -369,7 +369,7 @@ func (c *ComponentManager[T]) getChangesBinary(source *PagedArray[Entity]) Compo components := make([]T, 0, changesLen) entities := make([]Entity, 0, changesLen) - source.EachData()(func(e *Entity) bool { + source.EachData(func(e *Entity) bool { assert.True(e != nil) entId := *e assert.True(c.Has(entId)) diff --git a/pkg/ecs/component-table_bench_test.go b/pkg/ecs/component-table_bench_test.go index 3eff8527..2265ad71 100644 --- a/pkg/ecs/component-table_bench_test.go +++ b/pkg/ecs/component-table_bench_test.go @@ -7,7 +7,20 @@ const maxComponentsLen = 1024 func BenchmarkComponentBitTable_SetAndTest(b *testing.B) { // using a fixed maximum components length + // create and extend the table table := NewComponentBitTable(maxComponentsLen) + for i := Entity(0); i < testEntitiesLen; i++ { + comp := ComponentId(i % maxComponentsLen) + table.Create(i) + table.Set(i, comp) + if !table.Test(i, comp) { + b.Fatalf("BitTable: expected entity %d to have component %d set", i, comp) + } + } + for i := Entity(0); i < testEntitiesLen; i++ { + table.Delete(i) + } + b.ReportAllocs() for b.Loop() { for i := Entity(0); i < testEntitiesLen; i++ { diff --git a/pkg/ecs/paged-array.go b/pkg/ecs/paged-array.go index 59a5765f..dbc6fd0e 100644 --- a/pkg/ecs/paged-array.go +++ b/pkg/ecs/paged-array.go @@ -115,7 +115,6 @@ func (a *PagedArray[T]) extend() { } func (a *PagedArray[T]) Append(value T) *T { - var result *T if a.currentPageIndex >= len(a.book) { a.extend() } @@ -130,7 +129,7 @@ func (a *PagedArray[T]) Append(value T) *T { page = a.book[a.currentPageIndex] } page.data[page.len] = value - result = &page.data[page.len] + result := &page.data[page.len] page.len++ a.len++ return result @@ -298,43 +297,39 @@ func (a *PagedArray[T]) EachParallel(numWorkers int) func(yield func(int, *T, in } } -func (a *PagedArray[T]) EachData() func(yield func(*T) bool) { - return func(yield func(*T) bool) { - var page *ArrayPage[T] - var book = a.book +func (a *PagedArray[T]) EachData(yield func(*T) bool) { + var page *ArrayPage[T] + var book = a.book - if a.len == 0 { - return - } + if a.len == 0 { + return + } - for i := a.currentPageIndex; i >= 0; i-- { - page = book[i] + for i := a.currentPageIndex; i >= 0; i-- { + page = book[i] - for j := page.len - 1; j >= 0; j-- { - if !yield(&page.data[j]) { - return - } + for j := page.len - 1; j >= 0; j-- { + if !yield(&page.data[j]) { + return } } } } -func (a *PagedArray[T]) EachDataValue() func(yield func(T) bool) { - return func(yield func(T) bool) { - var page *ArrayPage[T] - var book = a.book +func (a *PagedArray[T]) EachDataValue(yield func(T) bool) { + var page *ArrayPage[T] + var book = a.book - if a.len == 0 { - return - } + if a.len == 0 { + return + } - for i := a.currentPageIndex; i >= 0; i-- { - page = book[i] + for i := a.currentPageIndex; i >= 0; i-- { + page = book[i] - for j := page.len - 1; j >= 0; j-- { - if !yield(page.data[j]) { - return - } + for j := page.len - 1; j >= 0; j-- { + if !yield(page.data[j]) { + return } } } diff --git a/stdcomponents/camera.go b/stdcomponents/camera.go index c8efe378..8bcfbe8d 100644 --- a/stdcomponents/camera.go +++ b/stdcomponents/camera.go @@ -49,7 +49,7 @@ type Camera struct { Tint color.RGBA } -func (c Camera) Rect() vectors.Rectangle { +func (c *Camera) Rect() vectors.Rectangle { // Calculate the non-rotated top-left corner of the view rectangle x := c.Target.X - (c.Offset.X / c.Zoom) y := c.Target.Y - (c.Offset.Y / c.Zoom) diff --git a/stdsystems/animation-spritematrix.go b/stdsystems/animation-spritematrix.go index 634056bc..62bd10c6 100644 --- a/stdsystems/animation-spritematrix.go +++ b/stdsystems/animation-spritematrix.go @@ -25,7 +25,7 @@ type AnimationSpriteMatrixSystem struct { func (s *AnimationSpriteMatrixSystem) Init() {} func (s *AnimationSpriteMatrixSystem) Run() { - s.AnimationPlayers.EachEntity()(func(e ecs.Entity) bool { + s.AnimationPlayers.EachEntity(func(e ecs.Entity) bool { animationPlayer := s.AnimationPlayers.GetUnsafe(e) spriteMatrix := s.SpriteMatrixes.Get(e) if spriteMatrix == nil { diff --git a/stdsystems/collision-detection-bvh.go b/stdsystems/collision-detection-bvh.go index 1ceb07a2..dad9f7d2 100644 --- a/stdsystems/collision-detection-bvh.go +++ b/stdsystems/collision-detection-bvh.go @@ -79,7 +79,7 @@ func (s *CollisionDetectionBVHSystem) Run(dt time.Duration) { } // Fill trees - s.GenericCollider.EachEntity()(func(entity ecs.Entity) bool { + s.GenericCollider.EachEntity(func(entity ecs.Entity) bool { aabb := s.AABB.GetUnsafe(entity) layer := s.GenericCollider.GetUnsafe(entity).Layer @@ -126,7 +126,7 @@ func (s *CollisionDetectionBVHSystem) findEntityCollisions() { func (s *CollisionDetectionBVHSystem) registerCollisionEvents() { for i := range s.collisionEvents { events := &s.collisionEvents[i] - events.EachData()(func(event *CollisionEvent) bool { + events.EachData(func(event *CollisionEvent) bool { pair := CollisionPair{event.entityA, event.entityB} s.currentCollisions[pair] = struct{}{} displacement := event.normal.Scale(event.depth) diff --git a/stdsystems/collision-detection.go b/stdsystems/collision-detection.go index 796f275e..1b8a7040 100644 --- a/stdsystems/collision-detection.go +++ b/stdsystems/collision-detection.go @@ -69,7 +69,7 @@ func (s *CollisionDetectionSystem) Init() { } func (s *CollisionDetectionSystem) Run(dt time.Duration) { - s.CollisionGridComponentManager.EachComponent()(func(grid *stdcomponents.CollisionGrid) bool { + s.CollisionGridComponentManager.EachComponent(func(grid *stdcomponents.CollisionGrid) bool { s.gridLookup[grid.Layer] = grid return true }) @@ -188,7 +188,7 @@ func (s *CollisionDetectionSystem) narrowPhase(entityA ecs.Entity, potentialEnti Scale: scaleA.XY, } - for entityB := range potentialEntities.EachDataValue() { + for entityB := range potentialEntities.EachDataValue { aabbB := s.AABB.GetUnsafe(entityB) assert.NotNil(aabbB) @@ -262,7 +262,7 @@ func (s *CollisionDetectionSystem) narrowPhase(entityA ecs.Entity, potentialEnti func (s *CollisionDetectionSystem) registerCollisionEvents() { for i := range s.collisionEventAcc { events := &s.collisionEventAcc[i] - events.EachData()(func(event *CollisionEvent) bool { + events.EachData(func(event *CollisionEvent) bool { pair := CollisionPair{event.entityA, event.entityB} s.currentCollisions[pair] = struct{}{} displacement := event.normal.Scale(event.depth) diff --git a/stdsystems/collision-reslution.go b/stdsystems/collision-reslution.go index 1698658a..a5f9b3c2 100644 --- a/stdsystems/collision-reslution.go +++ b/stdsystems/collision-reslution.go @@ -17,6 +17,7 @@ package stdsystems import ( "github.com/negrel/assert" "gomp/pkg/ecs" + "gomp/pkg/worker" "gomp/stdcomponents" "gomp/vectors" "time" @@ -36,7 +37,7 @@ type CollisionResolutionSystem struct { func (s *CollisionResolutionSystem) Init() {} func (s *CollisionResolutionSystem) Run(dt time.Duration) { - s.Collisions.EachComponent()(func(collision *stdcomponents.Collision) bool { + s.Collisions.ProcessComponents(func(collision *stdcomponents.Collision, _ worker.WorkerId) { if collision.State == stdcomponents.CollisionStateEnter || collision.State == stdcomponents.CollisionStateStay { // Resolve penetration var displacement vectors.Vec2 @@ -46,7 +47,7 @@ func (s *CollisionResolutionSystem) Run(dt time.Duration) { rigidbody2 := s.RigidBodies.GetUnsafe(collision.E2) if rigidbody1 == nil || rigidbody2 == nil { - return true + return } if !rigidbody1.IsStatic && !rigidbody2.IsStatic { @@ -79,7 +80,7 @@ func (s *CollisionResolutionSystem) Run(dt time.Duration) { velocityAlongNormal := relativeVelocity.Dot(collision.Normal) if velocityAlongNormal > 0 { - return true + return } e := float32(1.0) // Coefficient of restitution (elasticity) @@ -96,7 +97,6 @@ func (s *CollisionResolutionSystem) Run(dt time.Duration) { velocity2.SetVec2(velocity2.Vec2().Add(impulse.Scale(1 / rigidbody2.Mass))) } } - return true }) } func (s *CollisionResolutionSystem) Destroy() {} diff --git a/stdsystems/collision-setup.go b/stdsystems/collision-setup.go index 403552cc..ed3b9dd9 100644 --- a/stdsystems/collision-setup.go +++ b/stdsystems/collision-setup.go @@ -83,7 +83,7 @@ func (s *CollisionSetupSystem) Destroy() { } func (s *CollisionSetupSystem) setup() { // Prepare grids - s.CollisionGridComponentManager.EachEntity()(func(entity ecs.Entity) bool { + s.CollisionGridComponentManager.EachEntity(func(entity ecs.Entity) bool { grid := s.CollisionGridComponentManager.GetUnsafe(entity) assert.NotNil(grid) @@ -308,7 +308,7 @@ func (s *CollisionSetupSystem) setup() { }) for i := range s.clearCellAccumulator { v := &s.clearCellAccumulator[i] - v.EachDataValue()(func(cellEntity ecs.Entity) bool { + v.EachDataValue(func(cellEntity ecs.Entity) bool { cell := s.CollisionCellComponentManager.GetUnsafe(cellEntity) assert.NotNil(cell) @@ -318,7 +318,7 @@ func (s *CollisionSetupSystem) setup() { grid.CellMap.Delete(cell.Index) return true }) - v.EachDataValue()(func(cellEntity ecs.Entity) bool { + v.EachDataValue(func(cellEntity ecs.Entity) bool { s.EntityManager.Delete(cellEntity) return true }) diff --git a/stdsystems/culling.go b/stdsystems/culling.go index 7db4aa7b..98993ffc 100644 --- a/stdsystems/culling.go +++ b/stdsystems/culling.go @@ -52,7 +52,7 @@ func (s *CullingSystem) Run(dt time.Duration) { r.Observed = false }) - s.Cameras.EachEntity()(func(entity ecs.Entity) bool { + s.Cameras.EachEntity(func(entity ecs.Entity) bool { camera := s.Cameras.GetUnsafe(entity) cameraRect := camera.Rect() diff --git a/stdsystems/render-2d-cameras.go b/stdsystems/render-2d-cameras.go index cce1936c..ce17819f 100644 --- a/stdsystems/render-2d-cameras.go +++ b/stdsystems/render-2d-cameras.go @@ -73,7 +73,7 @@ func (s *Render2DCamerasSystem) Run(dt time.Duration) { s.renderObjectsSorted = make([]renderObjectSorted, 0, max(s.RenderVisibles.Len(), cap(s.renderObjects)*2)) } - s.RenderVisibles.EachEntity()(func(entity ecs.Entity) bool { + s.RenderVisibles.EachEntity(func(entity ecs.Entity) bool { o := s.RenderOrders.GetUnsafe(entity) assert.NotNil(o) @@ -110,7 +110,7 @@ func (s *Render2DCamerasSystem) Run(dt time.Duration) { }) } - s.Cameras.EachEntity()(func(cameraEntity ecs.Entity) bool { + s.Cameras.EachEntity(func(cameraEntity ecs.Entity) bool { camera := s.Cameras.GetUnsafe(cameraEntity) assert.NotNil(camera) renderTexture := s.RenderTexture2D.GetUnsafe(cameraEntity) diff --git a/stdsystems/render.go b/stdsystems/render.go index 496acb36..9d11fea0 100644 --- a/stdsystems/render.go +++ b/stdsystems/render.go @@ -41,7 +41,7 @@ func (s *RenderSystem) Init() { } func (s *RenderSystem) Run(dt time.Duration) bool { - s.FrameBuffer2D.EachComponent()(func(c *stdcomponents.FrameBuffer2D) bool { + s.FrameBuffer2D.EachComponent(func(c *stdcomponents.FrameBuffer2D) bool { s.frames = append(s.frames, *c) return true }) diff --git a/stdsystems/sprite-matrix.go b/stdsystems/sprite-matrix.go index 83fcdf53..537b19fa 100644 --- a/stdsystems/sprite-matrix.go +++ b/stdsystems/sprite-matrix.go @@ -26,7 +26,7 @@ type SpriteMatrixSystem struct { func (s *SpriteMatrixSystem) Init() {} func (s *SpriteMatrixSystem) Run() { - s.SpriteMatrixes.EachEntity()(func(entity ecs.Entity) bool { + s.SpriteMatrixes.EachEntity(func(entity ecs.Entity) bool { spriteMatrix := s.SpriteMatrixes.Get(entity) // position := s.Positions.GetUnsafe(entity) animationState := s.AnimationStates.GetUnsafe(entity) // diff --git a/stdsystems/ysort.go b/stdsystems/ysort.go index bc2c0360..b42f7c5b 100644 --- a/stdsystems/ysort.go +++ b/stdsystems/ysort.go @@ -16,6 +16,7 @@ package stdsystems import ( "gomp/pkg/ecs" + "gomp/pkg/worker" "gomp/stdcomponents" ) @@ -34,7 +35,7 @@ type YSortSystem struct { func (s *YSortSystem) Init() {} func (s *YSortSystem) Run() { - s.YSorts.EachEntity()(func(entity ecs.Entity) bool { + s.YSorts.ProcessEntities(func(entity ecs.Entity, _ worker.WorkerId) { pos := s.Positions.GetUnsafe(entity) renderOrder := s.RenderOrders.GetUnsafe(entity) @@ -44,8 +45,6 @@ func (s *YSortSystem) Run() { // Preserve original Z layer but add Y-based offset //renderOrder.CalculatedZ = float32(int(pos.Z)) + yDepth renderOrder.CalculatedZ = yDepth - - return true }) } func (s *YSortSystem) Destroy() {}