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

activate

概述

activate 方法是 CompositeGoal 类的一个方法,用于激活该复合目标,并在其子目标中选择激活的目标,并将其加入到 subGoals 数组中等待处理。

语法

activate()

参数

返回值

示例

const yuka = require('yuka');
const { CompositeGoal, Goal } = yuka;

class MyCompositeGoal extends CompositeGoal {
  constructor(owner) {
    super(owner);
  }

  activate() {
    // 重置子目标数组
    this.clearSubGoals();

    // 在这里添加欲激活的子目标
    // ...

    // 激活第一个子目标 (例子)
    this.subGoals[0].activate();
  }
}

class MyGoal extends Goal {
  constructor(owner) {
    super(owner);
  }

  activate() {
    console.log('我的目标已激活');
  }
}

const entity = {};

const myCompositeGoal = new MyCompositeGoal(entity);
const myGoal1 = new MyGoal(entity);

myCompositeGoal.addSubGoal(myGoal1);

myCompositeGoal.activate(); // 控制台输出:"我的目标已激活"

实现细节

activate 方法主要用于激活复合目标及其子目标。实现时,它会先清空 subGoals 数组,再添加需要激活的子目标。最后,它会激活第一个子目标,将其添加到 subGoals 数组中等待处理。

在激活子目标时,activate 方法会设置 status 属性为 Goal.STATUS_ACTIVE,并在处理周期中让子目标的 process 方法进行处理。