diff options
| author | Martin Kagamino Lehoux <martin@lehoux.net> | 2025-07-08 23:45:58 +0200 |
|---|---|---|
| committer | Martin Kagamino Lehoux <martin@lehoux.net> | 2025-07-08 23:45:58 +0200 |
| commit | 8216f68ff8516cebc961028f8b8a87c1684dc15a (patch) | |
| tree | 10eb4b82f30ff490362805d2dff1a245ac1c3ca9 /physics | |
| parent | ee4c612c2a1ec1b98cddea11bfd74fe9afbe85a6 (diff) | |
Allow diagonal move
Diffstat (limited to 'physics')
| -rw-r--r-- | physics/move.go | 36 |
1 files changed, 25 insertions, 11 deletions
diff --git a/physics/move.go b/physics/move.go index b6f13d7..f11e66a 100644 --- a/physics/move.go +++ b/physics/move.go @@ -59,18 +59,32 @@ func SearchPath(origin Point, destination Point, moveMap MoveMap) (Path, bool) { return search.search() } -func (s *PathSearch) addNeighbors(point Point, node Node) { - if point.X >= 100 { - s.consider(Point{X: point.X - 100, Y: point.Y}, point) +var neighborsDirectionsDiagonal = []Point{ + {X: -100, Y: 0}, // left + {X: 100, Y: 0}, // right + {X: 0, Y: -100}, // up + {X: 0, Y: 100}, // down + {X: -100, Y: -100}, // top-left + {X: 100, Y: -100}, // top-right + {X: -100, Y: 100}, // bottom-left + {X: 100, Y: 100}, // bottom-right } - if point.X <= s.moveMap.Width-100 { - s.consider(Point{X: point.X + 100, Y: point.Y}, point) +var neighborsDirectionsStraight = []Point{ + {X: -100, Y: 0}, // left + {X: 100, Y: 0}, // right + {X: 0, Y: -100}, // up + {X: 0, Y: 100}, // down } - if point.Y >= 100 { - s.consider(Point{X: point.X, Y: point.Y - 100}, point) - } - if point.Y <= s.moveMap.Height-100 { - s.consider(Point{X: point.X, Y: point.Y + 100}, point) + +// Add neighbors to the open list +// Choose neighborsDirections +func (s *PathSearch) addNeighbors(point Point) { + for _, dir := range neighborsDirectionsDiagonal { + neighbor := Point{X: point.X + dir.X, Y: point.Y + dir.Y} + if neighbor.X >= 0 && neighbor.X <= s.moveMap.Width-100 && + neighbor.Y >= 0 && neighbor.Y <= s.moveMap.Height-100 { + s.consider(neighbor, point) + } } } @@ -127,7 +141,7 @@ func (s *PathSearch) search() (Path, bool) { point, node = s.bestOpenNode() s.closedList[point] = node delete(s.openList, point) - s.addNeighbors(point, node) + s.addNeighbors(point) } if point == s.destination { return s.buildPath(), true |
