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

replanIfFailed

描述

replanIfFailed 方法是 Yuka.js 库中的一个目标(Goal),用于在当前目标失败时进行重新计划。如果当前目标失败,则自动重新规划一个新目标以尝试达到预期结果。该方法需在定义目标时设置。

语法

replanIfFailed(enabled)

参数

  • enabled:一个布尔值,指示是否开启重新计划功能。

示例

import { Goal } from 'yuka';

class MyGoal extends Goal {

  constructor( entity ) {

    super( entity );

    this.replanIfFailed( true ); // 开启重新计划功能

  }

  // 其他方法

}

备注

如果在执行 evaluate 方法时发现当前目标已经失败(FAILED 状态),则自动调用 replan 方法重新计划一个新目标,直到找到一个成功的目标或没有目标时停止。

示例

import { Goal, Time } from 'yuka';

class WanderGoal extends Goal {

  constructor( entity ) {

    super( entity );
    
    this.replanIfFailed( true );
    this.targetPosition = null;
    this.timeSpent = 0;

  }

  activate() {

    this.timeSpent = 0;
    this.targetPosition = this.entity.position.clone().randomize( 10 );
    return Goal.STATUS_ACTIVE;

  }

  execute() {

    if ( this.entity.position.distanceTo( this.targetPosition ) < 1 ) {

      return Goal.STATUS_COMPLETED;

    }

    this.timeSpent += Time.deltaTime;

    if ( this.timeSpent > 5 ) {
  
      return Goal.STATUS_FAILED;

    }

    this.entity.steering.seek( this.targetPosition );

    return Goal.STATUS_ACTIVE;

  }

}

在上述示例中,当目标 WanderGoal 执行时,如果计划失败,将自动重新计划一个新目标尝试达到预期结果。如果在执行 execute 方法超过 5 秒仍未达到预期结果,该目标将标记为失败。