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

globalState

概述

globalState 是 StateMachine 的一个属性,表示 StateMachine 的全局状态。全局状态在整个状态机的生命周期中一直存在,不会随着状态的转换而改变,并且可以在任何状态下进行读取或写入。

用法

获取全局状态

const sm = new StateMachine({
  globalState: {
    count: 0,
    message: '',
  },
  // ...
});

const count = sm.globalState.count;
const message = sm.globalState.message;

修改全局状态

const sm = new StateMachine({
  globalState: {
    count: 0,
    message: '',
  },
  // ...
});

sm.globalState.count = 1;
sm.globalState.message = 'Hello World';

注意事项

  1. 请勿修改全局状态的引用,例如以下代码:

    const sm = new StateMachine({
      globalState: {
        count: 0,
        message: '',
      },
      // ...
    });
    
    const globalState = sm.globalState;
    globalState.count = 1; // 这会改变 StateMachine 中的全局状态
    

    正确的做法应该是:

    const sm = new StateMachine({
      globalState: {
        count: 0,
        message: '',
      },
      // ...
    });
    
    sm.globalState.count = 1; // 直接修改 StateMachine 中的全局状态
    
  2. 全局状态应该是简单类型或者可序列化的复杂类型。如果全局状态包含函数等不可序列化的属性,可能会导致状态机在序列化和反序列化时出错。

参考资料