Skip to content

Commit

Permalink
utils/graph: Moved from AStar to dijkstra
Browse files Browse the repository at this point in the history
Turns out it's fast enough now and the result it's much better.

Also fixed the life bars that where not working since the change from 'float64' to 'int'
  • Loading branch information
xescugc committed Mar 7, 2024
1 parent 52ab7e5 commit ac090bc
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 3 deletions.
2 changes: 1 addition & 1 deletion client/game/lines.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,6 @@ func (ls *Lines) DrawUnit(screen *ebiten.Image, c *CameraStore, u *store.Unit) {

op = &ebiten.DrawImageOptions{}
op.GeoM.Translate(float64(u.X-cs.X), float64(u.Y-cs.Y-ls.lifeBarProgress.Bounds().Dy()))
screen.DrawImage(ls.lifeBarProgress.(*ebiten.Image).SubImage(image.Rect(0, 0, ls.lifeBarProgress.Bounds().Dx()*int((u.Health/h)), ls.lifeBarProgress.Bounds().Dy())).(*ebiten.Image), op)
screen.DrawImage(ls.lifeBarProgress.(*ebiten.Image).SubImage(image.Rect(0, 0, int(float64(ls.lifeBarProgress.Bounds().Dx())*(u.Health/h)), ls.lifeBarProgress.Bounds().Dy())).(*ebiten.Image), op)
}
}
Binary file modified server/assets/wasm/maze-wars.wasm
Binary file not shown.
7 changes: 5 additions & 2 deletions utils/graph/astar.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,10 @@ func (g *Graph) AStar(sx, sy int, d utils.Direction, tx, ty int, atScale bool) [
if !neighborStep.open && !neighborStep.closed {
neighborStep.cost = cost
neighborStep.open = true
neighborStep.rank = cost + neighbor.Node.MDistance(tn)
// If no node is ranked it's basically doing a dijkstra
// which now it's much faster and gives better results
//neighborStep.rank = cost + neighbor.Node.MDistance(tn)
neighborStep.rank = cost
neighborStep.parent = current
// We try to give priority to the ones
// that are not on the same direction.
Expand All @@ -156,7 +159,7 @@ func (g *Graph) AStar(sx, sy int, d utils.Direction, tx, ty int, atScale bool) [
// This helps on prioritizing doing
// diagonals
if neighborStep.step.Facing == current.step.Facing {
neighborStep.rank += 64
neighborStep.rank += 32
}
heap.Push(nq, neighborStep)
}
Expand Down

0 comments on commit ac090bc

Please sign in to comment.