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

registerType

为StateMachine注册新的状态类型。

语法

StateMachine.registerType(typeName: string, options: Object);

参数

  • typeName:字符串类型,表示要注册的状态类型名称。
  • options:对象类型,表示注册的状态类型选项,包含以下属性:
    • data:状态的自定义数据对象,默认为null
    • onEnter:函数类型,表示进入该状态时要执行的函数,默认为noop函数。
    • onLeave:函数类型,表示离开该状态时要执行的函数,默认为noop函数。
    • canEnter:函数类型,表示在进入该状态前要满足的条件,默认为始终返回true的函数。
    • canLeave:函数类型,表示在离开该状态前要满足的条件,默认为始终返回true的函数。

示例

const stateMachine = new StateMachine();

// 注册两种状态类型:"started"和"stopped"
StateMachine.registerType("started", {
  data: { timestamp: null },
  onEnter: function() {
    this.data.timestamp = Date.now();
    console.log("Started at " + this.data.timestamp);
  },
  canLeave: function(trigger, nextState) {
    const duration = Date.now() - this.data.timestamp;
    console.log("Stopped after " + duration + "ms");
    return true;
  }
});

StateMachine.registerType("stopped", {
  onEnter: function() {
    console.log("Stopped");
  }
});

// 设置状态机的初始状态为"started"
stateMachine.setState("started");

// 触发"stopped"状态
stateMachine.trigger("stop");

/* output:
Started at 1627095590964
Stopped after 175ms
Stopped
*/

备注

  • typeName不能与已有的状态类型名称重复,否则会抛出Error异常。
  • typeName必须是一个合法的 JavaScript 标识符。
  • options中的函数中的this指向当前的状态对象。