-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpathfinding.go
More file actions
62 lines (50 loc) · 1.22 KB
/
pathfinding.go
File metadata and controls
62 lines (50 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package main
import (
"syscall/js"
astar "./lib/nickdavies"
)
var width int
var height int
var a astar.AStar
var p2p astar.AStarConfig
func setGrid(frst js.Value, args []js.Value) interface{} {
width = args[0].Int()
height = args[1].Int()
a = astar.NewAStar(width, height)
p2p = astar.NewPointToPoint()
return frst
}
func setObstacles(frst js.Value, args []js.Value) interface{} {
for i := 0; i < len(args); i += 2 {
a.FillTile(astar.Point{args[i].Int(), args[i+1].Int()}, -1)
}
return frst
}
func findPath(frst js.Value, args []js.Value) interface{} {
fromX := args[0].Int()
fromY := args[1].Int()
toX := args[2].Int()
toY := args[3].Int()
source := []astar.Point{astar.Point{fromX, fromY}}
target := []astar.Point{astar.Point{toX, toY}}
path := a.FindPath(p2p, source, target)
j := 0
outGrid := make([]interface{}, 0)
for path != nil {
outGrid = append(outGrid, path.Col)
outGrid = append(outGrid, path.Row)
path = path.Parent
j += 2
}
return outGrid
}
func registerCallbacks() {
js.Global().Set("setGrid", js.FuncOf(setGrid))
js.Global().Set("setObstacles", js.FuncOf(setObstacles))
js.Global().Set("findPath", js.FuncOf(findPath))
}
func main() {
c := make(chan bool)
registerCallbacks()
<-c
}