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 方法将 Yuka 行为(Behavior)对象转换为 JSON 格式。该方法的返回值是一个包含以下属性的对象:

  • type:字符串,指示行为的类型 (Behavior type)。
  • data:对象,行为的具体数据。
  • priority:数字,行为的优先级。
  • probability:数字,行为被执行的概率。
  • decorators:一个包含一组修饰器(Decorator)对象的数组,这些修饰器被应用于行为中。

语法

const json = behavior.toJSON();

返回值

一个包含 type, data, priority, probabilitydecorators 属性的对象。

示例

import { SeekBehavior } from 'yuka';

const behavior = new SeekBehavior();

behavior.target = { x: 10, y: 20 };
behavior.priority = 1;
behavior.probability = 0.5;

const json = behavior.toJSON();

console.log(json);
// 输出:
// {
//   "type": "SeekBehavior",
//   "data": {
//     "target": {
//       "x": 10,
//       "y": 20
//     }
//   },
//   "priority": 1,
//   "probability": 0.5,
//   "decorators": []
// }

注意事项

如果 SeekBehavior 对象中有一些方法无法 JSON 化,那么将不会被包含在返回的 JSON 格式的对象中。在该情况下,toJSON 方法将只返回 type, data, priority, probabilitydecorators 附带的属性。