-
Notifications
You must be signed in to change notification settings - Fork 0
/
movement.go
74 lines (65 loc) · 2.13 KB
/
movement.go
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
63
64
65
66
67
68
69
70
71
72
73
74
package main
import (
"math"
"strconv"
"github.com/beefsack/go-astar"
)
var cache map[string]*tMesh
func cachedGetMeshByPos(x, z, y int64) *tMesh {
sx := strconv.FormatInt(x, 10)
sz := strconv.FormatInt(z, 10)
sy := strconv.FormatInt(y, 10)
tMesh := cache[sx+"-"+sz+"-"+sy]
if tMesh == nil { // if its not found, cache it
tMesh = db.getMeshByPos(x, z, y)
cache[sx+"-"+sz+"-"+sy] = tMesh
}
return tMesh
}
func movementCalculatePath(a, b *tMesh) []*tMesh {
Ax := strconv.FormatInt(a.Position.X, 10)
Az := strconv.FormatInt(a.Position.Z, 10)
Ay := strconv.FormatInt(a.VerticalLevel, 10)
Bx := strconv.FormatInt(b.Position.X, 10)
Bz := strconv.FormatInt(b.Position.Z, 10)
By := strconv.FormatInt(b.VerticalLevel, 10)
cache = map[string]*tMesh{
Ax + "-" + Az + "-" + Ay: a,
Bx + "-" + Bz + "-" + By: b,
}
res := []*tMesh{}
path, _, _ := astar.Path(a, b)
for _, m := range path {
res = append(res, m.(*tMesh))
}
return res
}
func (t *tMesh) PathNeighbors() []astar.Pather {
res := []astar.Pather{}
log := []tPos{}
// TODO we should get meshes in all verticalLevels we consider the player height is
// right now, we are not having into consideration the walkable property of meshes
// which are not in the same verticalLevel that the player, but the player is some
// verticalLevels height, not just one
for _, m := range []*tMesh{
cachedGetMeshByPos(t.Position.X, t.Position.Z-1, t.VerticalLevel), // UP is z-1
cachedGetMeshByPos(t.Position.X+1, t.Position.Z, t.VerticalLevel), // RIGHT is x+1
cachedGetMeshByPos(t.Position.X, t.Position.Z+1, t.VerticalLevel), // DOWN is z+1
cachedGetMeshByPos(t.Position.X-1, t.Position.Z, t.VerticalLevel), // LEFT is x-1
} {
if m != nil && m.Walkable {
res = append(res, m)
log = append(log, m.Position)
}
}
return res
}
func (t *tMesh) PathNeighborCost(to astar.Pather) float64 {
toMesh := to.(*tMesh)
return float64(toMesh.WalkingCost)
}
func (t *tMesh) PathEstimatedCost(to astar.Pather) float64 {
toMesh := to.(*tMesh)
distance := math.Abs(float64(toMesh.Position.X)-float64(t.Position.X)) + math.Abs(float64(toMesh.Position.Z)-float64(t.Position.Z))
return distance
}