246 lines
6.8 KiB
C#
246 lines
6.8 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using DG.Tweening;
|
|
|
|
public class PanelMain : BasePanel
|
|
{
|
|
[SerializeField] float[] mIconPosX;
|
|
[SerializeField] Image[] mImgLevelIcons;
|
|
|
|
[SerializeField] Button mBtnLast;
|
|
[SerializeField] Button mBtnNext;
|
|
|
|
[SerializeField] Button mBtnStart;
|
|
[SerializeField] Button mBtnLevel;
|
|
|
|
private List<Image> mLevelIconList = new List<Image>();
|
|
[SerializeField] List<int> mLevelList = new List<int>();
|
|
|
|
private int mTotalLevel;
|
|
private const int FOCUS_IDX = 2;
|
|
|
|
private int mWaftDir = 1;
|
|
private bool mIsWafting = false;
|
|
|
|
private bool mIsRolling = false;
|
|
private TimerUnit mRollTimer;
|
|
|
|
private const float FIX_DURATION = 0.2f;
|
|
private const float FIX_WAIT = 10;
|
|
|
|
private const float ROLL_DURATION = 0.3f;
|
|
private const float WAFT_DURATION = 4f;
|
|
|
|
private void Awake()
|
|
{
|
|
UIUtils.BindBtn(mBtnLast, OnClickLast);
|
|
UIUtils.BindBtn(mBtnNext, OnClickNext);
|
|
UIUtils.BindBtn(mBtnStart, OnClickStart);
|
|
UIUtils.BindBtn(mBtnLevel, OnClickLevel);
|
|
}
|
|
|
|
public override void OnOpen()
|
|
{
|
|
base.OnOpen();
|
|
|
|
mRollTimer = TimerManager.Instance.CreateTimerUnit();
|
|
|
|
InitLevelIcons();
|
|
}
|
|
|
|
public override void OnClose()
|
|
{
|
|
base.OnClose();
|
|
|
|
if (mRollTimer != null)
|
|
{
|
|
mRollTimer.Destroy();
|
|
mRollTimer = null;
|
|
}
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
if (mRollTimer != null)
|
|
{
|
|
mRollTimer.Destroy();
|
|
mRollTimer = null;
|
|
}
|
|
}
|
|
|
|
private void InitLevelIcons()
|
|
{
|
|
mTotalLevel = GameConfig.Instance.LevelSort.Count;
|
|
|
|
int tLevel = 0;
|
|
for (int i = 0; i < mImgLevelIcons.Length; i++)
|
|
{
|
|
tLevel = GetLevelInCircle(-2 + i + PlayerData.Instance.CurrentLevel, mTotalLevel);
|
|
mLevelList.Add(tLevel);
|
|
|
|
mImgLevelIcons[i].sprite = ResourceManager.Instance.LoadRes<Sprite>(Const.Path.GetLevelIconBig(GameConfig.Instance.LevelSort[(tLevel - 1) % mTotalLevel]));
|
|
mLevelIconList.Add(mImgLevelIcons[i]);
|
|
}
|
|
|
|
Waft();
|
|
}
|
|
|
|
private int GetLevelInCircle(int pLevelNum, int pTotalLevel)
|
|
{
|
|
int tFinalLevel = pLevelNum;
|
|
if (tFinalLevel <= 0)
|
|
{
|
|
while (tFinalLevel <= 0)
|
|
{
|
|
tFinalLevel += pTotalLevel;
|
|
}
|
|
}
|
|
else if (tFinalLevel > pTotalLevel)
|
|
{
|
|
tFinalLevel = (tFinalLevel - 1) % pTotalLevel + 1;
|
|
}
|
|
|
|
return tFinalLevel;
|
|
}
|
|
|
|
private void FixRolling()
|
|
{
|
|
mIsWafting = false;
|
|
mIsRolling = false;
|
|
|
|
for (int i = 1; i < mLevelIconList.Count - 1; i++)
|
|
{
|
|
mLevelIconList[i].GetComponent<RectTransform>().DOKill();
|
|
mLevelIconList[i].GetComponent<RectTransform>().DOAnchorPosX(mIconPosX[i], FIX_DURATION).SetEase(Ease.OutSine);
|
|
}
|
|
|
|
mRollTimer.StartTimer(Waft, FIX_WAIT);
|
|
}
|
|
|
|
private void Waft()
|
|
{
|
|
mIsWafting = true;
|
|
mIsRolling = false;
|
|
|
|
for (int i = 1; i < mLevelIconList.Count - 1; i++)
|
|
{
|
|
mLevelIconList[i].GetComponent<RectTransform>().DOKill();
|
|
mLevelIconList[i].GetComponent<RectTransform>().DOAnchorPosX(mIconPosX[i] + mWaftDir * 80, WAFT_DURATION).SetEase(Ease.Linear);
|
|
}
|
|
|
|
if (mWaftDir == 1)
|
|
{
|
|
mRollTimer.StartTimer(RollRight, WAFT_DURATION);
|
|
}
|
|
else
|
|
{
|
|
mRollTimer.StartTimer(RollLeft, WAFT_DURATION);
|
|
}
|
|
}
|
|
|
|
private void RollLeft()
|
|
{
|
|
mIsRolling = true;
|
|
|
|
for (int i = 0; i < mLevelList.Count; i++)
|
|
{
|
|
mLevelList[i] = GetLevelInCircle(mLevelList[i] + 1, mTotalLevel);
|
|
}
|
|
|
|
Image tStartImg = mLevelIconList[0];
|
|
for (int i = 1; i < mLevelIconList.Count; i++)
|
|
{
|
|
int tLastIndex = (i - 1) % mLevelIconList.Count;
|
|
|
|
mLevelIconList[tLastIndex] = mLevelIconList[i];
|
|
mLevelIconList[tLastIndex].GetComponent<RectTransform>().DOKill();
|
|
mLevelIconList[tLastIndex].GetComponent<RectTransform>().DOAnchorPosX(mIconPosX[tLastIndex], ROLL_DURATION).SetEase(Ease.OutSine);
|
|
}
|
|
mLevelIconList[mLevelIconList.Count - 1] = tStartImg;
|
|
mLevelIconList[mLevelIconList.Count - 1].sprite = ResourceManager.Instance.LoadRes<Sprite>(Const.Path.GetLevelIconBig(GameConfig.Instance.LevelSort[mLevelList[mLevelIconList.Count - 1] - 1]));
|
|
mLevelIconList[mLevelIconList.Count - 1].GetComponent<RectTransform>().anchoredPosition = new Vector2(mIconPosX[mLevelIconList.Count - 1], 0);
|
|
|
|
mRollTimer.StartTimer(RollOver, ROLL_DURATION);
|
|
}
|
|
|
|
private void RollRight()
|
|
{
|
|
mIsRolling = true;
|
|
|
|
for (int i = 0; i < mLevelList.Count; i++)
|
|
{
|
|
mLevelList[i] = GetLevelInCircle(mLevelList[i] - 1, mTotalLevel);
|
|
}
|
|
|
|
Image tEndImg = mLevelIconList[mLevelIconList.Count - 1];
|
|
for (int i = mLevelIconList.Count - 2; i >= 0; i--)
|
|
{
|
|
int tNextIndex = (i + 1) % mLevelIconList.Count;
|
|
|
|
mLevelIconList[tNextIndex] = mLevelIconList[i];
|
|
mLevelIconList[tNextIndex].GetComponent<RectTransform>().DOKill();
|
|
mLevelIconList[tNextIndex].GetComponent<RectTransform>().DOAnchorPosX(mIconPosX[tNextIndex], ROLL_DURATION).SetEase(Ease.OutSine);
|
|
}
|
|
mLevelIconList[0] = tEndImg;
|
|
mLevelIconList[0].sprite = ResourceManager.Instance.LoadRes<Sprite>(Const.Path.GetLevelIconBig(GameConfig.Instance.LevelSort[mLevelList[0] - 1]));
|
|
mLevelIconList[0].GetComponent<RectTransform>().anchoredPosition = new Vector2(mIconPosX[0], 0);
|
|
|
|
mRollTimer.StartTimer(RollOver, ROLL_DURATION);
|
|
}
|
|
|
|
private void RollOver()
|
|
{
|
|
mIsRolling = false;
|
|
mRollTimer.StartTimer(Waft, mIsWafting ? -1 : FIX_WAIT);
|
|
}
|
|
|
|
private void OnClickLast()
|
|
{
|
|
if (mIsRolling)
|
|
return;
|
|
|
|
if (mIsWafting && mWaftDir == -1)//turn around
|
|
{
|
|
FixRolling();
|
|
}
|
|
else
|
|
{
|
|
mIsWafting = false;
|
|
RollRight();
|
|
}
|
|
|
|
mWaftDir = 1;
|
|
}
|
|
|
|
private void OnClickNext()
|
|
{
|
|
if (mIsRolling)
|
|
return;
|
|
|
|
if (mIsWafting && mWaftDir == 1)//turn around
|
|
{
|
|
FixRolling();
|
|
}
|
|
else
|
|
{
|
|
mIsWafting = false;
|
|
RollLeft();
|
|
}
|
|
|
|
mWaftDir = -1;
|
|
}
|
|
|
|
private void OnClickStart()
|
|
{
|
|
PlayerData.Instance.CurrentLevel = mLevelList[FOCUS_IDX];
|
|
GameManager.Instance.LoadGame();
|
|
}
|
|
|
|
private void OnClickLevel()
|
|
{
|
|
PanelLevel tUILevel = UIManager.Instance.OpenUI<PanelLevel>();
|
|
tUILevel.Init(false);
|
|
}
|
|
} |