cocos-animator/animator-runtime/core/AnimatorTransition.ts

72 lines
2.3 KiB
TypeScript
Raw Normal View History

2021-01-19 14:30:12 +00:00
import AnimatorCondition, { ParamType } from "./AnimatorCondition";
import AnimatorController from "./AnimatorController";
/**
*
*/
export default class AnimatorTransition {
private _toStateName: string = '';
private _hasExitTime: boolean = false;
private _conditions: AnimatorCondition[] = [];
private _ac: AnimatorController = null;
constructor(data: any, ac: AnimatorController) {
this._toStateName = data.toState;
this._hasExitTime = data.hasExitTime;
this._ac = ac;
for (let i = 0; i < data.conditions.length; i++) {
let condition: AnimatorCondition = new AnimatorCondition(data.conditions[i], ac);
this._conditions.push(condition);
}
}
/**
* transition是否有效hasExitTime以及没有添加任何condition时此transition无效并忽略
*/
public isValid(): boolean {
return this._hasExitTime || this._conditions.length > 0;
}
/**
*
*/
public check(): boolean {
if (this._toStateName === this._ac.curState.name) {
return false;
}
if (this._hasExitTime && (this._ac.curState !== this._ac.animCompleteState || !this._ac.animComplete)) {
return false;
}
for (let i = 0; i < this._conditions.length; i++) {
if (!this._conditions[i].check()) {
return false;
}
}
return true;
}
/**
*
*/
public doTrans() {
// 满足条件时重置动画播完标记
if (this._hasExitTime) {
this._ac.animComplete = false;
}
// 满足状态转换条件时重置trigger和autoTrigger
for (let i = 0; i < this._conditions.length; i++) {
let type = this._conditions[i].getParamType();
let name = this._conditions[i].getParamName();
if (type === ParamType.TRIGGER) {
this._ac.params.resetTrigger(name);
} else if (type === ParamType.AUTO_TRIGGER) {
this._ac.params.resetAutoTrigger(name);
}
}
this._ac.changeState(this._toStateName);
}
}