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

toJSON

简介

toJSON 方法将 OffsetPursuitBehavior 类型的行为对象转换为 JSON 对象。

语法

behavior.toJSON()

返回值

返回值为一个 JSON 对象。

实现逻辑

在执行 toJSON 方法时,会根据行为对象的当前状态获取相应的属性值,并将属性值存储到一个 JSON 对象中返回。

  • targetId: 目标物体的 ID,类型为字符串。
  • offset: 目标物体相对于自身的偏移量,类型为向量对象。

示例代码

const behavior = new Yuka.OffsetPursuitBehavior();
behavior.target = new Yuka.GameEntity( 'target' );
behavior.offset.set( 10, 0, 0 );

const json = behavior.toJSON();
console.log( json ); // { "type": "OffsetPursuitBehavior", "targetId": "target", "offset": [ 10, 0, 0 ] }

注意事项

  • OffsetPursuitBehavior 类型是 SteeringBehavior 的子类,因此 toJSON 方法只能在 OffsetPursuitBehavior 类型的对象上调用。
  • 转换后的 JSON 对象不包含所有属性,只包含当前状态下需要保存的属性。