From 337dee61ada57e1d44079822abc437b8fc168843 Mon Sep 17 00:00:00 2001 From: Martin Kagamino Lehoux Date: Thu, 24 Jul 2025 15:05:22 +0200 Subject: refactor: Extract closest --- main.go | 20 +------------------- 1 file changed, 1 insertion(+), 19 deletions(-) (limited to 'main.go') diff --git a/main.go b/main.go index 22f175b..e4d74b0 100644 --- a/main.go +++ b/main.go @@ -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) -- cgit v1.3