Repository navigation
Expand file tree
/
Copy pathworld_path.go
More file actions
60 lines (58 loc) · 1.48 KB
/
Copy pathworld_path.go
File metadata and controls
60 lines (58 loc) · 1.48 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
package grid
import "github.com/actfuns/pathfinding/finder"
// findPathWorld is shared by the built-in grids. It always runs the configured
// Finder (there is no open-grid shortcut), applies smoothing, then maps tile
// centres to world space while preserving the caller's original start and end points.
func findPathWorld(g Grid, smoother Smoother, wx1, wy1, wx2, wy2 float32, buff *PathBuff) ([][2]float32, error) {
var dst [][2]float32
var tileDst [][2]int32
if buff != nil {
dst = buff.World[:0]
tileDst = buff.Tile[:0]
}
f := g.Finder()
if f == nil {
return nil, nil
}
sx, sy := g.WorldToTile(wx1, wy1)
ex, ey := g.WorldToTile(wx2, wy2)
if !g.IsInside(sx, sy) || !g.IsInside(ex, ey) {
return nil, finder.ErrOutOfBounds
}
path, err := f.FindPath(sx, sy, ex, ey, g, tileDst)
if err != nil {
return nil, err
}
if len(path) == 0 {
return nil, nil
}
if smoother != nil {
path = smoother(path)
}
if len(path) == 0 {
return nil, nil
}
if buff != nil {
buff.Tile = path
}
worldLen := len(path)
if worldLen == 1 && (wx1 != wx2 || wy1 != wy2) {
// Distinct world positions in one tile still have two endpoints.
worldLen = 2
}
var out [][2]float32
if cap(dst) >= worldLen {
out = dst[:worldLen]
} else {
out = make([][2]float32, worldLen)
}
for i, p := range path {
out[i][0], out[i][1] = g.TileToWorld(p[0], p[1])
}
out[0][0], out[0][1] = wx1, wy1
out[len(out)-1][0], out[len(out)-1][1] = wx2, wy2
if buff != nil {
buff.World = out
}
return out, nil
}