cocos-animator/animator-runtime/AnimatorAnimation.ts

75 lines
2.3 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 } = cc._decorator;
/** Animation状态机组件 */
@ccclass
@requireComponent(cc.Animation)
export default class AnimatorAnimation extends AnimatorBase {
/** Animation组件 */
private _animation: cc.Animation = null;
/** 当前的动画实例 */
private _animState: cc.AnimationState = null;
protected onLoad() {
if (!this.PlayOnLoad || this._hasInit) {
return;
}
this._hasInit = true;
this._animation = this.getComponent(cc.Animation);
this._animation.on("finished", this.onAnimFinished, this);
this._animation.on("lastframe", this.onAnimFinished, this);
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.PlayOnLoad || this._hasInit) {
return;
}
this._hasInit = true;
this.initArgs(...args);
this._animation = this.getComponent(cc.Animation);
this._animation.on("finished", this.onAnimFinished, this);
this._animation.on("lastframe", this.onAnimFinished, this);
if (this.AssetRawUrl !== null) {
this.initJson(this.AssetRawUrl.json);
}
}
/**
*
* @override
* @param animName
* @param loop
*/
protected playAnimation(animName: string, loop: boolean) {
this._animState = this._animation.play(animName);
this._animState.wrapMode = loop ? cc.WrapMode.Loop : cc.WrapMode.Default;
}
/**
*
* @override
* @param scale
*/
protected scaleTime(scale: number) {
if (scale > 0 && this._animState)
this._animState.speed = scale;
}
}