53 lines
1.6 KiB
TypeScript
Raw Permalink Normal View History

2022-12-08 21:14:02 +08:00
import { _decorator, Animation } from "cc";
import State from "../../Base/State";
import StateMachine, { getInitParamsTrigger } from "../../Base/StateMachine";
import { EntityTypeEnum } from "../../Common";
import { EntityStateEnum, ParamsNameEnum } from "../../Enum";
import ObjectPoolManager from "../../Global/ObjectPoolManager";
const { ccclass, property } = _decorator;
2022-11-27 23:23:47 +08:00
2022-12-08 21:14:02 +08:00
@ccclass("ExplosionStateMachine")
2022-11-27 23:23:47 +08:00
export class ExplosionStateMachine extends StateMachine {
init(type: EntityTypeEnum) {
2022-12-08 21:14:02 +08:00
this.type = type;
this.animationComponent = this.node.addComponent(Animation);
2022-11-27 23:23:47 +08:00
2022-12-08 21:14:02 +08:00
this.initParams();
this.initStateMachines();
this.initAnimationEvent();
2022-11-27 23:23:47 +08:00
}
initParams() {
2022-12-08 21:14:02 +08:00
this.params.set(ParamsNameEnum.Idle, getInitParamsTrigger());
2022-11-27 23:23:47 +08:00
}
initStateMachines() {
2022-12-08 21:14:02 +08:00
this.stateMachines.set(ParamsNameEnum.Idle, new State(this, `${this.type}${EntityStateEnum.Idle}`));
2022-11-27 23:23:47 +08:00
}
initAnimationEvent() {
this.animationComponent.on(Animation.EventType.FINISHED, () => {
2022-12-08 21:14:02 +08:00
const whiteList = [EntityStateEnum.Idle];
const name = this.animationComponent.defaultClip.name;
if (whiteList.some((v) => name.includes(v))) {
ObjectPoolManager.Instance.ret(this.node);
2022-11-27 23:23:47 +08:00
}
2022-12-08 21:14:02 +08:00
});
2022-11-27 23:23:47 +08:00
}
run() {
switch (this.currentState) {
case this.stateMachines.get(ParamsNameEnum.Idle):
if (this.params.get(ParamsNameEnum.Idle).value) {
2022-12-08 21:14:02 +08:00
this.currentState = this.stateMachines.get(ParamsNameEnum.Idle);
2022-11-27 23:23:47 +08:00
} else {
2022-12-08 21:14:02 +08:00
this.currentState = this.currentState;
2022-11-27 23:23:47 +08:00
}
2022-12-08 21:14:02 +08:00
break;
2022-11-27 23:23:47 +08:00
default:
2022-12-08 21:14:02 +08:00
this.currentState = this.stateMachines.get(ParamsNameEnum.Idle);
break;
2022-11-27 23:23:47 +08:00
}
}
}