2022-12-08 21:14:02 +08:00
|
|
|
|
import State from "./State";
|
|
|
|
|
import StateMachine from "./StateMachine";
|
2022-11-27 23:23:47 +08:00
|
|
|
|
|
|
|
|
|
/***
|
|
|
|
|
* 子有限状态机基类
|
|
|
|
|
* 用处:例如有个idle的state,但是有多个方向,为了让主状态机更整洁,可以把同类型的但具体不同的state都封装在子状态机中
|
|
|
|
|
*/
|
|
|
|
|
export default abstract class SubStateMachine {
|
2022-12-08 21:14:02 +08:00
|
|
|
|
private _currentState: State = null;
|
|
|
|
|
stateMachines: Map<string, State> = new Map();
|
2022-11-27 23:23:47 +08:00
|
|
|
|
|
|
|
|
|
constructor(public fsm: StateMachine) {}
|
|
|
|
|
|
|
|
|
|
get currentState() {
|
2022-12-08 21:14:02 +08:00
|
|
|
|
return this._currentState;
|
2022-11-27 23:23:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
set currentState(newState) {
|
|
|
|
|
if (!newState) {
|
2022-12-08 21:14:02 +08:00
|
|
|
|
return;
|
2022-11-27 23:23:47 +08:00
|
|
|
|
}
|
2022-12-08 21:14:02 +08:00
|
|
|
|
this._currentState = newState;
|
|
|
|
|
this._currentState.run();
|
2022-11-27 23:23:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/***
|
|
|
|
|
* 具体类实现
|
|
|
|
|
*/
|
2022-12-08 21:14:02 +08:00
|
|
|
|
abstract run(): void;
|
2022-11-27 23:23:47 +08:00
|
|
|
|
}
|