cocos-animator/animator-runtime/animator3.x/AnimatorSpine.ts

123 lines
3.8 KiB
TypeScript
Raw Permalink Normal View History

import { sp, _decorator } from "cc";
import AnimatorSpineSecondary from "./AnimatorSpineSecondary";
import AnimatorBase, { AnimationPlayer } from "./core/AnimatorBase";
import AnimatorStateLogic from "./core/AnimatorStateLogic";
const { ccclass, property, requireComponent, disallowMultiple } = _decorator;
/**
* Spine状态机组件trackIndex为0
*/
@ccclass
@disallowMultiple
@requireComponent(sp.Skeleton)
export default class AnimatorSpine extends AnimatorBase {
/** spine组件 */
private _spine: sp.Skeleton = null!;
/** 动画完成的回调 */
private _completeListenerMap: Map<(entry?: any) => void, any> = new Map();
/** 次状态机注册的回调 */
private _secondaryListenerMap: Map<(entry?: any) => void, AnimatorSpineSecondary> = new Map();
protected start() {
if (!this.PlayOnStart || this._hasInit) {
return;
}
this._hasInit = true;
this._spine = this.getComponent(sp.Skeleton)!;
this._spine.setCompleteListener(this.onSpineComplete.bind(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.PlayOnStart || this._hasInit) {
return;
}
this._hasInit = true;
this.initArgs(...args);
this._spine = this.getComponent(sp.Skeleton)!;
this._spine.setCompleteListener(this.onSpineComplete.bind(this));
if (this.AssetRawUrl !== null) {
this.initJson(this.AssetRawUrl.json);
}
}
private onSpineComplete(entry: any) {
entry.trackIndex === 0 && this.onAnimFinished();
this._completeListenerMap.forEach((target, cb) => { target ? cb.call(target, entry) : cb(entry); });
this._secondaryListenerMap.forEach((target, cb) => { entry.trackIndex === target.TrackIndex && cb.call(target, entry); });
}
/**
*
* @override
* @param animName
* @param loop
*/
protected playAnimation(animName: string, loop: boolean) {
if (animName) {
this._spine.setAnimation(0, animName, loop);
} else {
this._spine.clearTrack(0);
}
}
/**
*
* @override
* @param scale
*/
protected scaleTime(scale: number) {
if (scale > 0)
this._spine.timeScale = scale;
}
/**
*
*/
public addSecondaryListener(cb: (entry?: any) => void, target: AnimatorSpineSecondary) {
this._secondaryListenerMap.set(cb, target);
}
/**
*
* @param cb
* @param target this对象
*/
public addCompleteListener(cb: (entry?: any) => void, target: any = null) {
if (this._completeListenerMap.has(cb)) {
return;
}
this._completeListenerMap.set(cb, target);
}
/**
*
* @param cb
*/
public removeCompleteListener(cb: (entry?: any) => void) {
this._completeListenerMap.delete(cb);
}
/**
*
*/
public clearCompleteListener() {
this._completeListenerMap.clear;
}
}