mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-01 15:52:40 +08:00
618 lines
21 KiB
TypeScript
618 lines
21 KiB
TypeScript
|
||
import { AnimationAnimator, AnimationClip, AnimationState } from '../animation';
|
||
import { ccclass, executeInEditMode, executionOrder, menu, property } from '../core/data/class-decorator';
|
||
import { EventTargetFactory, IEventTargetCallback } 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 { Component } from './component';
|
||
|
||
/**
|
||
* !#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);
|
||
|
||
/**
|
||
* !#en The animation component is used to play back animations.
|
||
*
|
||
* Animation provide several events to register:
|
||
* - play : Emit when begin playing animation
|
||
* - stop : Emit when stop playing animation
|
||
* - pause : Emit when pause animation
|
||
* - resume : Emit when resume animation
|
||
* - lastframe : If animation repeat count is larger than 1, emit when animation play to the last frame
|
||
* - finished : Emit when finish playing animation
|
||
*
|
||
* !#zh Animation 组件用于播放动画。
|
||
*
|
||
* Animation 提供了一系列可注册的事件:
|
||
* - play : 开始播放时
|
||
* - stop : 停止播放时
|
||
* - pause : 暂停播放时
|
||
* - resume : 恢复播放时
|
||
* - lastframe : 假如动画循环次数大于 1,当动画播放到最后一帧时
|
||
* - finished : 动画播放完成时
|
||
*/
|
||
@ccclass('cc.AnimationComponent')
|
||
@executionOrder(99)
|
||
@executeInEditMode
|
||
@menu('Components/AnimationComponent')
|
||
export class AnimationComponent extends EventTargetFactory(Component) {
|
||
|
||
@property({ type: Boolean })
|
||
get preview () {
|
||
return this._preview;
|
||
}
|
||
|
||
set preview (value) {
|
||
this._preview = value;
|
||
if (value) {
|
||
cc.engine.animatingInEditMode = true;
|
||
this.play();
|
||
} else {
|
||
cc.engine.animatingInEditMode = false;
|
||
this.stop();
|
||
}
|
||
}
|
||
|
||
/**
|
||
* !#en Animation will play the default clip when start game.
|
||
* !#zh 在勾选自动播放或调用 play() 时默认播放的动画剪辑。
|
||
*/
|
||
@property({ type: AnimationClip })
|
||
get defaultClip () {
|
||
return this._defaultClip;
|
||
}
|
||
|
||
set defaultClip (value) {
|
||
if (!CC_EDITOR || (cc.engine && cc.engine.isPlaying)) {
|
||
return;
|
||
}
|
||
|
||
this._defaultClip = value;
|
||
|
||
if (!value) {
|
||
return;
|
||
}
|
||
|
||
const clips = this._clips;
|
||
|
||
for (let i = 0, l = clips.length; i < l; i++) {
|
||
if (equalClips(value, clips[i])) {
|
||
return;
|
||
}
|
||
}
|
||
|
||
this.addClip(value);
|
||
}
|
||
|
||
/**
|
||
* !#en Current played clip.
|
||
* !#zh 当前播放的动画剪辑。
|
||
*/
|
||
get currentClip () {
|
||
return this._currentClip;
|
||
}
|
||
|
||
set currentClip (value) {
|
||
this._currentClip = value;
|
||
}
|
||
|
||
@property({ type: [AnimationClip] })
|
||
get clips () {
|
||
return this._clips;
|
||
}
|
||
public static EventType = EventType;
|
||
|
||
/**
|
||
* !#en Whether the animation should auto play the default clip when start game.
|
||
* !#zh 是否在运行游戏后自动播放默认动画剪辑。
|
||
*/
|
||
@property
|
||
public playOnLoad = false;
|
||
private _preview = false;
|
||
|
||
private _animator: AnimationAnimator | null = null;
|
||
private _nameToState = createMap(true);
|
||
private _didInit = false;
|
||
private _currentClip: AnimationClip | null = null;
|
||
|
||
/**
|
||
* !#en All the clips used in this animation.
|
||
* !#zh 通过脚本可以访问并播放的 AnimationClip 列表。
|
||
*/
|
||
@property({ type: [AnimationClip] })
|
||
private _clips: AnimationClip[] = [];
|
||
|
||
@property
|
||
private _defaultClip: AnimationClip | null = null;
|
||
|
||
constructor () {
|
||
super();
|
||
}
|
||
|
||
public start () {
|
||
if (!CC_EDITOR && this.playOnLoad && this._defaultClip) {
|
||
const isPlaying = this._animator && this._animator.isPlaying;
|
||
if (!isPlaying) {
|
||
const state = this.getAnimationState(this._defaultClip.name);
|
||
this._animator!.playState(state);
|
||
}
|
||
}
|
||
}
|
||
|
||
public onEnable () {
|
||
if (this._animator) {
|
||
this._animator.resume();
|
||
}
|
||
}
|
||
|
||
public onDisable () {
|
||
if (this._animator) {
|
||
this._animator.pause();
|
||
}
|
||
}
|
||
|
||
public onDestroy () {
|
||
this.stop();
|
||
}
|
||
|
||
/**
|
||
* !#en Get all the clips used in this animation.
|
||
* !#zh 获取动画组件上的所有动画剪辑。
|
||
*/
|
||
public getClips () {
|
||
return this._clips;
|
||
}
|
||
|
||
/**
|
||
* !#en Plays an animation and stop other animations.
|
||
* !#zh 播放指定的动画,并且停止当前正在播放动画。如果没有指定动画,则播放默认动画。
|
||
* @param [name] - The name of animation to play. If no name is supplied then the default animation will be played.
|
||
* @param [startTime] - play an animation from startTime
|
||
* @return The AnimationState of playing animation. In cases where the animation can't be played
|
||
* (ie, there is no default animation or no animation with the specified name), the function will return null.
|
||
* @example
|
||
* var animCtrl = this.node.getComponent(cc.Animation);
|
||
* animCtrl.play("linear");
|
||
*/
|
||
public play (name?: string, startTime?: number) {
|
||
const state = this.playAdditive(name, startTime);
|
||
this._animator!.stopStatesExcept(state);
|
||
return state;
|
||
}
|
||
|
||
/**
|
||
* !#en
|
||
* Plays an additive animation, it will not stop other animations.
|
||
* If there are other animations playing, then will play several animations at the same time.
|
||
* !#zh 播放指定的动画(将不会停止当前播放的动画)。如果没有指定动画,则播放默认动画。
|
||
* @param [name] - The name of animation to play. If no name is supplied then the default animation will be played.
|
||
* @param [startTime] - play an animation from startTime
|
||
* @return The AnimationState of playing animation. In cases where the animation can't be played
|
||
* (ie, there is no default animation or no animation with the specified name), the function will return null.
|
||
* @example
|
||
* // linear_1 and linear_2 at the same time playing.
|
||
* var animCtrl = this.node.getComponent(cc.Animation);
|
||
* animCtrl.playAdditive("linear_1");
|
||
* animCtrl.playAdditive("linear_2");
|
||
*/
|
||
public playAdditive (name?: string, startTime?: number) {
|
||
this._init();
|
||
const state = this.getAnimationState(name || (this._defaultClip && this._defaultClip.name) || '');
|
||
|
||
if (state) {
|
||
this.enabled = true;
|
||
|
||
const animator = this._animator;
|
||
if (animator!.isPlaying && state.isPlaying) {
|
||
if (state.isPaused) {
|
||
animator!.resumeState(state);
|
||
}
|
||
else {
|
||
animator!.stopState(state);
|
||
animator!.playState(state, startTime);
|
||
}
|
||
}
|
||
else {
|
||
animator!.playState(state, startTime);
|
||
}
|
||
|
||
// Animation cannot be played when the component is not enabledInHierarchy.
|
||
// That would cause an error for the animation lost the reference after destroying the node.
|
||
// If users play the animation when the component is not enabledInHierarchy,
|
||
// we pause the animator here so that it will automatically resume the animation when users enable the component.
|
||
if (!this.enabledInHierarchy) {
|
||
animator!.pause();
|
||
}
|
||
|
||
this.currentClip = state.clip;
|
||
}
|
||
return state;
|
||
}
|
||
|
||
/**
|
||
* !#en Stops an animation named name. If no name is supplied then stops all playing animations that were started with this Animation. <br/>
|
||
* Stopping an animation also Rewinds it to the Start.
|
||
* !#zh 停止指定的动画。如果没有指定名字,则停止当前正在播放的动画。
|
||
* @param [name] - The animation to stop, if not supplied then stops all playing animations.
|
||
*/
|
||
public stop (name?: string) {
|
||
if (!this._didInit) {
|
||
return;
|
||
}
|
||
if (name) {
|
||
const state = this._nameToState[name];
|
||
if (state) {
|
||
this._animator!.stopState(state);
|
||
}
|
||
}
|
||
else {
|
||
this._animator!.stop();
|
||
}
|
||
}
|
||
|
||
/**
|
||
* !#en Pauses an animation named name. If no name is supplied then pauses all playing animations that were started with this Animation.
|
||
* !#zh 暂停当前或者指定的动画。如果没有指定名字,则暂停当前正在播放的动画。
|
||
* @param [name] - The animation to pauses, if not supplied then pauses all playing animations.
|
||
*/
|
||
public pause (name?: string) {
|
||
if (!this._didInit) {
|
||
return;
|
||
}
|
||
if (name) {
|
||
const state = this._nameToState[name];
|
||
if (state) {
|
||
this._animator!.pauseState(state);
|
||
}
|
||
}
|
||
else {
|
||
this.enabled = false;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* !#en Resumes an animation named name. If no name is supplied then resumes all paused animations that were started with this Animation.
|
||
* !#zh 重新播放指定的动画,如果没有指定名字,则重新播放当前正在播放的动画。
|
||
* @method resume
|
||
* @param {String} [name] - The animation to resumes, if not supplied then resumes all paused animations.
|
||
*/
|
||
public resume (name?: string) {
|
||
if (!this._didInit) {
|
||
return;
|
||
}
|
||
if (name) {
|
||
const state = this._nameToState[name];
|
||
if (state) {
|
||
this._animator!.resumeState(state);
|
||
}
|
||
}
|
||
else {
|
||
this.enabled = true;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* !#en Make an animation named name go to the specified time. If no name is supplied then make all animations go to the specified time.
|
||
* !#zh 设置指定动画的播放时间。如果没有指定名字,则设置当前播放动画的播放时间。
|
||
* @param [time] - The time to go to
|
||
* @param [name] - Specified animation name, if not supplied then make all animations go to the time.
|
||
*/
|
||
public setCurrentTime (time: number, name?: string) {
|
||
this._init();
|
||
if (name) {
|
||
const state = this._nameToState[name];
|
||
if (state) {
|
||
this._animator!.setStateTime(state, time);
|
||
}
|
||
}
|
||
else {
|
||
this._animator!.setStateTime(time);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* !#en Returns the animation state named name. If no animation with the specified name, the function will return null.
|
||
* !#zh 获取当前或者指定的动画状态,如果未找到指定动画剪辑则返回 null。
|
||
*/
|
||
public getAnimationState (name: string) {
|
||
this._init();
|
||
let state = this._nameToState[name];
|
||
|
||
if (CC_EDITOR && (!state || !ArrayUtils.contains(this._clips, state.clip))) {
|
||
this._didInit = false;
|
||
|
||
if (this._animator) {
|
||
this._animator!.stop();
|
||
}
|
||
|
||
this._init();
|
||
state = this._nameToState[name];
|
||
}
|
||
|
||
if (state && !state.curveLoaded) {
|
||
this._animator!._reloadClip(state);
|
||
}
|
||
|
||
return state || null;
|
||
}
|
||
|
||
/**
|
||
* !#en Adds a clip to the animation with name newName. If a clip with that name already exists it will be replaced with the new clip.
|
||
* !#zh 添加动画剪辑,并且可以重新设置该动画剪辑的名称。
|
||
* @param clip the clip to add
|
||
* @return The AnimationState which gives full control over the animation clip.
|
||
*/
|
||
public addClip (clip: AnimationClip, newName?: string): AnimationState | undefined {
|
||
if (!clip) {
|
||
warnID(3900);
|
||
return;
|
||
}
|
||
this._init();
|
||
|
||
// add clip
|
||
if (!ArrayUtils.contains(this._clips, clip)) {
|
||
this._clips.push(clip);
|
||
}
|
||
|
||
// replace same name clip
|
||
newName = newName || clip.name;
|
||
const oldState = this._nameToState[newName];
|
||
if (oldState) {
|
||
if (oldState.clip === clip) {
|
||
return oldState;
|
||
}
|
||
else {
|
||
const index = this._clips.indexOf(oldState.clip);
|
||
if (index !== -1) {
|
||
this._clips.splice(index, 1);
|
||
}
|
||
}
|
||
}
|
||
|
||
// replace state
|
||
const newState = new AnimationState(clip, newName);
|
||
this._nameToState[newName] = newState;
|
||
return newState;
|
||
}
|
||
|
||
/**
|
||
* !#en
|
||
* Remove clip from the animation list. This will remove the clip and any animation states based on it.
|
||
* If there are animation states depand on the clip are playing or clip is defaultClip, it will not delete the clip.
|
||
* 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 的。
|
||
* 但是如果 force 参数为 true,则会强制停止该动画,然后移除该动画剪辑和相关的动画。这时候如果 clip 是 defaultClip,defaultClip 将会被重置为 null。
|
||
* @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) {
|
||
if (!clip) {
|
||
warnID(3901);
|
||
return;
|
||
}
|
||
this._init();
|
||
|
||
let state;
|
||
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) { this.stop(state.name); }
|
||
else {
|
||
if (!CC_TEST) { warnID(3903); }
|
||
return;
|
||
}
|
||
}
|
||
|
||
this._clips = this._clips.filter((item) => item !== clip);
|
||
|
||
if (state) {
|
||
delete this._nameToState[state.name];
|
||
}
|
||
}
|
||
|
||
/**
|
||
* !#en
|
||
* Samples animations at the current state.<br/>
|
||
* This is useful when you explicitly want to set up some animation state, and sample it once.
|
||
* !#zh 对指定或当前动画进行采样。你可以手动将动画设置到某一个状态,然后采样一次。
|
||
*/
|
||
public sample (name: string) {
|
||
this._init();
|
||
|
||
if (name) {
|
||
const state = this._nameToState[name];
|
||
if (state) {
|
||
state.sample();
|
||
}
|
||
}
|
||
else {
|
||
this._animator!.sample();
|
||
}
|
||
}
|
||
|
||
/**
|
||
* !#en
|
||
* Register animation event callback.
|
||
* The event arguments will provide the AnimationState which emit the event.
|
||
* When play an animation, will auto register the event callback to the AnimationState,
|
||
* and unregister the event callback from the AnimationState when animation stopped.
|
||
* !#zh
|
||
* 注册动画事件回调。
|
||
* 回调的事件里将会附上发送事件的 AnimationState。
|
||
* 当播放一个动画时,会自动将事件注册到对应的 AnimationState 上,停止播放时会将事件从这个 AnimationState 上取消注册。
|
||
* @param type - A string representing the event type to listen for.
|
||
* @param callback - The callback that will be invoked when the event is dispatched.
|
||
* The callback is ignored if it is a duplicate (the callbacks are unique).
|
||
* @param [target] - The target (this object) to invoke the callback, can be null
|
||
* @param [useCapture=false] - When set to true, the capture argument prevents callback
|
||
* from being invoked when the event's eventPhase attribute value is BUBBLING_PHASE.
|
||
* When false, callback will NOT be invoked when event's eventPhase attribute value is CAPTURING_PHASE.
|
||
* Either way, callback will be invoked when event's eventPhase attribute value is AT_TARGET.
|
||
*
|
||
* @return Just returns the incoming callback so you can save the anonymous function easier.
|
||
* @typescript
|
||
* on(type: string, callback: (event: Event.EventCustom) => void, target?: any, useCapture?: boolean): (event: Event.EventCustom) => void
|
||
* on<T>(type: string, callback: (event: T) => void, target?: any, useCapture?: boolean): (event: T) => void
|
||
* @example
|
||
* 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 | null, useCapture?: boolean) {
|
||
this._init();
|
||
|
||
const ret = super.on(type, callback, target, useCapture);
|
||
|
||
if (type === 'lastframe') {
|
||
for (const state of this._animator!._anims.array) {
|
||
state._lastframeEventOn = true;
|
||
}
|
||
}
|
||
|
||
return ret;
|
||
}
|
||
|
||
/**
|
||
* !#en
|
||
* Unregister animation event callback.
|
||
* !#zh
|
||
* 取消注册动画事件回调。
|
||
* @method off
|
||
* @param {String} type - A string representing the event type being removed.
|
||
* @param {Function} [callback] - The callback to remove.
|
||
* @param {Object} [target] - The target (this object) to invoke the callback, if it's not given, only callback without target will be removed
|
||
* @param {Boolean} [useCapture=false] - Specifies whether the callback being removed was registered as a capturing callback or not.
|
||
* If not specified, useCapture defaults to false. If a callback was registered twice,
|
||
* one with capture and one without, each must be removed separately. Removal of a capturing callback
|
||
* does not affect a non-capturing version of the same listener, and vice versa.
|
||
*
|
||
* @example
|
||
* // unregister event to all animation
|
||
* animation.off('play', this.onPlay, this);
|
||
*/
|
||
public off (type: string, callback: IEventTargetCallback, target?: Object | null, useCapture?: boolean) {
|
||
this._init();
|
||
|
||
if (type === 'lastframe') {
|
||
const nameToState = this._nameToState;
|
||
for (const name of Object.keys(nameToState)) {
|
||
const state = nameToState[name];
|
||
state._lastframeEventOn = false;
|
||
}
|
||
}
|
||
|
||
super.off(type, callback, target, useCapture);
|
||
}
|
||
|
||
///////////////////////////////////////////////////////////////////////////////
|
||
// Internal Methods
|
||
///////////////////////////////////////////////////////////////////////////////
|
||
|
||
// Dont forget to call _init before every actual process in public methods.
|
||
// Just invoking _init by onLoad is not enough because onLoad is called only if the entity is active.
|
||
|
||
private _init () {
|
||
if (this._didInit) {
|
||
return;
|
||
}
|
||
this._didInit = true;
|
||
this._animator = new AnimationAnimator(this.node, this);
|
||
this._createStates();
|
||
}
|
||
|
||
private _createStates () {
|
||
this._nameToState = createMap(true);
|
||
|
||
// create animation states
|
||
let state: AnimationState | null = null;
|
||
let defaultClipState: AnimationState | undefined;
|
||
for (const clip of this._clips) {
|
||
if (clip) {
|
||
state = new AnimationState(clip);
|
||
|
||
if (CC_EDITOR) {
|
||
this._animator!._reloadClip(state);
|
||
}
|
||
|
||
this._nameToState[state.name] = state;
|
||
if (equalClips(this._defaultClip, clip)) {
|
||
defaultClipState = state;
|
||
}
|
||
}
|
||
}
|
||
if (this._defaultClip && !defaultClipState) {
|
||
state = new AnimationState(this._defaultClip);
|
||
|
||
if (CC_EDITOR) {
|
||
this._animator!._reloadClip(state);
|
||
}
|
||
|
||
this._nameToState[state.name] = state;
|
||
}
|
||
}
|
||
}
|
||
|
||
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);
|
||
}
|