mirror of
https://github.com/wisplite/a-star-go.git
synced 2026-06-27 15:37:07 -05:00
runtime statistics label
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"container/heap"
|
||||
"fmt"
|
||||
"math"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Item struct {
|
||||
@@ -52,6 +53,7 @@ type AStar struct {
|
||||
width int
|
||||
height int
|
||||
heuristic func(x int, y int, endX int, endY int) float32
|
||||
timeTaken time.Duration
|
||||
}
|
||||
|
||||
func (a *AStar) Init(width int, height int) {
|
||||
@@ -195,7 +197,25 @@ func (a *AStar) GetTerrainCost(x int, y int) float32 {
|
||||
return 1.0
|
||||
}
|
||||
|
||||
func (a *AStar) GetEvaluatedCells() int {
|
||||
cellsEvaluated := 0
|
||||
for _, closed := range a.closedSet {
|
||||
if closed {
|
||||
cellsEvaluated++
|
||||
}
|
||||
}
|
||||
return cellsEvaluated
|
||||
}
|
||||
|
||||
func (a *AStar) GetTimeTaken() time.Duration {
|
||||
return a.timeTaken
|
||||
}
|
||||
|
||||
func (a *AStar) CalculatePath(startX int, startY int, endX int, endY int) [][]int {
|
||||
timer := time.Now()
|
||||
defer func() {
|
||||
a.timeTaken = time.Since(timer)
|
||||
}()
|
||||
startIndex := startY*a.width + startX
|
||||
endIndex := endY*a.width + endX
|
||||
a.gScores[startIndex] = 0
|
||||
|
||||
@@ -380,6 +380,7 @@ func main() {
|
||||
if startPos.X >= 0 && startPos.Y >= 0 {
|
||||
ox, oy := int(startPos.X), int(startPos.Y)
|
||||
rl.ImageDrawPixel(mapImage, int32(startPos.X), int32(startPos.Y), rl.NewColor(240, 240, 240, 255))
|
||||
astar.SetGridType(ox, oy, 0)
|
||||
tex.markRegion(ox, oy, ox, oy, width, height)
|
||||
}
|
||||
rl.ImageDrawPixel(mapImage, int32(x), int32(y), rl.NewColor(0, 255, 0, 255))
|
||||
@@ -392,6 +393,7 @@ func main() {
|
||||
if endPos.X >= 0 && endPos.Y >= 0 {
|
||||
ox, oy := int(endPos.X), int(endPos.Y)
|
||||
rl.ImageDrawPixel(mapImage, int32(endPos.X), int32(endPos.Y), rl.NewColor(240, 240, 240, 255))
|
||||
astar.SetGridType(ox, oy, 0)
|
||||
tex.markRegion(ox, oy, ox, oy, width, height)
|
||||
}
|
||||
rl.ImageDrawPixel(mapImage, int32(x), int32(y), rl.NewColor(255, 0, 0, 255))
|
||||
@@ -554,6 +556,9 @@ func main() {
|
||||
tex.markFull()
|
||||
}
|
||||
|
||||
// Status Label
|
||||
rg.Label(rl.NewRectangle((10*scale), (screenHeight-(30*scale)), (180*scale), (30*scale)), "Evaluated "+strconv.Itoa(astar.GetEvaluatedCells())+" cells in "+astar.GetTimeTaken().String())
|
||||
|
||||
rl.EndDrawing()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user