diff --git a/cocos/animation/animation-animator.ts b/cocos/animation/animation-animator.ts deleted file mode 100644 index 6147ddca0d..0000000000 --- a/cocos/animation/animation-animator.ts +++ /dev/null @@ -1,180 +0,0 @@ -import { AnimationComponent } from '../components/animation-component'; -import { binarySearchEpsilon as binarySearch } from '../core/data/utils/binary-search'; -import { errorID } from '../core/platform/CCDebug'; -import { MutableForwardIterator } from '../core/utils/array'; -import { Node } from '../scene-graph'; -import { AnimationBlendState } from './animation-blend-state'; -import { EventAnimCurve, EventInfo } from './animation-curve'; -import { AnimationState } from './animation-state'; -import { Playable } from './playable'; -import { WrapModeMask } from './types'; - -/** - * The actual animator for Animation Component. - */ -export class AnimationAnimator extends Playable { - public _anims = new MutableForwardIterator([]); - - constructor (public target: Node, public animation: AnimationComponent) { - super(); - } - - public playState (state: AnimationState, startTime?: number) { - if (!state.clip) { - return; - } - - if (!state.curveLoaded) { - state.initialize(this.target); - } - - state.animator = this; - state.play(); - - if (typeof startTime === 'number') { - state.setTime(startTime); - } - - this.play(); - } - - public stopStatesExcept (state: AnimationState) { - const iterator = this._anims; - const array = iterator.array; - for (iterator.i = 0; iterator.i < array.length; ++iterator.i) { - const anim = array[iterator.i]; - if (anim === state) { - continue; - } - - this.stopState(anim); - } - } - - public addAnimation (anim: AnimationState) { - const index = this._anims.array.indexOf(anim); - if (index === -1) { - this._anims.push(anim); - } - - anim._setEventTarget(this.animation); - } - - public removeAnimation (anim: AnimationState) { - const index = this._anims.array.indexOf(anim); - if (index >= 0) { - this._anims.fastRemoveAt(index); - - if (this._anims.array.length === 0) { - this.stop(); - } - } - else { - errorID(3908); - } - - anim.animator = null; - } - - public sample () { - const iterator = this._anims; - const array = iterator.array; - for (iterator.i = 0; iterator.i < array.length; ++iterator.i) { - const anim = array[iterator.i]; - anim.sample(); - } - } - - public stopState (state?: AnimationState) { - if (state) { - state.stop(); - } - } - - public pauseState (state?: AnimationState) { - if (state) { - state.pause(); - } - } - - public resumeState (state?: AnimationState) { - if (state) { - state.resume(); - } - - if (this.isPaused) { - this.resume(); - } - } - - public setStateTime (state: AnimationState, time: number): void; - - public setStateTime (time: number): void; - - public setStateTime (state: AnimationState | number, time?: number) { - if (time !== undefined) { - (state as AnimationState).setTime(time); - (state as AnimationState).sample(); - } - else { - for (const anim of this._anims.array) { - anim.setTime(state as number); - anim.sample(); - } - } - } - - public onStop () { - const iterator = this._anims; - const array = iterator.array; - for (iterator.i = 0; iterator.i < array.length; ++iterator.i) { - const anim = array[iterator.i]; - anim.stop(); - } - } - - public onPause () { - for (const anim of this._anims.array) { - anim.pause(); - // need to unbind animator to anim, or it maybe cannot be gc. - anim.animator = null; - } - } - - public onResume () { - for (const anim of this._anims.array) { - // rebind animator to anim - anim.animator = this; - - anim.resume(); - } - } - - public _reloadClip (state: AnimationState) { - state.initialize(this.target); - } -} - -// 这个方法应该是 SampledAnimCurve 才能用 -function createBatchedProperty (propPath, firstDotIndex, mainValue, animValue) { - mainValue = mainValue.clone(); - let nextValue = mainValue; - let leftIndex = firstDotIndex + 1; - let rightIndex = propPath.indexOf('.', leftIndex); - - // scan property path - while (rightIndex !== -1) { - const nextName = propPath.slice(leftIndex, rightIndex); - nextValue = nextValue[nextName]; - leftIndex = rightIndex + 1; - rightIndex = propPath.indexOf('.', leftIndex); - } - const lastPropName = propPath.slice(leftIndex); - nextValue[lastPropName] = animValue; - - return mainValue; -} - -if (CC_TEST) { - cc._Test.createBatchedProperty = createBatchedProperty; -} diff --git a/cocos/animation/animation-manager.ts b/cocos/animation/animation-manager.ts index 9e17433a77..10d13cc976 100644 --- a/cocos/animation/animation-manager.ts +++ b/cocos/animation/animation-manager.ts @@ -1,6 +1,6 @@ import { ccclass } from '../core/data/class-decorator'; import { errorID } from '../core/platform/CCDebug'; -import { MutableForwardIterator } from '../core/utils/array'; +import { MutableForwardIterator, remove } from '../core/utils/array'; import { Node } from '../scene-graph'; import { AnimationBlendState } from './animation-blend-state'; import { AnimationState } from './animation-state'; @@ -27,6 +27,10 @@ export class AnimationManager { this._crossFades.push(crossFade); } + public removeCrossFade (crossFade: CrossFade) { + remove(this._crossFades, crossFade); + } + public update (dt: number) { for (const crossFade of this._crossFades) { crossFade.update(dt); diff --git a/cocos/animation/animation-state.ts b/cocos/animation/animation-state.ts index 6d37159de3..fa339a7ebf 100644 --- a/cocos/animation/animation-state.ts +++ b/cocos/animation/animation-state.ts @@ -1,6 +1,5 @@ import { binarySearchEpsilon as binarySearch } from '../core/data/utils/binary-search'; import { Node } from '../scene-graph'; -import { AnimationAnimator } from './animation-animator'; import { AnimationBlendState } from './animation-blend-state'; import { AnimationClip } from './animation-clip'; import { AnimCurve, EventAnimCurve, EventInfo } from './animation-curve'; @@ -120,7 +119,7 @@ export class AnimationState extends Playable { set delay (value: number) { this._delayTime = this._delay = value; } - public animator: AnimationAnimator | null = null; + /** * !#en The curves list. * !#zh 曲线列表。 @@ -487,10 +486,6 @@ export class AnimationState extends Playable { cc.director.getAnimationManager().addAnimation(this); - if (this.animator) { - this.animator.addAnimation(this); - } - this.emit('play', this); } @@ -499,10 +494,6 @@ export class AnimationState extends Playable { cc.director.getAnimationManager().removeAnimation(this); } - if (this.animator) { - this.animator.removeAnimation(this); - } - this.emit('stop', this); } diff --git a/cocos/animation/cross-fade.ts b/cocos/animation/cross-fade.ts index 82f8fd482e..338e7e4d64 100644 --- a/cocos/animation/cross-fade.ts +++ b/cocos/animation/cross-fade.ts @@ -15,22 +15,20 @@ export class CrossFade extends Playable { constructor (public target: Node) { super(); - this._fadings.push({ - state: null, - easeDuration: Number.POSITIVE_INFINITY, - easeTime: 0, - }); + this._unshiftDefault(); } public update (deltaTime: number) { + if (!this.isPlaying || this.isPaused) { + return; + } let absoluteWeight = 1.0; for (let i = 0; i < this._fadings.length; ++i) { if (absoluteWeight === 0) { for (let j = i; j < this._fadings.length; ++j) { const fading = this._fadings[j]; if (fading.state) { - fading.state.weight = 0; - fading.state.stop(); + this._directStopState(fading.state); } } this._fadings.splice(i); @@ -65,6 +63,48 @@ export class CrossFade extends Playable { } } + public onPause () { + super.onPause(); + for (const fading of this._fadings) { + if (fading.state) { + fading.state.pause(); + } + } + } + + public onResume () { + super.onResume(); + for (const fading of this._fadings) { + if (fading.state) { + fading.state.resume(); + } + } + } + + public onStop () { + super.onStop(); + for (const fading of this._fadings) { + if (fading.state) { + this._directStopState(fading.state); + } + } + this._fadings.length = 0; + this._unshiftDefault(); + } + + private _unshiftDefault () { + this._fadings.unshift({ + state: null, + easeDuration: Number.POSITIVE_INFINITY, + easeTime: 0, + }); + } + + private _directStopState (state: AnimationState) { + state.weight = 0; + state.stop(); + } + private _directPlayState (state: AnimationState) { if (!state.clip) { return; diff --git a/cocos/animation/index.ts b/cocos/animation/index.ts index 244b1893c0..6c660dd93f 100644 --- a/cocos/animation/index.ts +++ b/cocos/animation/index.ts @@ -7,4 +7,3 @@ export * from './animation-curve'; export * from './animation-clip'; export * from './animation-manager'; export * from './animation-state'; -export * from './animation-animator'; diff --git a/cocos/components/animation-component.ts b/cocos/components/animation-component.ts index 611a9692b7..94123526da 100644 --- a/cocos/components/animation-component.ts +++ b/cocos/components/animation-component.ts @@ -1,5 +1,5 @@ -import { AnimationAnimator, AnimationClip, AnimationState } from '../animation'; +import { AnimationClip, AnimationState } from '../animation'; import { CrossFade } from '../animation/cross-fade'; import { ccclass, executeInEditMode, executionOrder, menu, property } from '../core/data/class-decorator'; import { EventTargetFactory, IEventTargetCallback } from '../core/event/event-target-factory'; @@ -81,12 +81,16 @@ export class AnimationComponent extends EventTargetFactory(Component) { set preview (value) { this._preview = value; + if (!this._defaultClip) { + return; + } if (value) { cc.engine.animatingInEditMode = true; - this.play(); + this.crossFade(this._defaultClip.name); + this._crossFade!.resume(); } else { cc.engine.animatingInEditMode = false; - this.stop(); + this._crossFade!.pause(); } } @@ -152,9 +156,8 @@ export class AnimationComponent extends EventTargetFactory(Component) { public playOnLoad = false; private _preview = false; - private _animator: AnimationAnimator | null = null; private _crossFade: CrossFade | null = null; - private _nameToState: Partial<{ [name: string]: AnimationState; }> = createMap(true); + private _nameToState: { [name: string]: AnimationState; } = createMap(true); private _didInit = false; private _currentClip: AnimationClip | null = null; @@ -163,7 +166,7 @@ export class AnimationComponent extends EventTargetFactory(Component) { * !#zh 通过脚本可以访问并播放的 AnimationClip 列表。 */ @property({ type: [AnimationClip] }) - private _clips: AnimationClip[] = []; + private _clips: Array<(AnimationClip | null)> = []; @property private _defaultClip: AnimationClip | null = null; @@ -172,37 +175,33 @@ export class AnimationComponent extends EventTargetFactory(Component) { super(); } - public update (deltaTime: number) { - if (this._animator) { - this._animator.update(deltaTime); - } - } - public start () { + this._init(); + this._startCrossFade(); 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); - this._crossFade.crossFade(state, 0); - } + this.crossFade(this._defaultClip.name, 0); } } public onEnable () { - if (this._animator) { - this._animator.resume(); + if (this._crossFade) { + this._crossFade.resume(); } } public onDisable () { - if (this._animator) { - this._animator.pause(); + if (this._crossFade) { + this._crossFade.pause(); } } public onDestroy () { - this.stop(); + if (this._crossFade) { + this._crossFade.stop(); + cc.director.getAnimationManager().removeCrossFade(this._crossFade); + this._crossFade.stop(); + this._crossFade = null; + } } /** @@ -224,146 +223,18 @@ export class AnimationComponent extends EventTargetFactory(Component) { * 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; - } - - public crossFade (name: string, duration = 0.3) { - const state = this.getAnimationState(name || (this._defaultClip && this._defaultClip.name) || ''); - this._crossFade.crossFade(state, duration); - 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) || ''); - + public play (name?: string, startTime = 0) { + const state = this.crossFade(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; + state.time = startTime; } return state; } - /** - * !#en Stops an animation named name. If no name is supplied then stops all playing animations that were started with this Animation.
- * 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); - } + public crossFade (name?: string, duration = 0.3) { + const state = this.getAnimationState(name || (this._defaultClip && this._defaultClip.name) || ''); + this._crossFade!.crossFade(state, duration); + return state; } /** @@ -372,23 +243,10 @@ export class AnimationComponent extends EventTargetFactory(Component) { */ 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]; - } - + const state = this._nameToState[name]; if (state && !state.curveLoaded) { - this._animator!._reloadClip(state); + state.initialize(this.node); } - return state || null; } @@ -449,7 +307,7 @@ export class AnimationComponent extends EventTargetFactory(Component) { } this._init(); - let state; + let state: AnimationState | undefined; for (const name of Object.keys(this._nameToState)) { state = this._nameToState[name]; const stateClip = state.clip; @@ -467,7 +325,7 @@ export class AnimationComponent extends EventTargetFactory(Component) { } if (state && state.isPlaying) { - if (force) { this.stop(state.name); } + if (force) { state.stop(); } else { if (!CC_TEST) { warnID(3903); } return; @@ -481,26 +339,6 @@ export class AnimationComponent extends EventTargetFactory(Component) { } } - /** - * !#en - * Samples animations at the current state.
- * 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. @@ -534,15 +372,12 @@ export class AnimationComponent extends EventTargetFactory(Component) { */ 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; + for (const stateName of Object.keys(this._nameToState)) { + this._nameToState[stateName]!._lastframeEventOn = true; } } - return ret; } @@ -590,42 +425,41 @@ export class AnimationComponent extends EventTargetFactory(Component) { return; } this._didInit = true; - this._animator = new AnimationAnimator(this.node, this); - this._createStates(); this._crossFade = new CrossFade(this.node); - cc.director.getAnimationManager().addCrossFade(this._crossFade); + this._createStates(); + } + + private _startCrossFade () { + cc.director.getAnimationManager().addCrossFade(this._crossFade!); + this._crossFade!.play(); } private _createStates () { this._nameToState = createMap(true); - - // create animation states - let state: AnimationState | null = null; - let defaultClipState: AnimationState | undefined; + // Create animation states. for (const clip of this._clips) { if (clip) { - state = new AnimationState(clip); - + const state = this._createState(clip); if (CC_EDITOR) { - this._animator!._reloadClip(state); - } - - this._nameToState[state.name] = state; - if (equalClips(this._defaultClip, clip)) { - defaultClipState = state; + state.initialize(this.node); } } } - if (this._defaultClip && !defaultClipState) { - state = new AnimationState(this._defaultClip); - + const isDefaultClipBounded = this._clips.findIndex((clip) => equalClips(clip, this._defaultClip)) >= 0; + if (this._defaultClip && !isDefaultClipBounded) { + const state = this._createState(this._defaultClip); if (CC_EDITOR) { - this._animator!._reloadClip(state); + state.initialize(this.node); } - - this._nameToState[state.name] = state; } } + + private _createState (clip: AnimationClip, name?: string) { + const state = new AnimationState(clip, name); + state._setEventTarget(this); + this._nameToState[state.name] = state; + return state; + } } cc.AnimationComponent = AnimationComponent; @@ -634,6 +468,5 @@ function equalClips (clip1: AnimationClip | null, clip2: AnimationClip | null) { if (clip1 === clip2) { return true; } - - return clip1 && clip2 && (clip1.name === clip2.name || clip1._uuid === clip2._uuid); + return !!clip1 && !!clip2 && (clip1.name === clip2.name || clip1._uuid === clip2._uuid); }