cocos-animator/animator-runtime/animator2.x/AnimatorAnimation.ts

91 lines
2.9 KiB
TypeScript
Raw Normal View History

2021-01-19 14:30:12 +00:00
import AnimatorBase, { AnimationPlayer } from "./core/AnimatorBase";
import AnimatorStateLogic from "./core/AnimatorStateLogic";
const { ccclass, property, requireComponent, disallowMultiple } = cc._decorator;
2021-01-19 14:30:12 +00:00
/**
* Cocos Animation状态机组件
*/
2021-01-19 14:30:12 +00:00
@ccclass
@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;
/** 记录初始的wrapmode */
private _wrapModeMap: Map<cc.AnimationState, cc.WrapMode> = new Map();
2021-01-19 14:30:12 +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);
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>) {
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);
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) {
if (!animName) {
return;
}
2021-01-19 14:30:12 +00:00
this._animState = this._animation.play(animName);
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) {
if (this._animState) {
2021-01-19 14:30:12 +00:00
this._animState.speed = scale;
}
2021-01-19 14:30:12 +00:00
}
}