2021-01-19 14:30:12 +00:00
|
|
|
|
import AnimatorBase, { AnimationPlayer } from "./core/AnimatorBase";
|
|
|
|
|
import AnimatorStateLogic from "./core/AnimatorStateLogic";
|
|
|
|
|
|
2021-01-24 11:29:37 +00:00
|
|
|
|
const { ccclass, property, requireComponent, disallowMultiple } = cc._decorator;
|
2021-01-19 14:30:12 +00:00
|
|
|
|
|
2021-01-24 11:29:37 +00:00
|
|
|
|
/**
|
|
|
|
|
* Cocos Animation状态机组件
|
|
|
|
|
*/
|
2021-01-19 14:30:12 +00:00
|
|
|
|
@ccclass
|
2021-01-24 11:29:37 +00:00
|
|
|
|
@disallowMultiple
|
2021-01-19 14:30:12 +00:00
|
|
|
|
@requireComponent(cc.Animation)
|
|
|
|
|
export default class AnimatorAnimation extends AnimatorBase {
|
|
|
|
|
/** Animation组件 */
|
|
|
|
|
private _animation: cc.Animation = null;
|
|
|
|
|
/** 当前的动画实例 */
|
|
|
|
|
private _animState: cc.AnimationState = null;
|
2021-01-24 11:29:37 +00:00
|
|
|
|
/** 记录初始的wrapmode */
|
|
|
|
|
private _wrapModeMap: Map<cc.AnimationState, cc.WrapMode> = new Map();
|
2021-01-19 14:30:12 +00:00
|
|
|
|
|
2021-01-24 11:29:37 +00:00
|
|
|
|
protected start() {
|
|
|
|
|
if (!this.PlayOnStart || this._hasInit) {
|
2021-01-19 14:30:12 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
this._hasInit = true;
|
|
|
|
|
|
|
|
|
|
this._animation = this.getComponent(cc.Animation);
|
2021-01-24 11:29:37 +00:00
|
|
|
|
this._animation.on(cc.Animation.EventType.FINISHED, this.onAnimFinished, this);
|
|
|
|
|
this._animation.on(cc.Animation.EventType.LASTFRAME, this.onAnimFinished, this);
|
2021-01-19 14:30:12 +00:00
|
|
|
|
|
|
|
|
|
if (this.AssetRawUrl !== null) {
|
|
|
|
|
this.initJson(this.AssetRawUrl.json);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 手动初始化状态机,可传入0-3个参数,类型如下
|
|
|
|
|
* - onStateChangeCall 状态切换时的回调
|
|
|
|
|
* - stateLogicMap 各个状态逻辑控制
|
|
|
|
|
* - animationPlayer 自定义动画控制
|
|
|
|
|
* @override
|
|
|
|
|
*/
|
|
|
|
|
public onInit(...args: Array<Map<string, AnimatorStateLogic> | ((fromState: string, toState: string) => void) | AnimationPlayer>) {
|
2021-01-24 11:29:37 +00:00
|
|
|
|
if (this.PlayOnStart || this._hasInit) {
|
2021-01-19 14:30:12 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
this._hasInit = true;
|
|
|
|
|
|
|
|
|
|
this.initArgs(...args);
|
|
|
|
|
|
|
|
|
|
this._animation = this.getComponent(cc.Animation);
|
2021-01-24 11:29:37 +00:00
|
|
|
|
this._animation.on(cc.Animation.EventType.FINISHED, this.onAnimFinished, this);
|
|
|
|
|
this._animation.on(cc.Animation.EventType.LASTFRAME, this.onAnimFinished, this);
|
2021-01-19 14:30:12 +00:00
|
|
|
|
|
|
|
|
|
if (this.AssetRawUrl !== null) {
|
|
|
|
|
this.initJson(this.AssetRawUrl.json);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 播放动画
|
|
|
|
|
* @override
|
|
|
|
|
* @param animName 动画名
|
|
|
|
|
* @param loop 是否循环播放
|
|
|
|
|
*/
|
|
|
|
|
protected playAnimation(animName: string, loop: boolean) {
|
2021-01-24 11:29:37 +00:00
|
|
|
|
if (!animName) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-19 14:30:12 +00:00
|
|
|
|
this._animState = this._animation.play(animName);
|
2021-01-24 11:29:37 +00:00
|
|
|
|
if (!this._animState) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (!this._wrapModeMap.has(this._animState)) {
|
|
|
|
|
this._wrapModeMap.set(this._animState, this._animState.wrapMode);
|
|
|
|
|
}
|
|
|
|
|
this._animState.wrapMode = loop ? cc.WrapMode.Loop : this._wrapModeMap.get(this._animState);
|
2021-01-19 14:30:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 缩放动画播放速率
|
|
|
|
|
* @override
|
|
|
|
|
* @param scale 缩放倍率
|
|
|
|
|
*/
|
|
|
|
|
protected scaleTime(scale: number) {
|
2021-01-24 11:29:37 +00:00
|
|
|
|
if (this._animState) {
|
2021-01-19 14:30:12 +00:00
|
|
|
|
this._animState.speed = scale;
|
2021-01-24 11:29:37 +00:00
|
|
|
|
}
|
2021-01-19 14:30:12 +00:00
|
|
|
|
}
|
|
|
|
|
}
|