diff options
| author | Martin Kagamino Lehoux <martin@lehoux.net> | 2023-10-02 22:03:25 +0200 |
|---|---|---|
| committer | Martin Kagamino Lehoux <martin@lehoux.net> | 2023-10-02 22:03:25 +0200 |
| commit | 7e80b9cc8535973be68a1444843ecfc81d3ff355 (patch) | |
| tree | 043c1901e25b00268f7aded60f6c2b9d4a70a6f7 | |
| parent | 129d4d173b7c55e7a24ed49c2f50b50c56192253 (diff) | |
fix(move): Keep current path during move if it still applies
| -rw-r--r-- | .vscode/settings.json | 7 | ||||
| -rw-r--r-- | README.md | 4 | ||||
| -rw-r--r-- | main.go | 14 | ||||
| -rw-r--r-- | move.go | 42 | ||||
| -rw-r--r-- | order.go | 6 |
5 files changed, 36 insertions, 37 deletions
diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..1596528 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,7 @@ +{ + "cSpell.words": [ + "ebiten", + "hajimehoshi", + "inpututil" + ] +}
\ No newline at end of file @@ -1,3 +1,7 @@ +## Debugging + +- Restart after kernel update until `glxinfo | grep "OpenGL version"` works + ## TODO ```go @@ -13,8 +13,8 @@ import ( type Action string const ( - Selecting Action = "selecting" - Patroling Action = "patroling" + Selecting Action = "selecting" + Patrolling Action = "patrolling" ) var soilColor = color.RGBA{0x60, 0x40, 0x20, 0xff} @@ -69,7 +69,7 @@ func (g *Game) updateSelecting(cursor Point, moveMap MoveMap) { } } -func (g *Game) updatePatroling(cursor Point) { +func (g *Game) updatePatrolling(cursor Point) { if inpututil.IsMouseButtonJustReleased(ebiten.MouseButtonRight) { destination := cursor.Div(100).Mul(100) for _, e := range g.Entities { @@ -87,15 +87,15 @@ func (g *Game) Update() error { slog.Info("selecting action") } if inpututil.IsKeyJustReleased(ebiten.KeyA) { // Should be Q - g.CurrentAction = Patroling - slog.Info("patroling action") + g.CurrentAction = Patrolling + slog.Info("patrolling action") } moveMap := g.getMoveMap() switch g.CurrentAction { case Selecting: g.updateSelecting(cursor, moveMap) - case Patroling: - g.updatePatroling(cursor) + case Patrolling: + g.updatePatrolling(cursor) } for _, e := range g.Entities { e.UpdateMove(moveMap) @@ -61,7 +61,7 @@ func SearchPath(origin Point, destination Point, moveMap MoveMap) (Path, bool) { return search.search() } -func (s *PathSearch) addNeighbours(point Point, node Node) { +func (s *PathSearch) addNeighbors(point Point, node Node) { if point.X >= 100 { s.consider(Point{point.X - 100, point.Y}, point) } @@ -129,7 +129,7 @@ func (s *PathSearch) search() (Path, bool) { point, node = s.bestOpenNode() s.closedList[point] = node delete(s.openList, point) - s.addNeighbours(point, node) + s.addNeighbors(point, node) } if point == s.destination { return s.buildPath(), true @@ -147,33 +147,16 @@ func NewMove(origin Point, destination Point, moveMap MoveMap) Move { slog.Info("no path found", slog.String("destination", destination.String())) return Move{IsActive: false} } - return Move{IsActive: true, Origin: origin, Destination: destination, Path: path} + return Move{IsActive: true, Origin: origin, Destination: destination, Path: path[1:]} } -func (m *Move) Update(position Point, speed int, moveMap MoveMap) Point { - if !m.IsActive { - return position - } - next := m.Path[0] - if Distance(position, next) < float64(speed) { - if next == m.Destination { - m.IsActive = false - slog.Info("move finished", slog.String("destintation", m.Destination.String())) - } else { - slog.Info("next joined, searching new path", slog.String("next", next.String())) - path, ok := SearchPath(next, m.Destination, moveMap) - if !ok { - slog.Info("no path found", slog.String("destination", m.Destination.String())) - m.IsActive = false - } else { - m.Path = path - } +func (p *Path) isValid(moveMap MoveMap) bool { + for _, point := range *p { + if moveMap.Blocked[point] { + return false } - return next } - remainingMove := next.Sub(position) - delta := remainingMove.Mul(speed).Div(int(Length(remainingMove))) - return position.Add(delta) + return true } func (e *Entity) StartMove(destination Point, moveMap MoveMap) { @@ -197,19 +180,22 @@ func (e *Entity) UpdateMove(moveMap MoveMap) { if !e.Move.Value.IsActive { return } - next := e.Move.Value.Path[1] + next := e.Move.Value.Path[0] if Distance(position, next) < MOVE_SPEED { e.Position.Value = next + remainingPath := e.Move.Value.Path[1:] if next == e.Move.Value.Destination { e.Move.Value.IsActive = false - slog.Info("move finished", slog.String("destintation", e.Move.Value.Destination.String())) + slog.Info("move finished", slog.String("destination", e.Move.Value.Destination.String())) + } else if remainingPath.isValid(moveMap) { + e.Move.Value.Path = remainingPath } else { path, ok := SearchPath(next, e.Move.Value.Destination, moveMap) if !ok { slog.Info("no path found", slog.String("destination", e.Move.Value.Destination.String())) e.Move.Value.IsActive = false } else { - e.Move.Value.Path = path + e.Move.Value.Path = path[1:] } } } else { @@ -1,6 +1,8 @@ package main -import "golang.org/x/exp/slog" +import ( + "golang.org/x/exp/slog" +) type Order interface { Update(e *Entity, g *Game) @@ -36,7 +38,7 @@ func Patrol(e *Entity, destination Point) Order { if e.Selection.IsEnabled && e.Position.IsEnabled && e.Move.IsEnabled && e.Order.IsEnabled { if e.Selection.Value.IsSelected { origin := e.Position.Value - slog.Info("patroling between", slog.String("origin", origin.String()), slog.String("destination", destination.String())) + slog.Info("patrolling between", slog.String("origin", origin.String()), slog.String("destination", destination.String())) e.Order.Value = &PatrolOrder{origin: origin, destination: destination} } } |
