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

229 lines
6.6 KiB
TypeScript
Raw Normal View History

2021-01-19 14:30:12 +00:00
import AnimatorController from "./AnimatorController";
import AnimatorState from "./AnimatorState";
import AnimatorStateLogic from "./AnimatorStateLogic";
const { ccclass, property, executionOrder } = cc._decorator;
/**
*
*/
export interface AnimationPlayer {
/** 设置动画播放结束的回调 */
setFinishedCallback(callback: (event: cc.Event.EventCustom) => void, target?: any): void;
/** 播放动画 */
playAnimation(animName: string, loop: boolean): void;
/** 缩放动画播放速率 */
scaleTime(scale: number): void;
}
/**
*
*/
@ccclass
@executionOrder(-1000)
export default class AnimatorBase extends cc.Component {
@property({ type: cc.JsonAsset, tooltip: CC_DEV && '状态机json文件' })
protected AssetRawUrl: cc.JsonAsset = null;
@property({ tooltip: CC_DEV && '是否在onLoad中自动启动状态机' })
protected PlayOnLoad: boolean = true;
@property({ tooltip: CC_DEV && '是否在update中自动触发状态机逻辑更新' })
protected AutoUpdate: boolean = true;
/** 是否初始化 */
protected _hasInit: boolean = false;
/** 状态机控制 */
protected _ac: AnimatorController = null;
/** 各个状态逻辑控制key为状态名 */
protected _stateLogicMap: Map<string, AnimatorStateLogic> = null;
/** 状态切换时的回调 */
protected _onStateChangeCall: (fromState: string, toState: string) => void = null;
/** 自定义的动画播放控制器 */
protected _animationPlayer: AnimationPlayer = null;
public get curStateName(): string {
return this._ac.curState.name;
}
public get curStateMotion(): string {
return this._ac.curState.motion;
}
/**
* 0-3
* - onStateChangeCall
* - stateLogicMap
* - animationPlayer
* @virtual
*/
public onInit(...args: Array<Map<string, AnimatorStateLogic> | ((fromState: string, toState: string) => void) | AnimationPlayer>) {
}
/**
*
*/
protected initArgs(...args: Array<Map<string, AnimatorStateLogic> | ((fromState: string, toState: string) => void) | AnimationPlayer>) {
args.forEach((arg) => {
if (!arg) {
return;
}
if (typeof arg === 'function') {
this._onStateChangeCall = arg;
} else if (typeof arg === 'object') {
if (arg instanceof Map) {
this._stateLogicMap = arg;
} else {
this._animationPlayer = arg;
this._animationPlayer.setFinishedCallback(this.onAnimFinished, this);
}
}
});
}
private updateAnimator() {
// 混合当前动画播放速度
let playSpeed = this._ac.curState.speed;
if (this._ac.curState.multi) {
playSpeed *= this._ac.params.getNumber(this._ac.curState.multi) || 1;
}
this.scaleTime(playSpeed);
// 更新AnimatorStateLogic
if (this._stateLogicMap) {
let curLogic = this._stateLogicMap.get(this._ac.curState.name);
curLogic && curLogic.onUpdate();
}
// 更新状态机逻辑
this._ac.updateAnimator();
}
protected update() {
if (this._hasInit && this.AutoUpdate) {
this.updateAnimator();
}
}
/**
*
*/
public manualUpdate() {
if (this._hasInit && !this.AutoUpdate) {
this.updateAnimator();
}
}
/**
* json文件
*/
protected initJson(json: any) {
this._ac = new AnimatorController(this, json);
}
/**
*
*/
protected onAnimFinished() {
this._ac.onAnimationComplete();
}
/**
*
* @virtual
* @param animName
* @param loop
*/
protected playAnimation(animName: string, loop: boolean) {
}
/**
*
* @virtual
* @param scale
*/
protected scaleTime(scale: number) {
}
/**
*
*/
public onStateChange(fromState: AnimatorState, toState: AnimatorState) {
if (toState.motion && toState.motion !== "") {
this.playAnimation(toState.motion, toState.loop);
}
let fromStateName = fromState ? fromState.name : '';
if (this._stateLogicMap) {
let fromLogic = this._stateLogicMap.get(fromStateName);
fromLogic && fromLogic.onExit();
let toLogic = this._stateLogicMap.get(toState.name);
toLogic && toLogic.onEntry();
}
this._onStateChangeCall && this._onStateChangeCall(fromStateName, toState.name);
}
/**
* boolean类型参数的值
*/
public setBool(key: string, value: boolean) {
this._ac.params.setBool(key, value);
}
/**
* boolean类型参数的值
*/
public getBool(key: string): boolean {
return this._ac.params.getBool(key) !== 0;
}
/**
* number类型参数的值
*/
public setNumber(key: string, value: number) {
this._ac.params.setNumber(key, value);
}
/**
* number类型参数的值
*/
public getNumber(key: string): number {
return this._ac.params.getNumber(key);
}
/**
* trigger类型参数的值
*/
public setTrigger(key: string) {
this._ac.params.setTrigger(key);
}
/**
* trigger类型参数的值
*/
public resetTrigger(key: string) {
this._ac.params.resetTrigger(key);
}
/**
* autoTrigger类型参数的值autoTrigger类型参数不需要主动resetreset
*/
public autoTrigger(key: string) {
this._ac.params.autoTrigger(key);
}
/**
*
* @param
*/
public play(stateName: string) {
if (!this._hasInit) {
return;
}
this._ac.play(stateName);
}
}