-
-
Notifications
You must be signed in to change notification settings - Fork 306
/
Copy pathsystem.js
98 lines (88 loc) · 2.21 KB
/
system.js
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
const { Pathfinding } = require('three-pathfinding');
const pathfinder = new Pathfinding();
const ZONE = 'level';
/**
* nav
*
* Pathfinding system, using PatrolJS.
*/
module.exports = AFRAME.registerSystem('nav', {
init: function () {
this.navMesh = null;
this.agents = new Set();
},
/**
* @param {THREE.Geometry} geometry
*/
setNavMeshGeometry: function (geometry) {
this.navMesh = new THREE.Mesh(geometry);
pathfinder.setZoneData(ZONE, Pathfinding.createZone(geometry));
Array.from(this.agents).forEach((agent) => agent.updateNavLocation());
},
/**
* @return {THREE.Mesh}
*/
getNavMesh: function () {
return this.navMesh;
},
/**
* @param {NavAgent} ctrl
*/
addAgent: function (ctrl) {
this.agents.add(ctrl);
},
/**
* @param {NavAgent} ctrl
*/
removeAgent: function (ctrl) {
this.agents.delete(ctrl);
},
/**
* @param {THREE.Vector3} start
* @param {THREE.Vector3} end
* @param {number} groupID
* @return {Array<THREE.Vector3>}
*/
getPath: function (start, end, groupID) {
return this.navMesh
? pathfinder.findPath(start, end, ZONE, groupID)
: null;
},
/**
* @param {THREE.Vector3} position
* @return {number}
*/
getGroup: function (position) {
return this.navMesh
? pathfinder.getGroup(ZONE, position)
: null;
},
/**
* @param {THREE.Vector3} position
* @param {number} groupID
* @return {Node}
*/
getNode: function (position, groupID) {
return this.navMesh
? pathfinder.getClosestNode(position, ZONE, groupID, true)
: null;
},
/**
* @param {THREE.Vector3} start Starting position.
* @param {THREE.Vector3} end Desired ending position.
* @param {number} groupID
* @param {Node} node
* @param {THREE.Vector3} endTarget (Output) Adjusted step end position.
* @return {Node} Current node, after step is taken.
*/
clampStep: function (start, end, groupID, node, endTarget) {
if (!this.navMesh) {
endTarget.copy(end);
return null;
} else if (!node) {
endTarget.copy(end);
return this.getNode(end, groupID);
}
return pathfinder.clampStep(start, end, node, ZONE, groupID, endTarget);
}
});