aboutsummaryrefslogtreecommitdiff
path: root/gather_order.go
diff options
context:
space:
mode:
authorMartin Kagamino Lehoux <martin@lehoux.net>2026-01-15 22:05:38 +0100
committerMartin Kagamino Lehoux <martin@lehoux.net>2026-01-15 22:05:46 +0100
commit13626410c4d6db7fdc15ded5ee7865fcdbf0b88a (patch)
treea549d918804acb7682c857e742f1af189692cf62 /gather_order.go
parentcf85fc095bc9b6ab4b49e4f88a5f04d7ac2fe74b (diff)
refactor: transition to struct-of-arrays ECS architecture
- Replace individual Entity structs with centralized component slices in Game struct (SoA layout). - Implement bitmask-based component lookup (CM_Position, CM_Move, etc.). - Convert logic from object-oriented Update() methods to standalone Systems (UpdateMoveSystem, UpdateGatherSystem, UpdateSpawnSystem). - Update rendering, selection, and input handling to iterate over component arrays.
Diffstat (limited to 'gather_order.go')
-rw-r--r--gather_order.go95
1 files changed, 36 insertions, 59 deletions
diff --git a/gather_order.go b/gather_order.go
index eec9b99..fd56fbe 100644
--- a/gather_order.go
+++ b/gather_order.go
@@ -4,35 +4,34 @@ import (
"math"
"time"
+ "age_of_empires/ecs"
"age_of_empires/physics"
"golang.org/x/exp/slog"
)
-type GatherOrder struct {
- source *Entity
-}
-
-func (o *GatherOrder) Update(e *Entity, g *Game) {
- if !e.Position.IsEnabled || !e.Move.IsEnabled || !e.ResourceGatherer.IsEnabled || e.ResourceGatherer.Value.CurrentTarget == nil {
- return
- }
-
- gatherer := &e.ResourceGatherer.Value
- if gatherer.CurrentVolume == gatherer.MaxCapacity {
- o.updateGathererFull(e, g, gatherer)
- return
- }
- if physics.Distance(e.Position.Value, gatherer.CurrentTarget.Position.Value) <= 100 {
- o.updateGathering(g.Now(), gatherer, &gatherer.CurrentTarget.ResourceSource.Value)
- return
- }
- if !e.Move.Value.IsActive {
- o.startMoveToSource(e, gatherer, g)
+func UpdateGatherSystem(g *Game, now time.Time, moveMap physics.MoveMap, masks []ecs.Mask, orders []OrderKind, positions []physics.Point, moves []physics.Move, gatherers []ResourceGatherer) {
+ required := ecs.CM_ResourceGatherer | ecs.CM_Position | ecs.CM_Move | ecs.CM_Order
+ for i, mask := range masks {
+ if mask&required == required {
+ order := &orders[i]
+ position := positions[i]
+ move := &moves[i]
+ gatherer := &gatherers[i]
+ if *order == OrderKindGather {
+ if gatherer.CurrentVolume == gatherer.MaxCapacity {
+ updateGathererFull(g, getAllStorageDockings(g), moveMap, gatherer, order, move, position)
+ } else if physics.Distance(position, g.Position[gatherer.Source]) <= 100 {
+ updateGathering(now, gatherer, &g.ResourceSource[gatherer.Source])
+ } else if !move.IsActive {
+ startMoveToSource(moveMap, gatherer, move, order, position, g.Position[gatherer.Source])
+ }
+ }
+ }
}
}
-func (*GatherOrder) updateGathering(now time.Time, gatherer *ResourceGatherer, source *ResourceSource) {
+func updateGathering(now time.Time, gatherer *ResourceGatherer, source *ResourceSource) {
if gatherer.CurrentVolume >= gatherer.MaxCapacity || gatherer.LastPickupTime.Add(200*time.Millisecond).After(now) {
return
}
@@ -41,63 +40,41 @@ func (*GatherOrder) updateGathering(now time.Time, gatherer *ResourceGatherer, s
source.Remaining -= 1
}
-func (o *GatherOrder) updateGathererFull(e *Entity, g *Game, gatherer *ResourceGatherer) {
- if e.Move.Value.IsActive {
+func updateGathererFull(g *Game, storageDockings []physics.Point, moveMap physics.MoveMap, gatherer *ResourceGatherer, order *OrderKind, move *physics.Move, position physics.Point) {
+ if move.IsActive {
return
}
- storageDockings := getAllStorageDockings(g)
- destination, distance := physics.Closest(e.Position.Value, storageDockings, g.getMoveMap())
+ destination, distance := physics.Closest(position, storageDockings, moveMap)
if distance == math.MaxInt {
slog.Info("no accessible storage, canceling gather order")
- e.Order.Value = nil
+ *order = OrderKindNone
return
}
slog.Info("storage target", slog.String("storage", destination.String()), slog.Int("distance", distance))
if distance > 1 {
slog.Info("moving to storage", slog.String("storage", destination.String()))
- physics.StartMove(&e.Move, e.Position, destination, g.getMoveMap())
+ *move = physics.NewMove(position, destination, moveMap)
return
}
g.ResourceAmount += gatherer.CurrentVolume
gatherer.CurrentVolume = 0
- o.startMoveToSource(e, gatherer, g)
+ startMoveToSource(moveMap, gatherer, move, order, position, g.Position[gatherer.Source])
}
-func (o *GatherOrder) startMoveToSource(e *Entity, gatherer *ResourceGatherer, g *Game) {
- sourceDockings := physics.AdjacentPoints(gatherer.CurrentTarget.Position.Value)
- destination, distance := physics.Closest(e.Position.Value, sourceDockings, g.getMoveMap())
+func startMoveToSource(moveMap physics.MoveMap, gatherer *ResourceGatherer, move *physics.Move, order *OrderKind, position physics.Point, sourcePosition physics.Point) {
+ sourceDockings := physics.AdjacentPoints(sourcePosition)
+ destination, distance := physics.Closest(position, sourceDockings, moveMap)
if distance == math.MaxInt {
slog.Info("no accessible storage, canceling gather order")
- e.Order.Value = nil
+ *order = OrderKindNone
return
}
- physics.StartMove(&e.Move, e.Position, destination, g.getMoveMap())
-}
-
-func getAllStorageDockings(g *Game) []physics.Point {
- storageDockings := []physics.Point{}
- for _, entity := range g.Entities {
- if entity.Position.IsEnabled && entity.ResourceStorage.IsEnabled {
- storageDockings = append(storageDockings, physics.AdjacentPoints(entity.Position.Value)...)
- }
- }
- return storageDockings
+ *move = physics.NewMove(position, destination, moveMap)
}
-func Gather(e *Entity, source *Entity, g *Game) Order {
- if !e.Position.IsEnabled || !e.Move.IsEnabled || !e.Order.IsEnabled {
- return nil
- }
- slog.Info("gathering from", slog.String("source", source.Position.Value.String()))
- e.Order.Value = &GatherOrder{source: source}
- e.ResourceGatherer.Value.CurrentTarget = source
- sourceDockings := physics.AdjacentPoints(source.Position.Value)
- destination, distance := physics.Closest(e.Position.Value, sourceDockings, g.getMoveMap())
- if distance == math.MaxInt {
- slog.Info("no accessible storage, canceling gather order")
- e.Order.Value = nil
- return nil
- }
- physics.StartMove(&e.Move, e.Position, destination, g.getMoveMap())
- return &GatherOrder{source: source}
+func Gather(moveMap physics.MoveMap, position physics.Point, move *physics.Move, order *OrderKind, gatherer *ResourceGatherer, source ecs.Entity, sourcePosition physics.Point) {
+ slog.Info("gathering from", slog.String("source", sourcePosition.String()))
+ *order = OrderKindGather
+ gatherer.Source = source
+ startMoveToSource(moveMap, gatherer, move, order, position, sourcePosition)
}