Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions examples/new-api/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ package config

import "gomp/stdcomponents"

const (
TickRate = 20
FrameRate = 0
)

const (
DefaultCollisionLayer stdcomponents.CollisionLayer = iota
PlayerCollisionLayer
Expand Down
3 changes: 2 additions & 1 deletion examples/new-api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -53,5 +54,5 @@ func main() {
game := NewGame(&initialScene)

engine := gomp.NewEngine(&game)
engine.Run(20, 0)
engine.Run(config.TickRate, config.FrameRate)
}
6 changes: 3 additions & 3 deletions examples/new-api/systems/asterodd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion examples/new-api/systems/camera-main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion examples/new-api/systems/camera-minimap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion examples/new-api/systems/collision-handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
9 changes: 5 additions & 4 deletions examples/new-api/systems/damping.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -29,21 +31,20 @@ 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
}
if velocity.Y < 0.1 && velocity.Y > -0.1 {
velocity.Y = 0
}
}
return true
})
}

Expand Down
8 changes: 4 additions & 4 deletions examples/new-api/systems/debug-info.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions examples/new-api/systems/hp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
6 changes: 3 additions & 3 deletions examples/new-api/systems/player.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -110,7 +110,7 @@ func (s *PlayerSystem) Run() {
if rl.IsKeyPressed(rl.KeyK) {
s.EntityManager.Delete(e)
}
}

return true
})
}
func (s *PlayerSystem) Destroy() {}
18 changes: 9 additions & 9 deletions examples/new-api/systems/render-bogdan.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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{
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down
16 changes: 8 additions & 8 deletions examples/new-api/systems/render-overlay.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)

Expand Down Expand Up @@ -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)

Expand All @@ -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{
Expand Down Expand Up @@ -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)
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion examples/new-api/systems/space-spawner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion examples/new-api/systems/spaceship-intents.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion examples/new-api/systems/spatial-audio.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 4 additions & 4 deletions pkg/ecs/component-manager-shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Loading
Loading