Beispiel #1
0
    ///////////////////////////////////////////////////////////////////////////////
    // Internal Functions
    ///////////////////////////////////////////////////////////////////////////////

    // ------------------------------------------------------------------
    // Desc:
    // ------------------------------------------------------------------

    void Init()
    {
        bool initialized = (nameToState != null);

        if (initialized == false)
        {
            sprite_            = GetComponent <exSprite>();
            defaultTextureInfo = sprite_.textureInfo;

            nameToState = new Dictionary <string, exSpriteAnimationState>();
            for (int i = 0; i < animations.Count; ++i)
            {
                exSpriteAnimationClip clip = animations[i];
                if (clip != null)
                {
                    exSpriteAnimationState state = new exSpriteAnimationState(clip);
                    nameToState[state.name] = state;
                    if (ReferenceEquals(defaultAnimation, clip))
                    {
                        curAnimation   = state;
                        lastFrameIndex = -1;
                    }
                }
            }
            exDebug.Assert(defaultAnimation == null || defaultAnimation == nameToState[defaultAnimation.name].clip);
        }
    }
Beispiel #2
0
    // ------------------------------------------------------------------
    /// Stop the playing animation, take the action that setup in the
    /// exSpriteAnimState.stopAction
    // ------------------------------------------------------------------

    public void Stop(exSpriteAnimationState _animState)
    {
        if (_animState != null)
        {
            if (ReferenceEquals(_animState, curAnimation))
            {
                curAnimation = null;
            }
            _animState.time = 0.0f;

            exSpriteAnimationClip.StopAction stopAction = _animState.stopAction;
            switch (stopAction)
            {
            case exSpriteAnimationClip.StopAction.DoNothing:
                break;

            case exSpriteAnimationClip.StopAction.DefaultSprite:
                sprite_.textureInfo = defaultTextureInfo;
                break;

            case exSpriteAnimationClip.StopAction.Hide:
                sprite_.enabled = false;
                break;

            case exSpriteAnimationClip.StopAction.Destroy:
                Object.Destroy(gameObject);
                break;
            }
        }
#if AUTO_DISABLE
        enabled = false;
#endif
    }
Beispiel #3
0
    // ------------------------------------------------------------------
    // Desc:
    // ------------------------------------------------------------------

    public void Play(string _name, float _time)
    {
        exSpriteAnimationState anim = GetAnimation(_name);

        if (anim != null)
        {
            Play(anim, _time);
        }
    }
Beispiel #4
0
    // ------------------------------------------------------------------
    /// \param _name the name of the animation to play
    /// \param _frame the frame count
    /// Play the animation by _name, start from the _frame
    // ------------------------------------------------------------------

    public void PlayByFrame(string _name, int _frame)
    {
        exSpriteAnimationState anim = GetAnimation(_name);

        if (anim != null)
        {
            float unitSeconds = 1.0f / anim.clip.frameRate;
            float time        = _frame * unitSeconds;
            Play(anim, time);
        }
    }
Beispiel #5
0
    // ------------------------------------------------------------------
    /// \param _animState the animation state to sample
    /// Samples animations at the current state.
    /// This is useful when you explicitly want to set up some animation state, and sample it once.
    // ------------------------------------------------------------------

    public void Sample(exSpriteAnimationState _animState)
    {
        if (_animState != null)
        {
            if (curAnimation != _animState)
            {
                curAnimation   = _animState;
                lastFrameIndex = -1;
            }
            Sample();
        }
    }
Beispiel #6
0
    // ------------------------------------------------------------------
    /// advance the time and check if we trigger any animation events
    // ------------------------------------------------------------------

    public void Step(exSpriteAnimationState _animState, float _deltaTime)
    {
        if (_animState != null)
        {
            if (curAnimation != _animState)
            {
                curAnimation   = _animState;
                lastFrameIndex = -1;
            }
            Step(_deltaTime);
        }
    }
Beispiel #7
0
    // ------------------------------------------------------------------
    /// \param _animState the animation state to play
    /// \param _time the time to play
    /// Play the animation by _animState, start from the _index of frame
    // ------------------------------------------------------------------

    private void Play(exSpriteAnimationState _animState, float _time)
    {
        curAnimation = _animState;
        if (curAnimation != null)
        {
            curIndex          = -1;
            curAnimation.time = _time;
            playStartFrame    = Time.frameCount;
            Sample();
#if AUTO_DISABLE
            enabled = true;
#endif
        }
    }
Beispiel #8
0
    // ------------------------------------------------------------------
    /// \param _name the name of animation state you want to add
    /// \param _animClip the sprite animation clip wants to add
    /// \return the instantiate animation state of the added _animClip
    /// Add a sprite animation clip, create a new animation state and saves
    /// it to the lookup table by the name of the clip
    ///
    /// \note if the animation already in the exSpriteAnimation.animations,
    /// it will override the old clip and return a new animation state.
    // ------------------------------------------------------------------

    public exSpriteAnimationState AddAnimation(string _name, exSpriteAnimationClip _animClip)
    {
        Init();

        exSpriteAnimationState oldState = null;

        if (nameToState.TryGetValue(_name, out oldState))
        {
            if (ReferenceEquals(oldState.clip, _animClip))
            {
                // if we already have the same name, just return the state
                return(oldState);
            }
            else
            {
                // override the old clip
                animations[animations.IndexOf(oldState.clip)] = _animClip;
            }
        }
        else if (animations.IndexOf(_animClip) != -1)
        {
            // we already have the animation
            oldState = nameToState[_name];
            if (ReferenceEquals(oldState.clip, _animClip))
            {
                return(oldState);
            }
        }
        else
        {
            animations.Add(_animClip);
        }

        exSpriteAnimationState newState = new exSpriteAnimationState(_name, _animClip);

        nameToState[_name] = newState;
        return(newState);
    }
    // ------------------------------------------------------------------ 
    /// \param _animState the animation state to play
    /// \param _time the time to play
    /// Play the animation by _animState, start from the _index of frame  
    // ------------------------------------------------------------------ 

    private void Play (exSpriteAnimationState _animState, float _time) {
        curAnimation = _animState;
        if (curAnimation != null) {
            curIndex = -1;
            curAnimation.time = _time;
            playStartFrame = Time.frameCount;
            Sample();
#if AUTO_DISABLE
            enabled = true;
#endif
        }
    }
Beispiel #10
0
    ///////////////////////////////////////////////////////////////////////////////
    // Internal Functions
    ///////////////////////////////////////////////////////////////////////////////
        
    // ------------------------------------------------------------------ 
    // Desc: 
    // ------------------------------------------------------------------ 

    void Init () {
        bool initialized = (nameToState != null);
        if (initialized == false) {
            sprite_ = GetComponent<exSprite>();
            defaultTextureInfo = sprite_.textureInfo;

            nameToState = new Dictionary<string, exSpriteAnimationState>();
            for (int i = 0; i < animations.Count; ++i) {
                exSpriteAnimationClip clip = animations[i];
                if (clip != null) {
                    exSpriteAnimationState state = new exSpriteAnimationState(clip);
                    nameToState[state.name] = state;
                    if (ReferenceEquals(defaultAnimation, clip)) {
                        curAnimation = state;
                        lastFrameIndex = -1;
                    }
                }
            }
            exDebug.Assert(defaultAnimation == null || defaultAnimation == nameToState[defaultAnimation.name].clip);
        }
    }
Beispiel #11
0
    // ------------------------------------------------------------------ 
    /// advance the time and check if we trigger any animation events
    // ------------------------------------------------------------------ 

    public void Step (exSpriteAnimationState _animState, float _deltaTime) {
        if (_animState != null) {
            if (curAnimation != _animState) {
                curAnimation = _animState;
                lastFrameIndex = -1;
            }
            Step(_deltaTime);
        }
    }
Beispiel #12
0
    // ------------------------------------------------------------------ 
    /// \param _animState the animation state to sample
    /// Samples animations at the current state.
    /// This is useful when you explicitly want to set up some animation state, and sample it once.
    // ------------------------------------------------------------------ 

    public void Sample (exSpriteAnimationState _animState) {
        if (_animState != null) {
            if (curAnimation != _animState) {
                curAnimation = _animState;
                lastFrameIndex = -1;
            }
            Sample();
        }
    }
Beispiel #13
0
    // ------------------------------------------------------------------ 
    /// \param _name the name of animation state you want to add
    /// \param _animClip the sprite animation clip wants to add
    /// \return the instantiate animation state of the added _animClip 
    /// Add a sprite animation clip, create a new animation state and saves 
    /// it to the lookup table by the name of the clip
    /// 
    /// \note if the animation already in the exSpriteAnimation.animations, 
    /// it will override the old clip and return a new animation state.
    // ------------------------------------------------------------------ 

    public exSpriteAnimationState AddAnimation (string _name, exSpriteAnimationClip _animClip) {
        Init();

        exSpriteAnimationState oldState = null;
        if (nameToState.TryGetValue(_name, out oldState)) {
            if (ReferenceEquals(oldState.clip, _animClip)) {
                // if we already have the same name, just return the state
                return oldState;
            }
            else {
                // override the old clip
                animations[animations.IndexOf(oldState.clip)] = _animClip;
            }
        }
        else if (animations.IndexOf(_animClip) != -1) {
            // we already have the animation
            oldState = nameToState[_name];
            if (ReferenceEquals(oldState.clip, _animClip)) {
                return oldState;
            }
        }
        else {
            animations.Add(_animClip);
        }
        
        exSpriteAnimationState newState = new exSpriteAnimationState(_name, _animClip);
        nameToState[_name] = newState;
        return newState;
    }
Beispiel #14
0
    // ------------------------------------------------------------------ 
    /// Stop the playing animation, take the action that setup in the 
    /// exSpriteAnimState.stopAction 
    // ------------------------------------------------------------------ 

    public void Stop ( exSpriteAnimationState _animState ) {
        if ( _animState != null ) {
            if (ReferenceEquals(_animState, curAnimation)) {
                curAnimation = null;
            }
            _animState.time = 0.0f;

            exSpriteAnimationClip.StopAction stopAction = _animState.stopAction;
            switch ( stopAction ) {
            case exSpriteAnimationClip.StopAction.DoNothing:
                break;

            case exSpriteAnimationClip.StopAction.DefaultSprite:
                sprite_.textureInfo = defaultTextureInfo;
                break;

            case exSpriteAnimationClip.StopAction.Hide:
                sprite_.enabled = false;
                break;

            case exSpriteAnimationClip.StopAction.Destroy:
                Object.Destroy(gameObject);
                break;
            }
        }
#if AUTO_DISABLE
        enabled = false;
#endif
    }
Beispiel #15
0
    // ------------------------------------------------------------------ 
    // Desc: 
    // ------------------------------------------------------------------ 

    public void Stop () {
        Stop (curAnimation);
        curAnimation = null;
    }
Beispiel #16
0
    // ------------------------------------------------------------------
    // Do step
    // ------------------------------------------------------------------

    void Step(float _deltaTime)
    {
        if (curAnimation != null)
        {
            int eventStartIndex = curAnimation.frame;
            if (lastFrameIndex == eventStartIndex)
            {
                ++eventStartIndex;
            }

            curAnimation.time += _deltaTime;
            Sample();

            // check if stop
            bool stop = false;
            if (curAnimation.wrapMode == WrapMode.Once ||
                curAnimation.wrapMode == WrapMode.Default ||
                curAnimation.wrapMode == WrapMode.ClampForever)
            {
                if (curAnimation.speed > 0.0f && curAnimation.frame >= curAnimation.totalFrames)
                {
                    if (curAnimation.wrapMode == WrapMode.ClampForever)
                    {
                        stop = false;
                        curAnimation.frame = curAnimation.totalFrames;
                        curAnimation.time  = (float)curAnimation.frame / curAnimation.clip.frameRate;
                    }
                    else
                    {
                        stop = true;
                        curAnimation.frame = curAnimation.totalFrames;
                    }
                }
                else if (curAnimation.speed < 0.0f && curAnimation.frame < 0)
                {
                    if (curAnimation.wrapMode == WrapMode.ClampForever)
                    {
                        stop = false;
                        curAnimation.time  = 0;
                        curAnimation.frame = 0;
                    }
                    else
                    {
                        stop = true;
                        curAnimation.frame = 0;
                    }
                }
            }

            exSpriteAnimationState backupAnimBeforeEvent = curAnimation;

            // trigger events
            if (eventStartIndex <= curAnimation.frame)
            {
                curAnimation.TriggerEvents(this, eventStartIndex, curAnimation.frame);
                lastFrameIndex = backupAnimBeforeEvent.frame;
            }

            // do stop
            if (stop)
            {
                Stop(backupAnimBeforeEvent);
            }
        }
        else
        {
            curIndex = -1;
        }
    }
Beispiel #17
0
    // ------------------------------------------------------------------
    // Desc:
    // ------------------------------------------------------------------

    public void Stop()
    {
        Stop(curAnimation);
        curAnimation = null;
    }