From 7e80b9cc8535973be68a1444843ecfc81d3ff355 Mon Sep 17 00:00:00 2001 From: Martin Kagamino Lehoux Date: Mon, 2 Oct 2023 22:03:25 +0200 Subject: fix(move): Keep current path during move if it still applies --- .vscode/settings.json | 7 +++++++ README.md | 4 ++++ main.go | 14 +++++++------- move.go | 42 ++++++++++++++---------------------------- order.go | 6 ++++-- 5 files changed, 36 insertions(+), 37 deletions(-) create mode 100644 .vscode/settings.json 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 diff --git a/README.md b/README.md index de8669a..3405603 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,7 @@ +## Debugging + +- Restart after kernel update until `glxinfo | grep "OpenGL version"` works + ## TODO ```go diff --git a/main.go b/main.go index adca3ff..78bbb48 100644 --- a/main.go +++ b/main.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) diff --git a/move.go b/move.go index c45378d..2568e53 100644 --- a/move.go +++ b/move.go @@ -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 { diff --git a/order.go b/order.go index 13c1b09..6836954 100644 --- a/order.go +++ b/order.go @@ -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} } } -- cgit v1.3