AABB
AlignmentBehavior
ArriveBehavior
AStar
BFS
BoundingSphere
BVH
BVHNode
Cell
CellSpacePartitioning
CohesionBehavior
CompositeGoal
ConvexHull
Corridor
CostTable
DFS
Dijkstra
Edge
EntityManager
EvadeBehavior
EventDispatcher
Behavior
FollowPathBehavior
FuzzyAND
FuzzyCompositeTerm
FuzzyFAIRLY
FuzzyModule
FuzzyOR
FuzzyRule
FuzzySet
FuzzyTerm
FuzzyVariable
FuzzyVERY
GameEntity
Goal
GoalEvaluator
Graph
GraphUtils
HalfEdge
HeuristicPolicyDijkstra
HeuristicPolicyEuclid
HeuristicPolicyEuclidSquared
HeuristicPolicyManhattan
InterposeBehavior
LeftSCurveFuzzySet
LeftShoulderFuzzySet
LineSegment
Logger
MathUtils
Matrix3
Matrix4
MemoryRecord
MemorySystem
MeshGeometry
MessageDispatcher
MovingEntity
NavEdge
NavMesh
NavMeshLoader
NavNode
Node
NormalDistFuzzySet
OBB
ObstacleAvoidanceBehavior
OffsetPursuitBehavior
OnPathBehavior
Path
Plane
Polygon
Polyhedron
PriorityQueue
PursuitBehavior
Quaternion
Ray
RectangleTriggerRegion
Regular
RightSCurveFuzzySet
RightShoulderFuzzySet
SAT
SeekBehavior
SeparationBehavior
SingletonFuzzySet
Smoother
SphericalTriggerRegion
State
StateMachine
SteeringBehavior
SteeringManager
Task
TaskQueue
Telegram
Think
Time
TriangularFuzzySet
Trigger
TriggerRegion
Vector3
Vehicle
Version
WanderBehavior

graph

graph类实现了一张图,用于在A*寻路算法中进行路径搜索。

方法

constructor(nodes, edges)

  • nodes - 数组,包含图上每个节点的坐标和唯一标识符;
  • edges - 数组,包含图上每个节点的相邻边的起点、终点和代价。

构造一个新的graph对象。

addNode(node)

  • node - 对象,表示新的节点,应包含坐标和唯一标识符。

将新的节点添加到图中。

addEdge(edge)

  • edge - 对象,表示新的边,应包含起点,终点和代价。

将新的边添加到图中。

neighbors(nodeId)

  • nodeId - 字符串,表示节点的唯一标识符。

获取节点的所有相邻节点,并返回它们及与当前节点的距离。

distance(fromNode, toNode)

  • fromNode - 字符串,表示起点节点的唯一标识符;
  • toNode - 字符串,表示终点节点的唯一标识符。

获取从起点节点到终点节点的距离。

示例代码

const nodes = [
  { id: 'A', x: 0, y: 0 },
  { id: 'B', x: 1, y: 0 },
  { id: 'C', x: 2, y: 0 },
  { id: 'D', x: 2, y: 1 },
  { id: 'E', x: 2, y: 2 },
  { id: 'F', x: 1, y: 2 },
  { id: 'G', x: 0, y: 2 }
];

const edges = [
  { from: 'A', to: 'B', cost: 1 },
  { from: 'B', to: 'C', cost: 2 },
  { from: 'C', to: 'D', cost: 3 },
  { from: 'D', to: 'E', cost: 4 },
  { from: 'E', to: 'F', cost: 5 },
  { from: 'F', to: 'G', cost: 6 },
  { from: 'G', to: 'A', cost: 7 }
];

const graph = new Graph(nodes, edges);
const path = astar.search(graph, 'A', 'E');