55 lines
1.9 KiB
TypeScript
Raw Normal View History

2022-11-27 23:23:47 +08:00
import { _decorator, Animation, AnimationClip } from 'cc'
import State from '../../Base/State'
2022-12-01 22:26:41 +08:00
import StateMachine, { getInitParamsTrigger } from '../../Base/StateMachine'
2022-11-27 23:23:47 +08:00
import { EntityStateEnum, EntityTypeEnum, ParamsNameEnum } from '../../Enum'
import { WeaponManager } from './WeaponManager'
2022-12-01 22:26:41 +08:00
const { ccclass } = _decorator
2022-11-27 23:23:47 +08:00
@ccclass('WeaponStateMachine')
export class WeaponStateMachine extends StateMachine {
init(type: EntityTypeEnum) {
this.type = type
this.animationComponent = this.node.addComponent(Animation)
this.initParams()
this.initStateMachines()
this.initAnimationEvent()
}
initParams() {
this.params.set(ParamsNameEnum.Idle, getInitParamsTrigger())
this.params.set(ParamsNameEnum.Attack, getInitParamsTrigger())
}
initStateMachines() {
this.stateMachines.set(ParamsNameEnum.Idle, new State(this, `${this.type}${EntityStateEnum.Idle}`))
this.stateMachines.set(ParamsNameEnum.Attack, new State(this, `${this.type}${EntityStateEnum.Attack}`, AnimationClip.WrapMode.Normal, true))
}
initAnimationEvent() {
this.animationComponent.on(Animation.EventType.FINISHED, () => {
2022-12-01 22:26:41 +08:00
if (this.animationComponent.defaultClip.name.includes(EntityStateEnum.Attack)) {
2022-11-27 23:23:47 +08:00
this.node.parent.getComponent(WeaponManager).state = EntityStateEnum.Idle
}
})
}
run() {
switch (this.currentState) {
case this.stateMachines.get(ParamsNameEnum.Idle):
case this.stateMachines.get(ParamsNameEnum.Attack):
if (this.params.get(ParamsNameEnum.Attack).value) {
this.currentState = this.stateMachines.get(ParamsNameEnum.Attack)
} if (this.params.get(ParamsNameEnum.Idle).value) {
this.currentState = this.stateMachines.get(ParamsNameEnum.Idle)
} else {
this.currentState = this.currentState
}
break
default:
this.currentState = this.stateMachines.get(ParamsNameEnum.Idle)
break
}
}
}