mirror of
https://github.com/wisplite/a-star-go.git
synced 2026-06-27 15:37:07 -05:00
add error box to prevent an occasional panic when the start or end pos wasn't set right
This commit is contained in:
@@ -290,6 +290,7 @@ func main() {
|
|||||||
editModeWidth := false
|
editModeWidth := false
|
||||||
editModeHeight := false
|
editModeHeight := false
|
||||||
generateGridError := false
|
generateGridError := false
|
||||||
|
posError := false
|
||||||
|
|
||||||
toolOptions := []string{"Wall", "Start", "End", "Erase"}
|
toolOptions := []string{"Wall", "Start", "End", "Erase"}
|
||||||
toolOptionsText := strings.Join(toolOptions, ";")
|
toolOptionsText := strings.Join(toolOptions, ";")
|
||||||
@@ -467,6 +468,13 @@ func main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if posError {
|
||||||
|
result := rg.MessageBox(rl.NewRectangle(screenWidth/2-(100*scale), screenHeight/2-(40*scale), (200*scale), (120*scale)), "Error", "Start and end positions must be\nwithin the generated grid", "OK")
|
||||||
|
if result >= 0 {
|
||||||
|
posError = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Generate Grid Button
|
// Generate Grid Button
|
||||||
if rg.Button(rl.NewRectangle(sidebarX+(10*scale), (40*scale), (180*scale), (30*scale)), "Generate Grid") {
|
if rg.Button(rl.NewRectangle(sidebarX+(10*scale), (40*scale), (180*scale), (30*scale)), "Generate Grid") {
|
||||||
width, _ = strconv.Atoi(widthInputValue)
|
width, _ = strconv.Atoi(widthInputValue)
|
||||||
@@ -521,39 +529,43 @@ func main() {
|
|||||||
|
|
||||||
// Calculate Path Button
|
// Calculate Path Button
|
||||||
if rg.Button(rl.NewRectangle(sidebarX+(10*scale), (screenHeight-(40*scale)), (180*scale), (30*scale)), "Calculate Path") {
|
if rg.Button(rl.NewRectangle(sidebarX+(10*scale), (screenHeight-(40*scale)), (180*scale), (30*scale)), "Calculate Path") {
|
||||||
astar.ResetGrid(false) // keep grid types, otherwise it will delete the board before simulating
|
if int(startPos.X) < 0 || int(startPos.X) >= width || int(startPos.Y) < 0 || int(startPos.Y) >= height || int(endPos.X) < 0 || int(endPos.X) >= width || int(endPos.Y) < 0 || int(endPos.Y) >= height {
|
||||||
astar.SetHeuristic(activeHeuristic)
|
posError = true
|
||||||
gridTypes := astar.GetGridTypes()
|
} else {
|
||||||
for i, gridType := range gridTypes {
|
astar.ResetGrid(false) // keep grid types, otherwise it will delete the board before simulating
|
||||||
// reset the map image
|
astar.SetHeuristic(activeHeuristic)
|
||||||
switch gridType {
|
gridTypes := astar.GetGridTypes()
|
||||||
case 0:
|
for i, gridType := range gridTypes {
|
||||||
rl.ImageDrawPixel(mapImage, int32(i%width), int32(i/width), rl.NewColor(240, 240, 240, 255))
|
// reset the map image
|
||||||
case 1:
|
switch gridType {
|
||||||
rl.ImageDrawPixel(mapImage, int32(i%width), int32(i/width), rl.NewColor(0, 0, 0, 255))
|
case 0:
|
||||||
case 2:
|
rl.ImageDrawPixel(mapImage, int32(i%width), int32(i/width), rl.NewColor(240, 240, 240, 255))
|
||||||
rl.ImageDrawPixel(mapImage, int32(i%width), int32(i/width), rl.NewColor(0, 255, 0, 255))
|
case 1:
|
||||||
case 3:
|
rl.ImageDrawPixel(mapImage, int32(i%width), int32(i/width), rl.NewColor(0, 0, 0, 255))
|
||||||
rl.ImageDrawPixel(mapImage, int32(i%width), int32(i/width), rl.NewColor(255, 0, 0, 255))
|
case 2:
|
||||||
}
|
rl.ImageDrawPixel(mapImage, int32(i%width), int32(i/width), rl.NewColor(0, 255, 0, 255))
|
||||||
}
|
case 3:
|
||||||
path := astar.CalculatePath(int(startPos.X), int(startPos.Y), int(endPos.X), int(endPos.Y))
|
rl.ImageDrawPixel(mapImage, int32(i%width), int32(i/width), rl.NewColor(255, 0, 0, 255))
|
||||||
closedSet := astar.GetClosedSet()
|
|
||||||
for i, closed := range closedSet {
|
|
||||||
if closed {
|
|
||||||
x := i % width
|
|
||||||
y := i / width
|
|
||||||
if x != int(startPos.X) || y != int(startPos.Y) {
|
|
||||||
rl.ImageDrawPixel(mapImage, int32(x), int32(y), rl.NewColor(0, 0, 255, 255))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
path := astar.CalculatePath(int(startPos.X), int(startPos.Y), int(endPos.X), int(endPos.Y))
|
||||||
for _, p := range path {
|
closedSet := astar.GetClosedSet()
|
||||||
if p[0] != int(startPos.X) || p[1] != int(startPos.Y) { // we want to keep the start position green
|
for i, closed := range closedSet {
|
||||||
rl.ImageDrawPixel(mapImage, int32(p[0]), int32(p[1]), rl.NewColor(255, 255, 0, 255))
|
if closed {
|
||||||
|
x := i % width
|
||||||
|
y := i / width
|
||||||
|
if x != int(startPos.X) || y != int(startPos.Y) {
|
||||||
|
rl.ImageDrawPixel(mapImage, int32(x), int32(y), rl.NewColor(0, 0, 255, 255))
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
for _, p := range path {
|
||||||
|
if p[0] != int(startPos.X) || p[1] != int(startPos.Y) { // we want to keep the start position green
|
||||||
|
rl.ImageDrawPixel(mapImage, int32(p[0]), int32(p[1]), rl.NewColor(255, 255, 0, 255))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
tex.markFull()
|
||||||
}
|
}
|
||||||
tex.markFull()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Status Label
|
// Status Label
|
||||||
|
|||||||
Reference in New Issue
Block a user