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

Copy

Yuka库的Copy类提供了一个可定制的动作,该动作允许实体将它们的数据复制到指定的实体。

用法

要使用Copy动作,您需要先创建一个Copy实例,并将其添加到您的实体的动作列表中。

import { Copy } from 'yuka';

const copyAction = new Copy( targetEntity, properties, duration );
entity.actions.add( copyAction );

在上面的代码中,targetEntity是您希望将实体数据复制到的目标实体。properties是您希望复制哪些实体属性的数组。duration参数是可选的,并表示复制动画的持续时间,以毫秒为单位。

在创建动作之后,该动作将按照每帧的速率自动更新。如果您需要在将Copy动作添加到实体的动作列表之前设置目标实体或属性列表,则可以使用以下代码:

const copyAction = new Copy();
copyAction.targetEntity = targetEntity;
copyAction.properties = properties;
entity.actions.add( copyAction );

例子

下面的代码演示了如何将一个实体的位置和旋转属性复制到另一个实体,并使其在1秒钟内“跳跃”到目标位置。

import { Vector3, Quaternion, Copy } from 'yuka';

const sourceEntity = new Entity();
sourceEntity.position.set( -10, 0, 0 );
sourceEntity.rotation.setFromAxisAngle( new Vector3( 0, 0, 1 ), 1 );

const targetEntity = new Entity();

const copyAction = new Copy( targetEntity, [ 'position', 'rotation' ], 1000 );
sourceEntity.actions.add( copyAction );

animate();
function animate() {

  requestAnimationFrame( animate );

  const deltaTime = clock.getDelta();

  // 模拟源实体在移动和旋转
  sourceEntity.position.x += deltaTime * 10;
  sourceEntity.rotation.z += deltaTime;

  // 手动更新动作(通常在场景更新循环中自动完成)
  sourceEntity.actions.update( deltaTime );

}

在上面的代码中,源实体具有位置和旋转属性,而目标实体是空的。Copy动作将使用targetEntity作为目标,并指定要复制的属性列表。通过将动画时间设置为1秒钟,可以使目标实体在源实体不断移动和旋转时平滑地跟踪源实体。

注意事项

  • 复制动作只复制原始值,不会深度复制引用属性。
  • 如果在复制过程中目标实体还不存在,则会抛出错误。
  • 如果指定的属性不存在,则不会执行任何操作。
  • 如果没有提供要复制的属性列表,则不会执行任何操作。