216 lines
5.9 KiB
Plaintext
216 lines
5.9 KiB
Plaintext
using Sirenix.OdinInspector;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class PHomeLotteryDisplayer : MonoBehaviour
|
|
{
|
|
//[SerializeField]
|
|
//private List<LotteryReward> m_lotteryRewards;
|
|
|
|
[SerializeField]
|
|
private NodeGrantPool m_lotteryPool;
|
|
|
|
[SerializeField]
|
|
private List<PHomeLotteryItem> m_lotteryItems;
|
|
[SerializeField]
|
|
private Button m_startBtn;
|
|
[SerializeField]
|
|
private Button m_getRewardBtn;
|
|
[SerializeField]
|
|
private Text m_startText;
|
|
[SerializeField]
|
|
private Image m_finalRewardImage;
|
|
[SerializeField]
|
|
private float m_stopTime = 8f;
|
|
[SerializeField]
|
|
private float m_stopStartTime = 2f;
|
|
private bool m_startLottery;
|
|
private int m_currIndex;
|
|
|
|
private void Start()
|
|
{
|
|
Init();
|
|
}
|
|
|
|
List<int> weightList;
|
|
public void Init()
|
|
{
|
|
gameObject.SetActive(true);
|
|
m_startBtn.onClick.RemoveAllListeners();
|
|
m_startBtn.onClick.AddListener(StartBtnClick);
|
|
m_getRewardBtn.onClick.RemoveAllListeners();
|
|
m_getRewardBtn.onClick.AddListener(GetRewardBtnClick);
|
|
m_getRewardBtn.gameObject.SetActive(false);
|
|
if (weightList == null)
|
|
{
|
|
weightList = new List<int>();
|
|
}
|
|
weightList.Clear();
|
|
int itemCount = m_lotteryItems.Count;
|
|
List<PHomePush.NodeChoseItem> grantItems = m_lotteryPool.GetItemsRandom(itemCount).ToList();
|
|
//for (int j = 0; j < m_lotteryRewards.Count; j++)
|
|
//{
|
|
// currWeight += m_lotteryRewards[j].Weight;
|
|
// weightList.Add(currWeight);
|
|
//}
|
|
for (int j = 0; j < m_lotteryItems.Count; j++)
|
|
{
|
|
m_lotteryItems[j].Init(grantItems[j]);
|
|
}
|
|
m_finalRewardImage.gameObject.SetActive(false);
|
|
m_startLottery = false;
|
|
if (m_currIndex != 0)
|
|
{
|
|
m_lotteryItems[m_currIndex].Select(false);
|
|
}
|
|
speedIndex = 0;
|
|
stopTimer = 0;
|
|
m_currIndex = 0;
|
|
}
|
|
|
|
int GetRewardIndexByIndex(int currWeight)
|
|
{
|
|
for (int j = 0; j < weightList.Count; j++)
|
|
{
|
|
if (currWeight < weightList[j])
|
|
{
|
|
return j;
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
void StartBtnClick()
|
|
{
|
|
m_finalRewardImage.gameObject.SetActive(false);
|
|
if (m_startLottery && stopTimer < m_stopStartTime) { return; }
|
|
m_startLottery = !m_startLottery;
|
|
m_startText.text = m_startLottery ? "停止" : "开始";
|
|
if (m_currIndex == 0)
|
|
{
|
|
m_currIndex = Random.Range(0, 20);
|
|
}
|
|
timer = 0;
|
|
if (m_startLottery)
|
|
{
|
|
speedIndex = 0;
|
|
}
|
|
}
|
|
|
|
|
|
float[] timeGap = { 0.5f, 0.4f, 0.3f, 0.2f, 0.1f };
|
|
float timer;
|
|
float stopTimer;
|
|
float totalTimer;
|
|
int speedIndex;
|
|
private void Update()
|
|
{
|
|
if (m_startLottery)
|
|
{
|
|
m_lotteryItems[m_currIndex].Select(true);
|
|
timer += Time.unscaledDeltaTime;
|
|
stopTimer += Time.unscaledDeltaTime;
|
|
totalTimer += Time.unscaledDeltaTime;
|
|
if (timer >= timeGap[speedIndex])
|
|
{
|
|
timer = 0;
|
|
if (speedIndex < timeGap.Length - 1)
|
|
{
|
|
speedIndex++;
|
|
}
|
|
m_lotteryItems[m_currIndex].Select(false);
|
|
m_currIndex++;
|
|
if (m_currIndex >= 20)
|
|
{
|
|
m_currIndex -= 20;
|
|
}
|
|
m_lotteryItems[m_currIndex].Select(true);
|
|
}
|
|
}
|
|
if (totalTimer > m_stopTime)
|
|
{
|
|
StartBtnClick();
|
|
totalTimer = 0;
|
|
}
|
|
if (!m_startLottery && speedIndex != 0)
|
|
{
|
|
m_lotteryItems[m_currIndex].Select(true);
|
|
timer += Time.unscaledDeltaTime;
|
|
if (timer >= timeGap[speedIndex])
|
|
{
|
|
timer = 0;
|
|
m_lotteryItems[m_currIndex].Select(false);
|
|
m_currIndex++;
|
|
if (m_currIndex >= 20) { m_currIndex -= 20; }
|
|
m_lotteryItems[m_currIndex].Select(true);
|
|
if (speedIndex > 0)
|
|
{
|
|
speedIndex--;
|
|
if (speedIndex == 0)
|
|
{
|
|
SetRewardStatu();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
void SetRewardStatu()
|
|
{
|
|
m_startText.text = "领取";
|
|
m_getRewardBtn.gameObject.SetActive(true);
|
|
m_finalRewardImage.gameObject.SetActive(true);
|
|
PHomePush.NodeChoseItem rewardItem = m_lotteryItems[m_currIndex].rewardItem;
|
|
if (rewardItem.GrantBuff != null)
|
|
{
|
|
m_finalRewardImage.sprite = rewardItem.GrantBuff.DesImage;
|
|
}
|
|
else if (rewardItem.GrantPet != null)
|
|
{
|
|
m_finalRewardImage.sprite = rewardItem.GrantPet.DesImage;
|
|
}
|
|
}
|
|
|
|
void GetRewardBtnClick()
|
|
{
|
|
PHomePush.NodeChoseItem rewardItem = m_lotteryItems[m_currIndex].rewardItem;
|
|
PHomeUnit playerUnit = PHomeGameMode.main.PlayerUnit;
|
|
if (rewardItem.GrantBuff != null)
|
|
{
|
|
PHomeGameMode.main.AddBuff(playerUnit, rewardItem.GrantBuff);
|
|
}
|
|
else if (rewardItem.GrantPet != null)
|
|
{
|
|
PHomeGameMode.main.PetLevelUp();
|
|
}
|
|
MessageDispatcher.Instance.SendMessage((uint)MsgType.EndLottery, null);
|
|
gameObject.SetActive(false);
|
|
}
|
|
|
|
//[Button]
|
|
//public void RefreshUI()
|
|
//{
|
|
// for (int j = 0; j < m_lotteryItems.Count; j++)
|
|
// {
|
|
// m_lotteryItems[j].Init(m_fixedRewards[j]);
|
|
// }
|
|
//}
|
|
}
|
|
|
|
[System.Serializable]
|
|
public struct LotteryReward
|
|
{
|
|
public LotteryRewardItem RewardItem;
|
|
public int Weight;
|
|
}
|
|
|
|
[System.Serializable]
|
|
public struct LotteryRewardItem
|
|
{
|
|
public PHomeLotteryPetLevelUp GrantPet;
|
|
public PHomeBuff GrantBuff;
|
|
} |