mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-01 07:42:38 +08:00
9711e049ba
* skeletal socket system * fix
507 lines
17 KiB
TypeScript
507 lines
17 KiB
TypeScript
/*
|
||
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
||
|
||
http://www.cocos.com
|
||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||
of this software and associated engine source code (the "Software"), a limited,
|
||
worldwide, royalty-free, non-assignable, revocable and non-exclusive license
|
||
to use Cocos Creator solely to develop games on your target platforms. You shall
|
||
not use Cocos Creator software for developing other software or tools that's
|
||
used for developing games. You are not granted to publish, distribute,
|
||
sublicense, and/or sell copies of Cocos Creator.
|
||
|
||
The software or tools in this License Agreement are licensed, not sold.
|
||
Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you.
|
||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||
THE SOFTWARE.
|
||
*/
|
||
|
||
/**
|
||
* @category animation
|
||
*/
|
||
|
||
import { Component } from '../components/component';
|
||
import { ccclass, executeInEditMode, executionOrder, menu, property } from '../core/data/class-decorator';
|
||
import { Event, EventTarget } from '../core/event';
|
||
import { CallbacksInvoker, ICallbackTable } from '../core/event/callbacks-invoker';
|
||
import { applyMixins, IEventTarget } from '../core/event/event-target-factory';
|
||
import { warnID } from '../core/platform/CCDebug';
|
||
import * as ArrayUtils from '../core/utils/array';
|
||
import { createMap } from '../core/utils/js-typed';
|
||
import { ccenum } from '../core/value-types/enum';
|
||
import { AnimationClip } from './animation-clip';
|
||
import { AnimationState } from './animation-state';
|
||
import { CrossFade } from './cross-fade';
|
||
|
||
/**
|
||
* @en The event type supported by Animation
|
||
* @zh Animation 支持的事件类型。
|
||
*/
|
||
export enum EventType {
|
||
/**
|
||
* @en Emit when begin playing animation
|
||
* @zh 开始播放时触发。
|
||
*/
|
||
PLAY = 'play',
|
||
/**
|
||
* @en Emit when stop playing animation
|
||
* @zh 停止播放时触发。
|
||
*/
|
||
STOP = 'stop',
|
||
/**
|
||
* @en Emit when pause animation
|
||
* @zh 暂停播放时触发。
|
||
*/
|
||
PAUSE = 'pause',
|
||
/**
|
||
* @en Emit when resume animation
|
||
* @zh 恢复播放时触发。
|
||
*/
|
||
RESUME = 'resume',
|
||
/**
|
||
* @en If animation repeat count is larger than 1, emit when animation play to the last frame
|
||
* @zh 假如动画循环次数大于 1,当动画播放到最后一帧时触发。
|
||
*/
|
||
LASTFRAME = 'lastframe',
|
||
/**
|
||
* @en Emit when finish playing animation
|
||
* @zh 动画播放完成时触发。
|
||
*/
|
||
FINISHED = 'finished',
|
||
}
|
||
ccenum(EventType);
|
||
|
||
/**
|
||
* 动画组件管理动画状态来控制动画的播放。
|
||
* 它提供了方便的接口用来预创建指定动画剪辑的动画状态,并提供了一系列事件:
|
||
* - play : 开始播放时
|
||
* - stop : 停止播放时
|
||
* - pause : 暂停播放时
|
||
* - resume : 恢复播放时
|
||
* - lastframe : 假如动画循环次数大于 1,当动画播放到最后一帧时
|
||
* - finished : 动画播放完成时
|
||
*/
|
||
@ccclass('cc.AnimationComponent')
|
||
@executionOrder(99)
|
||
@executeInEditMode
|
||
@menu('Components/AnimationComponent')
|
||
export class AnimationComponent extends Component implements IEventTarget {
|
||
/**
|
||
* 获取此动画组件的自有动画剪辑。
|
||
* 动画组件开始运行时会为每个自有动画剪辑创建动画状态。
|
||
*/
|
||
@property({ type: [AnimationClip] })
|
||
get clips () {
|
||
return this._clips;
|
||
}
|
||
|
||
/**
|
||
* 设置此动画组件的自有动画剪辑。
|
||
* 设置时将移除已有的动画剪辑,并将其动画状态设为停止;若默认动画剪辑不在新的自有动画剪辑中,将被重置为空。
|
||
*/
|
||
set clips (value) {
|
||
if (this._crossFade) {
|
||
this._crossFade.clear();
|
||
}
|
||
// Remove state for old automatic clips.
|
||
for (const clip of this._clips) {
|
||
if (clip) {
|
||
this._removeStateOfAutomaticClip(clip);
|
||
}
|
||
}
|
||
// Create state for new clips.
|
||
for (const clip of value) {
|
||
if (clip) {
|
||
this.createState(clip);
|
||
}
|
||
}
|
||
// Default clip should be in the list of automatic clips.
|
||
const newDefaultClip = value.find((clip) => equalClips(clip, this._defaultClip));
|
||
if (newDefaultClip) {
|
||
this._defaultClip = newDefaultClip;
|
||
} else {
|
||
this._defaultClip = null;
|
||
}
|
||
|
||
this._clips = value;
|
||
}
|
||
|
||
/**
|
||
* 获取默认动画剪辑。
|
||
* @see [[playOnLoad]]
|
||
*/
|
||
@property({ type: AnimationClip })
|
||
get defaultClip () {
|
||
return this._defaultClip;
|
||
}
|
||
|
||
/**
|
||
* 设置默认动画剪辑。当指定的剪辑不在 `this.clips` 中时会被自动添加至 `this.clips`。
|
||
*/
|
||
set defaultClip (value) {
|
||
this._defaultClip = value;
|
||
if (!value) {
|
||
return;
|
||
}
|
||
const isBoundedDefaultClip = this._clips.findIndex((clip) => equalClips(clip, value)) >= 0;
|
||
if (!isBoundedDefaultClip) {
|
||
this._clips.push(value);
|
||
this.createState(value);
|
||
}
|
||
}
|
||
|
||
public static EventType = EventType;
|
||
|
||
/**
|
||
* 是否在动画组件开始运行时自动播放默认动画剪辑。
|
||
*/
|
||
@property
|
||
public playOnLoad = false;
|
||
|
||
public _callbackTable: ICallbackTable = createMap(true);
|
||
|
||
protected _crossFade = new CrossFade();
|
||
|
||
protected _nameToState: { [name: string]: AnimationState; } = createMap(true);
|
||
|
||
@property({ type: [AnimationClip] })
|
||
protected _clips: Array<(AnimationClip | null)> = [];
|
||
|
||
@property
|
||
protected _defaultClip: AnimationClip | null = null;
|
||
|
||
public onLoad () {
|
||
this.clips = this._clips;
|
||
}
|
||
|
||
public start () {
|
||
for (const stateName of Object.keys(this._nameToState)) {
|
||
const state = this._nameToState[stateName];
|
||
state.initialize(this.node);
|
||
}
|
||
cc.director.getAnimationManager().addCrossFade(this._crossFade);
|
||
this._crossFade.play();
|
||
if (!CC_EDITOR && this.playOnLoad && this._defaultClip) {
|
||
this.crossFade(this._defaultClip.name, 0);
|
||
}
|
||
}
|
||
|
||
public onEnable () {
|
||
if (this._crossFade) {
|
||
this._crossFade.resume();
|
||
}
|
||
}
|
||
|
||
public onDisable () {
|
||
if (this._crossFade) {
|
||
this._crossFade.pause();
|
||
}
|
||
}
|
||
|
||
public onDestroy () {
|
||
if (this._crossFade) {
|
||
this._crossFade.stop();
|
||
cc.director.getAnimationManager().removeCrossFade(this._crossFade);
|
||
this._crossFade.stop();
|
||
}
|
||
for (const name of Object.keys(this._nameToState)) {
|
||
const state = this._nameToState[name];
|
||
state.stop();
|
||
}
|
||
this._nameToState = null as any;
|
||
}
|
||
|
||
/**
|
||
* 立即切换到指定动画状态。
|
||
* @param [name] 目标动画状态的名称;若未指定,使用默认动画剪辑的名称。
|
||
*/
|
||
public play (name?: string) {
|
||
if (!name) {
|
||
if (!this._defaultClip) {
|
||
return;
|
||
} else {
|
||
name = this._defaultClip.name;
|
||
}
|
||
}
|
||
this.crossFade(name, 0);
|
||
}
|
||
|
||
/**
|
||
* 在指定周期内从当前动画状态平滑地切换到指定动画状态。
|
||
* @param name 目标动画状态的名称。
|
||
* @param duration 切换周期,单位为秒。
|
||
*/
|
||
public crossFade (name: string, duration = 0.3) {
|
||
const state = this._nameToState[name];
|
||
if (state) {
|
||
this._crossFade.crossFade(state, duration);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 暂停所有动画状态,并暂停动画切换。
|
||
*/
|
||
public pause () {
|
||
this._crossFade.pause();
|
||
}
|
||
|
||
/**
|
||
* 恢复所有动画状态,并继续动画切换。
|
||
*/
|
||
public resume () {
|
||
this._crossFade.resume();
|
||
}
|
||
|
||
/**
|
||
* 停止所有动画状态,并停止动画切换。
|
||
*/
|
||
public stop () {
|
||
this._crossFade.stop();
|
||
}
|
||
|
||
/**
|
||
* 获取指定的动画状态。
|
||
* @deprecated 将在 V1.0.0 移除,请转用 `this.getState()`。
|
||
*/
|
||
public getAnimationState (name: string) {
|
||
return this.getState(name);
|
||
}
|
||
|
||
/**
|
||
* 获取指定的动画状态。
|
||
* @param name 动画状态的名称。
|
||
* @returns 不存在指定名称的动画状态时返回空,否则返回指定的动画状态。
|
||
*/
|
||
public getState (name: string) {
|
||
const state = this._nameToState[name];
|
||
if (state && !state.curveLoaded) {
|
||
state.initialize(this.node);
|
||
}
|
||
return state || null;
|
||
}
|
||
|
||
/**
|
||
* 使用指定的动画剪辑创建一个动画状态,并将其命名为指定的名称。
|
||
* 若指定名称的动画状态已存在,已存在的动画状态将先被设为停止并被移除。
|
||
* @param clip 动画剪辑。
|
||
* @param name 动画状态的名称,若未指定,则使用动画剪辑的名称。
|
||
* @returns 新创建的动画状态。
|
||
*/
|
||
public createState (clip: AnimationClip, name?: string) {
|
||
name = name || clip.name;
|
||
this.removeState(name);
|
||
|
||
return this._doCreateState(clip, name);
|
||
}
|
||
|
||
/**
|
||
* 停止并移除指定名称的动画状态。
|
||
* @param name 动画状态的名称。
|
||
*/
|
||
public removeState (name: string) {
|
||
const state = this._nameToState[name];
|
||
if (state) {
|
||
state.stop();
|
||
delete this._nameToState[name];
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 添加一个动画剪辑到 `this.clips`中并以此剪辑创建动画状态。
|
||
* @deprecated 将在 V1.0.0 移除,请转用 `this.createState()`。
|
||
* @param clip 动画剪辑。
|
||
* @param name 动画状态的名称,若未指定,则使用动画剪辑的名称。
|
||
* @returns 新创建的动画状态。
|
||
*/
|
||
public addClip (clip: AnimationClip, name?: string): AnimationState {
|
||
if (!ArrayUtils.contains(this._clips, clip)) {
|
||
this._clips.push(clip);
|
||
}
|
||
return this.createState(clip, name);
|
||
}
|
||
|
||
/**
|
||
* @en
|
||
* Remove clip from the animation list. This will remove the clip and any animation states based on it.<br>
|
||
* If there are animation states depand on the clip are playing or clip is defaultClip, it will not delete the clip.<br>
|
||
* But if force is true, then will always remove the clip and any animation states based on it. If clip is defaultClip, defaultClip will be reset to null
|
||
* @zh
|
||
* 从动画列表中移除指定的动画剪辑,<br/>
|
||
* 如果依赖于 clip 的 AnimationState 正在播放或者 clip 是 defaultClip 的话,默认是不会删除 clip 的。<br/>
|
||
* 但是如果 force 参数为 true,则会强制停止该动画,然后移除该动画剪辑和相关的动画。这时候如果 clip 是 defaultClip,defaultClip 将会被重置为 null。<br/>
|
||
* @deprecated 将在 V1.0.0 移除,请转用 `this.removeState()`。
|
||
* @param {Boolean} [force=false] - If force is true, then will always remove the clip and any animation states based on it.
|
||
*/
|
||
public removeClip (clip: AnimationClip, force?: boolean) {
|
||
let state: AnimationState | undefined;
|
||
for (const name of Object.keys(this._nameToState)) {
|
||
state = this._nameToState[name];
|
||
const stateClip = state.clip;
|
||
if (stateClip === clip) {
|
||
break;
|
||
}
|
||
}
|
||
|
||
if (clip === this._defaultClip) {
|
||
if (force) { this._defaultClip = null; }
|
||
else {
|
||
if (!CC_TEST) { warnID(3902); }
|
||
return;
|
||
}
|
||
}
|
||
|
||
if (state && state.isPlaying) {
|
||
if (force) { state.stop(); }
|
||
else {
|
||
if (!CC_TEST) { warnID(3903); }
|
||
return;
|
||
}
|
||
}
|
||
|
||
this._clips = this._clips.filter((item) => item !== clip);
|
||
|
||
if (state) {
|
||
delete this._nameToState[state.name];
|
||
}
|
||
}
|
||
|
||
/**
|
||
* @en
|
||
* Register animation event callback.<bg>
|
||
* The event arguments will provide the AnimationState which emit the event.<bg>
|
||
* When play an animation, will auto register the event callback to the AnimationState,<bg>
|
||
* and unregister the event callback from the AnimationState when animation stopped.
|
||
* @zh
|
||
* 注册动画事件回调。<bg>
|
||
* 回调的事件里将会附上发送事件的 AnimationState。<bg>
|
||
* 当播放一个动画时,会自动将事件注册到对应的 AnimationState 上,停止播放时会将事件从这个 AnimationState 上取消注册。
|
||
* @param type - 表示要侦听的事件类型的字符串。
|
||
* @param callback - 调度事件时将调用的回调。
|
||
* 如果回调是重复的(回调是唯一的),则忽略回调。
|
||
* @param target - 调用回调的目标(此对象)可以为null
|
||
* @return 只返回传入的回调,以便可以更轻松地保存匿名函数。
|
||
* @example
|
||
* ```typescript
|
||
* onPlay: function (type, state) {
|
||
* // callback
|
||
* }
|
||
*
|
||
* // register event to all animation
|
||
* animation.on('play', this.onPlay, this);
|
||
* ```
|
||
*/
|
||
public on (type: string, callback: (state: AnimationState) => void, target?: Object) {
|
||
const ret = EventTarget.prototype.on.call(this, type, callback, target);
|
||
if (type === 'lastframe') {
|
||
for (const stateName of Object.keys(this._nameToState)) {
|
||
this._nameToState[stateName]!._lastframeEventOn = true;
|
||
}
|
||
}
|
||
return ret;
|
||
}
|
||
|
||
/**
|
||
* @en
|
||
* Unregister animation event callback.
|
||
* @zh
|
||
* 取消注册动画事件回调。
|
||
* @param {String} type - 要删除的事件类型的字符串。
|
||
* @param {Function} callback - 要删除的回调
|
||
* @param {Object} target - 调用回调的目标(此对象),如果没有给出,则只删除没有目标的回调
|
||
* @example
|
||
* ```typescript
|
||
* // unregister event to all animation
|
||
* animation.off('play', this.onPlay, this);
|
||
* ```
|
||
*/
|
||
public off (type: string, callback: Function, target?: Object) {
|
||
if (type === 'lastframe') {
|
||
const nameToState = this._nameToState;
|
||
for (const name of Object.keys(nameToState)) {
|
||
const state = nameToState[name]!;
|
||
state._lastframeEventOn = false;
|
||
}
|
||
}
|
||
|
||
EventTarget.prototype.off.call(this, type, callback, target);
|
||
}
|
||
|
||
/**
|
||
* IEventTarget implementations, they will be overwrote with the same implementation in EventTarget by applyMixins
|
||
*/
|
||
public targetOff (keyOrTarget?: string | Object | undefined): void {}
|
||
public once (type: string, callback: Function, target?: Object | undefined): Function | undefined {
|
||
return;
|
||
}
|
||
public dispatchEvent (event: Event): void {}
|
||
public hasEventListener (key: string, callback?: Function | undefined, target?: Object | undefined): boolean {
|
||
return false;
|
||
}
|
||
public removeAll (keyOrTarget?: string | Object | undefined): void {}
|
||
public emit (key: string, ...args: any[]): void {}
|
||
|
||
protected _createState (clip: AnimationClip, name?: string) {
|
||
return new AnimationState(clip, name);
|
||
}
|
||
|
||
private _getStateByNameOrDefaultClip (name?: string) {
|
||
if (!name) {
|
||
if (!this._defaultClip) {
|
||
return null;
|
||
} else {
|
||
name = this._defaultClip.name;
|
||
}
|
||
}
|
||
const state = this._nameToState[name];
|
||
if (state) {
|
||
return state;
|
||
} else {
|
||
return null;
|
||
}
|
||
}
|
||
|
||
private _removeStateOfAutomaticClip (clip: AnimationClip) {
|
||
// tslint:disable-next-line:forin
|
||
for (const name in this._nameToState) {
|
||
const state = this._nameToState[name];
|
||
if (equalClips(clip, state.clip)) {
|
||
state.stop();
|
||
delete this._nameToState[name];
|
||
}
|
||
}
|
||
}
|
||
|
||
private _doCreateState (clip: AnimationClip, name: string) {
|
||
const state = this._createState(clip, name);
|
||
state._setEventTarget(this);
|
||
if (this.node) {
|
||
state.initialize(this.node);
|
||
}
|
||
this._nameToState[state.name] = state;
|
||
return state;
|
||
}
|
||
}
|
||
|
||
// for restore on and off
|
||
const {on, off} = AnimationComponent.prototype;
|
||
applyMixins(AnimationComponent, [CallbacksInvoker, EventTarget]);
|
||
// restore
|
||
AnimationComponent.prototype.on = on;
|
||
AnimationComponent.prototype.off = off;
|
||
|
||
cc.AnimationComponent = AnimationComponent;
|
||
|
||
function equalClips (clip1: AnimationClip | null, clip2: AnimationClip | null) {
|
||
if (clip1 === clip2) {
|
||
return true;
|
||
}
|
||
return !!clip1 && !!clip2 && (clip1.name === clip2.name || clip1._uuid === clip2._uuid);
|
||
}
|