少年修仙传客户端代码仓库
client_linchunjie
2018-08-21 a1e7374b1d3e6af8b8cf6bf6f59e3f9b101eb8be
System/Treasure/FunctionUnlockFlyObject.cs
@@ -1,213 +1,218 @@
//--------------------------------------------------------
//    [Author]:           第二世界
//    [  Date ]:           Tuesday, October 10, 2017
//--------------------------------------------------------
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System;
using TableConfig;
namespace Snxxz.UI
{
    public class FunctionUnlockFlyObject : MonoBehaviour
    {
        public static event Action<bool, int> functionUnLockShowBeginEvent;
        public static event Action<FunctionUnlockType> functionUnLockShowEndEvent;
        [SerializeField] Image m_FunctionIcon;
        [SerializeField] RawImage m_TreasureIcon;
        [SerializeField] UIEffect m_Effect;
        int m_Id = 0;
        FunctionUnlockType m_UnLockType;
        FunctionUnlockFlyObjectTarget target;
        Vector3 originalPosition = Vector3.zero;
        float originalScale = 2f;
        float speed = 3f;
        float durationLimit = 10f;
        float timer = 0f;
        bool beginMove = false;
        Action onReached;
        public void SetContent(FunctionUnlockType _type, int _id)
        {
            m_UnLockType = _type;
            m_Id = _id;
            switch (_type)
            {
                case FunctionUnlockType.Treasure:
                    UI3DTreasureExhibition.Instance.BeginShowTreasure(m_Id, m_TreasureIcon);
                    originalScale = 9f;
                    break;
                case FunctionUnlockType.Normal:
                    var config = ConfigManager.Instance.GetTemplate<FuncOpenLVConfig>(m_Id);
                    m_FunctionIcon.SetSprite(config.Icon);
                    originalScale = 2f;
                    break;
                case FunctionUnlockType.TreasureSkill:
                    Treasure _treaure;
                    ModelCenter.Instance.GetModel<TreasureModel>().TryGetTreasure(m_Id, out _treaure);
                    var _skillCfg = ConfigManager.Instance.GetTemplate<SkillConfig>(_treaure.unLockSkill);
                    m_FunctionIcon.SetSprite(_skillCfg.IconName);
                    m_FunctionIcon.SetNativeSize();
                    originalScale = 1f;
                    break;
                case FunctionUnlockType.Skill:
                    var skillConfig = ConfigManager.Instance.GetTemplate<SkillConfig>(m_Id);
                    m_FunctionIcon.SetSprite(skillConfig.IconName);
                    originalScale = 1f;
                    break;
            }
            this.transform.localScale = Vector3.one * originalScale;
            if (m_Effect != null)
            {
                m_Effect.Play();
            }
            target = FunctionUnlockFlyObjectTargetCenter.GetTarget(m_UnLockType, m_Id);
            if (target != null)
            {
                target.Prepare();
                beginMove = false;
            }
        }
        public void SetContent(int _id, int _skillId)
        {
            m_UnLockType = FunctionUnlockType.Treasure;
            m_Id = _id;
            var skillConfig = ConfigManager.Instance.GetTemplate<SkillConfig>(_skillId);
            m_FunctionIcon.SetSprite(skillConfig.IconName);
            originalScale = 1f;
            this.transform.localScale = Vector3.one * originalScale;
            if (m_Effect != null)
            {
                m_Effect.Play();
            }
            target = FunctionUnlockFlyObjectTargetCenter.GetTarget(m_UnLockType, m_Id);
            if (target != null)
            {
                target.Prepare();
                beginMove = false;
            }
        }
        public void SetSpeed(float _speed)
        {
            speed = _speed;
        }
        public void SetScale(float _scale)
        {
            originalScale = _scale;
        }
        public void Begin(Action _callBack)
        {
            onReached = _callBack;
            if (target == null)
            {
                if (onReached != null)
                {
                    onReached();
                    onReached = null;
                }
                GameObject.Destroy(this.gameObject);
                return;
            }
            if (functionUnLockShowBeginEvent != null)
            {
                functionUnLockShowBeginEvent(target.mainWinUnFold, target.skillGroup);
            }
            timer = 0f;
            this.gameObject.SetActive(true);
            SoundPlayer.Instance.PlayUIAudio(22);
            StartCoroutine(Co_WaitOneFrameToStart());
        }
        IEnumerator Co_WaitOneFrameToStart()
        {
            yield return null;
            originalPosition = this.transform.position.SetZ(0);
            beginMove = true;
        }
        private void LateUpdate()
        {
            if (!beginMove)
            {
                return;
            }
            timer += Time.deltaTime;
            var to = target.transform.position.SetZ(0);
            var normalizeDir = Vector3.Normalize(to - this.transform.position.SetZ(0));
            var delta1 = normalizeDir * speed * Time.deltaTime;
            var delta2 = to - this.transform.position.SetZ(0);
            var delta = Vector3.Magnitude(delta1) < Vector3.Magnitude(delta2) ? delta1 : delta2;
            this.transform.Translate(delta);
            var scaleT = 1 - Vector3.Distance(to, this.transform.position) / Vector3.Distance(originalPosition, to);
            this.transform.localScale = Vector3.one * Mathf.Lerp(originalScale, 1, scaleT);
            if (timer > durationLimit || Vector3.Distance(to, this.transform.position.SetZ(0)) < 0.1f)
            {
                try
                {
                    if (onReached != null)
                    {
                        onReached();
                        onReached = null;
                    }
                    target.ResponseFunctionUnLock();
                    if (m_Effect != null)
                    {
                        m_Effect.Stop();
                    }
                    if (functionUnLockShowEndEvent != null)
                    {
                        functionUnLockShowEndEvent(m_UnLockType);
                    }
                    switch (m_UnLockType)
                    {
                        case FunctionUnlockType.Treasure:
                        case FunctionUnlockType.TreasureSkill:
                            UI3DTreasureExhibition.Instance.StopShow();
                            break;
                        case FunctionUnlockType.Normal:
                            break;
                    }
                    GameObject.Destroy(this.gameObject);
                }
                catch (Exception ex)
                {
                }
            }
        }
    }
}
//--------------------------------------------------------
//    [Author]:           第二世界
//    [  Date ]:           Tuesday, October 10, 2017
//--------------------------------------------------------
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System;
using TableConfig;
namespace Snxxz.UI
{
    public class FunctionUnlockFlyObject : MonoBehaviour
    {
        public static event Action<bool, int> functionUnLockShowBeginEvent;
        public static event Action<FunctionUnlockType> functionUnLockShowEndEvent;
        [SerializeField] Image m_FunctionIcon;
        [SerializeField] RawImage m_TreasureIcon;
        [SerializeField] UIEffect m_Effect;
        int m_Id = 0;
        FunctionUnlockType m_UnLockType;
        FunctionUnlockFlyObjectTarget target;
        Vector3 originalPosition = Vector3.zero;
        float originalScale = 2f;
        float speed = 3f;
        float durationLimit = 10f;
        float timer = 0f;
        bool beginMove = false;
        Action onReached;
        public void SetContent(FunctionUnlockType _type, int _id)
        {
            m_UnLockType = _type;
            m_Id = _id;
            switch (_type)
            {
                case FunctionUnlockType.Treasure:
                    UI3DTreasureExhibition.Instance.BeginShowTreasure(m_Id, m_TreasureIcon);
                    originalScale = 9f;
                    break;
                case FunctionUnlockType.Normal:
                    var config = ConfigManager.Instance.GetTemplate<FuncOpenLVConfig>(m_Id);
                    m_FunctionIcon.SetSprite(config.Icon);
                    originalScale = 2f;
                    break;
                case FunctionUnlockType.TreasureSkill:
                    Treasure _treaure;
                    ModelCenter.Instance.GetModel<TreasureModel>().TryGetTreasure(m_Id, out _treaure);
                    var _skillCfg = ConfigManager.Instance.GetTemplate<SkillConfig>(_treaure.unLockSkill);
                    m_FunctionIcon.SetSprite(_skillCfg.IconName);
                    m_FunctionIcon.SetNativeSize();
                    originalScale = 1f;
                    break;
                case FunctionUnlockType.Skill:
                    var skillConfig = ConfigManager.Instance.GetTemplate<SkillConfig>(m_Id);
                    m_FunctionIcon.SetSprite(skillConfig.IconName);
                    originalScale = 1f;
                    break;
                case FunctionUnlockType.TreasureFunc:
                    UI3DTreasureExhibition.Instance.BeginShowTreasure(m_Id, m_TreasureIcon);
                    originalScale = 9f;
                    break;
            }
            this.transform.localScale = Vector3.one * originalScale;
            if (m_Effect != null)
            {
                m_Effect.Play();
            }
            target = FunctionUnlockFlyObjectTargetCenter.GetTarget(m_UnLockType, m_Id);
            if (target != null)
            {
                target.Prepare();
                beginMove = false;
            }
        }
        public void SetContent(int _id, int _skillId)
        {
            m_UnLockType = FunctionUnlockType.Treasure;
            m_Id = _id;
            var skillConfig = ConfigManager.Instance.GetTemplate<SkillConfig>(_skillId);
            m_FunctionIcon.SetSprite(skillConfig.IconName);
            originalScale = 1f;
            this.transform.localScale = Vector3.one * originalScale;
            if (m_Effect != null)
            {
                m_Effect.Play();
            }
            target = FunctionUnlockFlyObjectTargetCenter.GetTarget(m_UnLockType, m_Id);
            if (target != null)
            {
                target.Prepare();
                beginMove = false;
            }
        }
        public void SetSpeed(float _speed)
        {
            speed = _speed;
        }
        public void SetScale(float _scale)
        {
            originalScale = _scale;
        }
        public void Begin(Action _callBack)
        {
            onReached = _callBack;
            if (target == null)
            {
                if (onReached != null)
                {
                    onReached();
                    onReached = null;
                }
                GameObject.Destroy(this.gameObject);
                return;
            }
            if (functionUnLockShowBeginEvent != null)
            {
                functionUnLockShowBeginEvent(target.mainWinUnFold, target.skillGroup);
            }
            timer = 0f;
            this.gameObject.SetActive(true);
            SoundPlayer.Instance.PlayUIAudio(22);
            StartCoroutine(Co_WaitOneFrameToStart());
        }
        IEnumerator Co_WaitOneFrameToStart()
        {
            yield return null;
            originalPosition = this.transform.position.SetZ(0);
            beginMove = true;
        }
        private void LateUpdate()
        {
            if (!beginMove)
            {
                return;
            }
            timer += Time.deltaTime;
            var to = target.transform.position.SetZ(0);
            var normalizeDir = Vector3.Normalize(to - this.transform.position.SetZ(0));
            var delta1 = normalizeDir * speed * Time.deltaTime;
            var delta2 = to - this.transform.position.SetZ(0);
            var delta = Vector3.Magnitude(delta1) < Vector3.Magnitude(delta2) ? delta1 : delta2;
            this.transform.Translate(delta);
            var scaleT = 1 - Vector3.Distance(to, this.transform.position) / Vector3.Distance(originalPosition, to);
            this.transform.localScale = Vector3.one * Mathf.Lerp(originalScale, 1, scaleT);
            if (timer > durationLimit || Vector3.Distance(to, this.transform.position.SetZ(0)) < 0.1f)
            {
                try
                {
                    if (onReached != null)
                    {
                        onReached();
                        onReached = null;
                    }
                    target.ResponseFunctionUnLock();
                    if (m_Effect != null)
                    {
                        m_Effect.Stop();
                    }
                    if (functionUnLockShowEndEvent != null)
                    {
                        functionUnLockShowEndEvent(m_UnLockType);
                    }
                    switch (m_UnLockType)
                    {
                        case FunctionUnlockType.Treasure:
                        case FunctionUnlockType.TreasureSkill:
                        case FunctionUnlockType.TreasureFunc:
                            UI3DTreasureExhibition.Instance.StopShow();
                            break;
                        case FunctionUnlockType.Normal:
                            break;
                    }
                    GameObject.Destroy(this.gameObject);
                }
                catch (Exception ex)
                {
                }
            }
        }
    }
}