//--------------------------------------------------------
|
// [Author]: 第二世界
|
// [ Date ]: Tuesday, October 10, 2017
|
//--------------------------------------------------------
|
using UnityEngine;
|
using System.Collections;
|
using UnityEngine.UI;
|
using System;
|
|
|
|
namespace vnxbqy.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 = 6f;
|
float durationLimit = 10f;
|
float timer = 0f;
|
|
bool beginMove = false;
|
|
Action onReached;
|
|
public void SetContent(FunctionUnlockType _type, int _id)
|
{
|
SetSpeed(10f);
|
|
m_UnLockType = _type;
|
m_Id = _id;
|
|
switch (_type)
|
{
|
case FunctionUnlockType.Treasure:
|
UI3DTreasureExhibition.Instance.ShowTreasure(m_Id, m_TreasureIcon);
|
originalScale = 9f;
|
break;
|
case FunctionUnlockType.Normal:
|
var config = FuncOpenLVConfig.Get(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 = SkillConfig.Get(_treaure.skillId);
|
m_FunctionIcon.SetSprite(_skillCfg.IconName);
|
m_FunctionIcon.SetNativeSize();
|
originalScale = 1f;
|
break;
|
case FunctionUnlockType.Skill:
|
var skillConfig = SkillConfig.Get(m_Id);
|
m_FunctionIcon.SetSprite(skillConfig.IconName);
|
originalScale = 1f;
|
break;
|
case FunctionUnlockType.TreasureFunc:
|
UI3DTreasureExhibition.Instance.ShowTreasure(m_Id, m_TreasureIcon);
|
originalScale = 9f;
|
break;
|
case FunctionUnlockType.TreasureChapter:
|
originalScale = 1.5f;
|
var treasureConfig = TreasureConfig.Get(m_Id);
|
m_FunctionIcon.SetSprite(treasureConfig.Icon);
|
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 = SkillConfig.Get(_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;
|
}
|
|
StopTreasureShow();
|
|
GameObject.Destroy(this.gameObject);
|
return;
|
}
|
|
if (functionUnLockShowBeginEvent != null)
|
{
|
functionUnLockShowBeginEvent(target.mainWinUnFold, target.skillGroup);
|
}
|
|
timer = 0f;
|
this.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 || !WindowCenter.Instance.IsOpen<MainInterfaceWin>())
|
{
|
try
|
{
|
if (onReached != null)
|
{
|
onReached();
|
onReached = null;
|
}
|
|
target.ResponseFunctionUnLock();
|
if (m_Effect != null)
|
{
|
m_Effect.Stop();
|
}
|
|
if (functionUnLockShowEndEvent != null)
|
{
|
functionUnLockShowEndEvent(m_UnLockType);
|
}
|
|
StopTreasureShow();
|
|
GameObject.Destroy(this.gameObject);
|
}
|
catch (Exception ex)
|
{
|
DebugEx.Log(ex.Message);
|
}
|
}
|
|
}
|
|
void StopTreasureShow()
|
{
|
switch (m_UnLockType)
|
{
|
case FunctionUnlockType.Treasure:
|
case FunctionUnlockType.TreasureSkill:
|
case FunctionUnlockType.TreasureFunc:
|
UI3DTreasureExhibition.Instance.Stop();
|
break;
|
}
|
}
|
}
|
|
}
|
|
|
|