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

resolveReferences

介绍

resolveReferences是Yuka js库的CohesionBehavior(内聚性行为)的一个方法,用于为实体解析引用。引用通常用于实体之间的关联,例如一个实体可能引用另一个实体的某个属性或行为。

语法

entity.resolveReferences();

参数

返回值

描述

resolveReferences方法用于在内聚性行为CohesionBehavior中为实体解析引用。在上下文中,引用是实体之间的关联,通过引用实现实体间通信。

解析引用的过程包括寻找被引用的实体,找到后将其绑定到当前实体上。这样,在该实体执行内聚性行为时,它可以访问其所引用的实体的属性和行为。

请注意,如果在解析引用期间找不到被引用的实体,则内部状态将不会被更改。

示例

class Enemy extends Entity {
  constructor() {
    super();
    this.attackPower = 10;
  }
}

class MeleeAttack extends CohesionBehavior {
  constructor() {
    super();
    this.target = null;
  }
  
  execute() {
    if (this.target) {
      console.log(`Attacking target with a power of ${this.target.attackPower}`);
    }
  }
  
  setReferences(references) {
    this.target = references.get('target');
  }
}

const enemy = new Enemy();
const attack = new MeleeAttack();
const references = new Map([['target', enemy]]);
attack.setReferences(references);
attack.resolveReferences();
attack.execute(); // 输出 "Attacking target with a power of 10"

在上面的示例中,我们实例化了一个敌人实体,并设置了它的攻击力。接下来,我们创建了一个CohesionBehavior实例- MeleeAttack,该实例有一个名为“target”的属性。

在设置引用期间,我们将敌人实体(enemy)与名为“target”的关键字映射。随后,我们调用了resolveReferences方法,实现了引用的解析。最后,我们运行了内聚性行为的execute方法,它可以访问引用的实体的行为(攻击力)。