diff options
| author | Martin Kagamino Lehoux <martin@lehoux.net> | 2025-07-24 15:05:22 +0200 |
|---|---|---|
| committer | Martin Kagamino Lehoux <martin@lehoux.net> | 2025-07-24 15:05:22 +0200 |
| commit | 337dee61ada57e1d44079822abc437b8fc168843 (patch) | |
| tree | 1a87534523882d8fc1d862df017396dbdd376f3b | |
| parent | e6e4ce18083ae5552563eab3291ac1642371d025 (diff) | |
refactor: Extract closest
| -rw-r--r-- | gather_order.go | 6 | ||||
| -rw-r--r-- | main.go | 20 | ||||
| -rw-r--r-- | physics/geometry.go | 18 | ||||
| -rw-r--r-- | spawn.go | 2 |
4 files changed, 22 insertions, 24 deletions
diff --git a/gather_order.go b/gather_order.go index be98ca4..10cbcaf 100644 --- a/gather_order.go +++ b/gather_order.go @@ -46,7 +46,7 @@ func (o *GatherOrder) updateGathererFull(e *Entity, g *Game, gatherer *ResourceG return } storageDockings := getAllStorageDockings(g) - destination, distance := g.Closest(e.Position.Value, storageDockings) + destination, distance := physics.Closest(e.Position.Value, storageDockings, g.getMoveMap()) if distance == math.MaxInt { slog.Info("no accessible storage, canceling gather order") e.Order.Value = nil @@ -65,7 +65,7 @@ func (o *GatherOrder) updateGathererFull(e *Entity, g *Game, gatherer *ResourceG func (o *GatherOrder) startMoveToSource(e *Entity, gatherer *ResourceGatherer, g *Game) { sourceDockings := physics.AdjacentPoints(gatherer.CurrentTarget.Position.Value) - destination, distance := g.Closest(e.Position.Value, sourceDockings) + 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 @@ -92,7 +92,7 @@ func Gather(e *Entity, source *Entity, g *Game) Order { e.Order.Value = &GatherOrder{source: source} e.ResourceGatherer.Value.CurrentTarget = source sourceDockings := physics.AdjacentPoints(source.Position.Value) - destination, distance := g.Closest(e.Position.Value, sourceDockings) + 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 @@ -7,7 +7,6 @@ import ( "image" "image/color" _ "image/jpeg" - "math" "os" "runtime/pprof" "time" @@ -54,23 +53,6 @@ func (g *Game) getMoveMap() physics.MoveMap { return physics.MoveMap{Width: 3200, Height: 2400, Blocked: blocked} } -func (g *Game) Closest(from physics.Point, candidates []physics.Point) (physics.Point, int) { - var closest physics.Point - closestDistance := math.MaxInt - moveMap := g.getMoveMap() - for _, dest := range candidates { - path, ok := physics.SearchPath(from, dest, moveMap) - if !ok { - continue - } - if len(path) < closestDistance { - closest = dest - closestDistance = len(path) - } - } - return closest, closestDistance -} - // TODO: This could be a maintained index func (g *Game) entityAt(position physics.Point) *Entity { for _, e := range g.Entities { @@ -193,7 +175,7 @@ func main() { game.Entities = append(game.Entities, &townCenter) game.UnitBuilder = NewEntityBuilder().WithImage(NewFilledCircleImage(100, color.White)).WithSelection("round").WithMove().WithOrder().WithResourceGatherer(15) for i := 0; i < 0; i++ { - spawnPosition, _ := game.Closest(townCenter.Position.Value, physics.AdjacentPoints(townCenter.Position.Value)) + spawnPosition, _ := physics.Closest(townCenter.Position.Value, physics.AdjacentPoints(townCenter.Position.Value), game.getMoveMap()) person := game.UnitBuilder.Build() person.Position = ecs.C(spawnPosition) game.Entities = append(game.Entities, &person) diff --git a/physics/geometry.go b/physics/geometry.go index 1902fdf..9949466 100644 --- a/physics/geometry.go +++ b/physics/geometry.go @@ -13,7 +13,7 @@ func Distance(p1, p2 Point) float64 { } func Length(p Point) float64 { - return Distance(p, Point{X:0,Y:0}) + return Distance(p, Point{X: 0, Y: 0}) } func Normalize(p Point) Point { @@ -28,3 +28,19 @@ func AdjacentPoints(from Point) []Point { from.Add(Point{X: 100, Y: 0}), } } + +func Closest(from Point, candidates []Point, moveMap MoveMap) (Point, int) { + var closest Point + closestDistance := math.MaxInt + for _, dest := range candidates { + path, ok := SearchPath(from, dest, moveMap) + if !ok { + continue + } + if len(path) < closestDistance { + closest = dest + closestDistance = len(path) + } + } + return closest, closestDistance +} @@ -42,7 +42,7 @@ func UpdateSpawn(g *Game, spawn *ecs.Component[Spawn], position ecs.Component[ph if len(spawn.Value.Requests) == 0 || now.Sub(spawn.Value.Requests[0].start) < time.Duration(spawn.Value.UnitSpawnDuration) { return } - spawnPosition, _ := g.Closest(position.Value, physics.AdjacentPoints(position.Value)) + spawnPosition, _ := physics.Closest(position.Value, physics.AdjacentPoints(position.Value), g.getMoveMap()) unit := g.UnitBuilder.Build() unit.Position = ecs.C(spawnPosition) g.Entities = append(g.Entities, &unit) |
