//--------------------------------------------------------
|
// [Author]: 第二世界
|
// [ Date ]: Friday, March 01, 2019
|
//--------------------------------------------------------
|
|
using LitJson;
|
using System;
|
using System.Collections;
|
using System.Collections.Generic;
|
using UnityEngine;
|
using UnityEngine.UI;
|
namespace vnxbqy.UI
|
{
|
|
public class PushCoinWin : Window
|
{
|
[SerializeField] Button closeBtn;
|
[SerializeField] Button startBtn;
|
[SerializeField] Text countText;
|
[SerializeField] List<Transform> awardObjs;
|
|
[SerializeField] Transform wordObj;
|
[SerializeField] Image wordImg;
|
|
[SerializeField] Button superPushBtn;
|
[SerializeField] Transform pushArrow;
|
[SerializeField] Text money1;
|
[SerializeField] Text money2;
|
[SerializeField] Text money3;
|
|
[SerializeField] Transform surpriseCardObj; // 至尊卡表现
|
[SerializeField] Image surpriseWord;
|
[SerializeField] UIEffect superEffect;
|
[SerializeField] Button ruleBtn;
|
[SerializeField] UIEffect ruleEffect;
|
|
[SerializeField] Button adWinBtn;
|
|
#region Built-in
|
|
List<UIAlphaTween> selectImags = new List<UIAlphaTween>();
|
|
PushCoinModel model { get { return ModelCenter.Instance.GetModel<PushCoinModel>(); } }
|
VipModel vipModel { get { return ModelCenter.Instance.GetModel<VipModel>(); } }
|
|
|
protected override void BindController()
|
{
|
}
|
|
protected override void AddListeners()
|
{
|
closeBtn.AddListener(() => { CloseImmediately(); });
|
startBtn.AddListener(() =>
|
{
|
|
if (UIHelper.GetMoneyCnt(48) == 0)
|
{
|
SysNotifyMgr.Instance.ShowTip("NoEnoughTimes");
|
return;
|
}
|
startBtn.interactable = false;
|
closeBtn.interactable = false;
|
|
model.PushCoinPack(0);
|
});
|
|
superPushBtn.AddListener(OnSuperPush);
|
ruleBtn.AddListener(() => { WindowCenter.Instance.Open<HowToPlayPushCoinWin>(); });
|
|
adWinBtn.AddListener(() => { WindowCenter.Instance.Open<VipRechargeWin>(false, 2); });
|
}
|
|
protected override void OnPreOpen()
|
{
|
WindowJumpMgr.Instance.ClearJumpData();
|
WindowCenter.Instance.CloseAll();
|
|
PlayerDatas.Instance.playerDataRefreshEvent += OnPlayerDataRefreshEvent;
|
vipModel.rechargeCountEvent += VipModel_rechargeCountEvent;
|
model.OnGetAwardResult += OnGetPrize;
|
|
countText.text = Language.Get("HFTurntable_2", UIHelper.GetMoneyCnt(48));
|
//需要考虑界面或者强制关闭游戏的情况 次数 生成掉落
|
DisplayAwards();
|
ShowMoney();
|
UI3DPushCoinStage.Instance.Open();
|
wordObj.SetActive(false);
|
closeBtn.interactable = true;
|
startBtn.interactable = true;
|
|
surpriseCardObj.SetActive(false);
|
|
if (PlayerDatas.Instance.baseData.LV < 100 && !model.IsActiveSuperPush())
|
{
|
ruleEffect.Play();
|
}
|
else
|
{
|
ruleEffect.Stop();
|
}
|
}
|
|
protected override void OnActived()
|
{
|
|
}
|
|
protected override void OnAfterOpen()
|
{
|
RestoreCoin();
|
ShowSuperPushState();
|
}
|
|
protected override void OnPreClose()
|
{
|
PlayerDatas.Instance.playerDataRefreshEvent -= OnPlayerDataRefreshEvent;
|
vipModel.rechargeCountEvent -= VipModel_rechargeCountEvent;
|
model.OnGetAwardResult -= OnGetPrize;
|
model.NoteCoin();
|
UI3DPushCoinStage.Instance.Close();
|
ShowSuperPush();
|
|
}
|
|
protected override void OnAfterClose()
|
{
|
}
|
|
|
#endregion
|
|
void OnGetPrize(int index)
|
{
|
if (index == 0)
|
return;
|
model.lastStopIndex = model.stopIndex;
|
model.stopIndex = index - 1;
|
StartTurn();
|
}
|
|
private void VipModel_rechargeCountEvent(int obj)
|
{
|
if (obj == model.ctgID)
|
{
|
ShowSuperPushState();
|
}
|
}
|
|
|
void ShowSurpriseCard(int mark)
|
{
|
surpriseCardObj.SetActive(true);
|
surpriseWord.SetSprite(string.Format("Show90OffCard" + mark));
|
surpriseWord.SetNativeSize();
|
Clock.AlarmAfter(2f, () => {
|
surpriseCardObj.SetActive(false);
|
ShowSuperPushState();
|
});
|
}
|
|
void ShowSuperPush()
|
{
|
if (!DayRemind.Instance.GetDayRemind(DayRemind.SuperPush))
|
{
|
VipModel.RechargeCount rechargeCount;
|
vipModel.TryGetRechargeCount(model.ctgID, out rechargeCount);
|
if (rechargeCount.totalCount == 0)
|
{
|
WindowCenter.Instance.Open<SuperPushWin>();
|
DayRemind.Instance.SetDayRemind(DayRemind.SuperPush, true);
|
}
|
}
|
}
|
|
|
void OnPlayerDataRefreshEvent(PlayerDataType type)
|
{
|
switch (type)
|
{
|
case PlayerDataType.Gold:
|
case PlayerDataType.GoldPaper:
|
case PlayerDataType.Silver:
|
case PlayerDataType.ExAttr6:
|
case PlayerDataType.default5:
|
case PlayerDataType.default6:
|
ShowMoney();
|
break;
|
case PlayerDataType.default40:
|
countText.text = Language.Get("HFTurntable_2", UIHelper.GetMoneyCnt(48));
|
break;
|
|
case PlayerDataType.LV2:
|
ShowSurpriseCard(1);
|
break;
|
}
|
}
|
|
float lastClickTime = 0;
|
|
void OnSuperPush()
|
{
|
if (!model.IsActiveSuperPush())
|
{
|
WindowCenter.Instance.Open<SuperPushWin>();
|
}
|
else
|
{
|
if (Time.time - lastClickTime > 5)
|
{
|
lastClickTime = Time.time;
|
UI3DPushCoinStage.Instance.SetSuperPush();
|
SoundPlayer.Instance.PlayUIAudio(856);
|
superEffect.Play();
|
Clock.AlarmAfter(5f, () => {
|
if (WindowCenter.Instance.IsOpen<PushCoinWin>())
|
UI3DPushCoinStage.Instance.InitPushPanel();
|
});
|
}
|
}
|
|
}
|
|
void ShowSuperPushState()
|
{
|
if (!model.IsActiveSuperPush())
|
{
|
superPushBtn.GetComponent<RotationTween>().Stop();
|
pushArrow.SetActive(false);
|
}
|
else
|
{
|
superPushBtn.GetComponent<RotationTween>().Play();
|
if (!PlayerDatas.Instance.baseData.IsActive90Off)
|
pushArrow.SetActive(true);
|
else
|
pushArrow.SetActive(false);
|
}
|
|
}
|
|
|
void ShowMoney()
|
{
|
money3.text = ItemLogicUtility.Instance.OnChangeCoinsUnit(PlayerDatas.Instance.baseData.allCopper);
|
money2.text = ItemLogicUtility.Instance.OnChangeCoinsUnit(PlayerDatas.Instance.baseData.bindDiamond);
|
money1.text = UIHelper.GetMoneyCnt(1).ToString();
|
}
|
|
void DisplayAwards()
|
{
|
selectImags.Clear();
|
for (int i = 0; i < awardObjs.Count; i++)
|
{
|
var num = model.awardsTab[i][2];
|
awardObjs[i].Find("icon").GetComponent<Image>().SetSprite(string.Format("pushcointype{0}_{1}", model.awardsTab[i][0], model.awardsTab[i][2]));
|
awardObjs[i].Find("num").GetComponent<Text>().text = num <= 1 ? "" : "X" + num;
|
selectImags.Add(awardObjs[i].Find("selectImg").GetComponent<UIAlphaTween>());
|
}
|
|
for (int i = 0; i < selectImags.Count; i++)
|
{
|
if (i != model.stopIndex)
|
selectImags[i].canvasGroup.alpha = 0;
|
else
|
selectImags[i].canvasGroup.alpha = 1;
|
}
|
}
|
|
|
float acceleration = 2f;
|
float startSpeed = 2f;
|
float maxSpeed = 10f;
|
|
|
|
// 实现转圈逻辑 selectImags,转到随机出来的索引,转到第二圈的时候停止
|
public void StartTurn()
|
{
|
|
StartCoroutine(Turn());
|
}
|
|
|
|
IEnumerator Turn()
|
{
|
int index = model.lastStopIndex;
|
float speed = startSpeed;
|
int turnStep = 0; // 转动步数 超过12以后才停止
|
while (true)
|
{
|
index++;
|
if (index >= selectImags.Count)
|
{
|
index = 0;
|
}
|
speed += acceleration;
|
if (speed > maxSpeed)
|
{
|
speed = maxSpeed;
|
}
|
for (int i = 0; i < selectImags.Count; i++)
|
{
|
if (i != index)
|
selectImags[i].canvasGroup.alpha = 0;
|
else
|
selectImags[i].canvasGroup.alpha = 1;
|
}
|
SoundPlayer.Instance.PlayUIAudio(855);
|
yield return new WaitForSeconds(1f / speed);
|
|
turnStep++;
|
if (turnStep > selectImags.Count && index == model.stopIndex)
|
{
|
break;
|
}
|
}
|
|
selectImags[model.stopIndex].SetStartState();
|
selectImags[model.stopIndex].Play();
|
|
yield return new WaitForSeconds(0.6f);
|
selectImags[model.stopIndex].Stop();
|
|
startBtn.interactable = true;
|
closeBtn.interactable = true;
|
ShowAward();
|
|
string iconMark = string.Format("pushcoinword{0}_{1}", model.awardsTab[model.stopIndex][0], model.awardsTab[model.stopIndex][2]);
|
var iconConfig = IconConfig.Get(iconMark);
|
if (iconConfig != null)
|
{
|
wordObj.SetActive(true);
|
wordImg.SetSprite(iconMark);
|
wordImg.SetNativeSize();
|
}
|
|
|
Clock.AlarmAfter(1f, () => { wordObj.SetActive(false); });
|
}
|
|
|
void ShowAward()
|
{
|
int awardType = model.awardsTab[model.stopIndex][0];
|
|
int count = 0;
|
bool isEmptyCoin = false;
|
|
if (awardType == 3)
|
{
|
//红包
|
List<Item> items = new List<Item>();
|
|
foreach (var arr in model.redPackAward)
|
{
|
var moneyType = arr[0];
|
ulong countEx = (ulong)arr[1];
|
if (GeneralDefine.moneyDisplayIds.ContainsKey(moneyType))
|
{
|
items.Add(new Item()
|
{
|
id = GeneralDefine.moneyDisplayIds[moneyType],
|
countEx = countEx,
|
});
|
|
}
|
}
|
|
ItemLogicUtility.Instance.ShowGetItem(items, "", 0);
|
}
|
else if (awardType == 4)
|
{
|
//大奖
|
foreach (var arr in model.superAward)
|
{
|
string coinName = PushCoinModel.COINKEY + arr[0];
|
CreateCoin(coinName, arr[1]);
|
}
|
}
|
else
|
{
|
if (UI3DPushCoinStage.Instance.coinParent.childCount == 0)
|
{
|
foreach (var key in model.modelCoinInfo.Keys)
|
{
|
string coinName = PushCoinModel.COINKEY + key;
|
count = model.modelCoinInfo[key];
|
if (count > 0)
|
CreateCoin(coinName, count);
|
}
|
isEmptyCoin = true;
|
}
|
else
|
{
|
string coinName = PushCoinModel.COINKEY + model.awardsTab[model.stopIndex][0];
|
count = model.awardsTab[model.stopIndex][2];
|
CreateCoin(coinName, count);
|
}
|
}
|
|
|
if (isEmptyCoin && !PlayerDatas.Instance.baseData.IsActive90Off)
|
{
|
isEmptyCoin = false;
|
//第一次抽奖 直接出一折扣券
|
var coin90off = UIUtility.CreateWidget(PushCoinModel.COINKEY + 4, PushCoinModel.COINKEY + 4);
|
coin90off.transform.SetParentEx(UI3DPushCoinStage.Instance.coinParent, new Vector3(UnityEngine.Random.Range(-1f, 1f), -0.5f, -0.5f), Quaternion.identity, new Vector3(2.5f, 0.16f, 2.5f));
|
ShowSurpriseCard(0);
|
}
|
//记录所有币的位置
|
model.NoteCoin();
|
}
|
|
void CreateCoin(string coinName, int count, float posz = 0)
|
{
|
for (int i = 0; i < count; i++)
|
{
|
var coin = UIUtility.CreateWidget(coinName, coinName);
|
//x轴随机位置
|
var pos = Vector3.zero + new Vector3(UnityEngine.Random.Range(-3f, 3f), UnityEngine.Random.Range(-0.2f, 0.5f), posz);
|
coin.transform.SetParentEx(UI3DPushCoinStage.Instance.coinParent, pos, Quaternion.identity, new Vector3(1, 0.08f, 1));
|
|
}
|
SoundPlayer.Instance.PlayUIAudio(853);
|
}
|
|
|
|
void RestoreCoin()
|
{
|
if (UI3DPushCoinStage.Instance.coinParent.childCount != 0)
|
return;
|
|
|
//多设备的情况下,如果和服务器数量不一致,重新掉落
|
int goldCnt = model.GetCoinCount(1);
|
int localGoldCnt = 0;
|
var josnStr = LocalSave.GetString(PlayerDatas.Instance.baseData.PlayerID + PushCoinModel.COINKEY + 1);
|
List <float> objsInfo = new List<float>();
|
if (string.IsNullOrEmpty(josnStr))
|
{
|
localGoldCnt = 0;
|
}
|
else
|
{
|
try
|
{
|
objsInfo = JsonMapper.ToObject<List<float>>(josnStr);
|
localGoldCnt = objsInfo.Count/6;
|
}
|
catch (Exception e)
|
{
|
Debug.LogError("金币数据异常:" + e.Message);
|
localGoldCnt = 0;
|
}
|
}
|
if (localGoldCnt != goldCnt)
|
{
|
CreateCoin(PushCoinModel.COINKEY + 1, goldCnt, -1.8f);
|
Debug.Log("重建金币:" + goldCnt);
|
}
|
else if (localGoldCnt != 0)
|
{
|
for (int i = 0; i < objsInfo.Count; i += 6)
|
{
|
var coin = UIUtility.CreateWidget(PushCoinModel.COINKEY + 1, PushCoinModel.COINKEY + 1);
|
coin.transform.SetParentEx(UI3DPushCoinStage.Instance.coinParent, new Vector3(objsInfo[i], objsInfo[i + 1], objsInfo[i + 2]),
|
Quaternion.Euler(objsInfo[i + 3], objsInfo[i + 4], objsInfo[i + 5]), new Vector3(1, 0.08f, 1));
|
}
|
|
}
|
|
int xyCnt = model.GetCoinCount(2);
|
int localXyCnt = 0;
|
josnStr = LocalSave.GetString(PlayerDatas.Instance.baseData.PlayerID + PushCoinModel.COINKEY + 2);
|
objsInfo.Clear();
|
if (string.IsNullOrEmpty(josnStr))
|
{
|
localXyCnt = 0;
|
}
|
else
|
{
|
try
|
{
|
objsInfo = JsonMapper.ToObject<List<float>>(josnStr);
|
localXyCnt = objsInfo.Count/6;
|
}
|
catch (Exception e)
|
{
|
Debug.LogError("仙玉币数据异常:" + e.Message);
|
localXyCnt = 0;
|
}
|
}
|
if (localXyCnt != xyCnt)
|
{
|
CreateCoin(PushCoinModel.COINKEY + 2, xyCnt, -1.8f);
|
Debug.Log("重建仙玉币:" + xyCnt);
|
}
|
else if (localXyCnt != 0)
|
{
|
for (int i = 0; i < objsInfo.Count; i += 6)
|
{
|
var coin = UIUtility.CreateWidget(PushCoinModel.COINKEY + 2, PushCoinModel.COINKEY + 2);
|
coin.transform.SetParentEx(UI3DPushCoinStage.Instance.coinParent, new Vector3(objsInfo[i], objsInfo[i + 1], objsInfo[i + 2]),
|
Quaternion.Euler(objsInfo[i + 3], objsInfo[i + 4], objsInfo[i + 5]), new Vector3(1, 0.08f, 1));
|
}
|
}
|
|
if (!PlayerDatas.Instance.baseData.IsActive90Off)
|
{
|
int discountPosZ = QuickSetting.Instance.GetQuickSettingValue<int>(QuickSetting.QuickSettingType.PushCoin, 2);
|
float posZ = -2f;
|
if (discountPosZ != 0)
|
posZ = Mathf.Max(-2.4f, 0 - discountPosZ / 100f);
|
josnStr = LocalSave.GetString(PlayerDatas.Instance.baseData.PlayerID + PushCoinModel.COINKEY + 4);
|
if (string.IsNullOrEmpty(josnStr) && UI3DPushCoinStage.Instance.coinParent.childCount != 0)
|
{
|
//非首次,又找不到至尊币
|
var coin90off = UIUtility.CreateWidget(PushCoinModel.COINKEY + 4, PushCoinModel.COINKEY + 4);
|
coin90off.transform.SetParentEx(UI3DPushCoinStage.Instance.coinParent, new Vector3(UnityEngine.Random.Range(-1f, 1f), -1.3f, posZ), Quaternion.identity, new Vector3(2.5f, 0.16f, 2.5f));
|
Debug.Log("重建打折币");
|
}
|
else
|
{
|
objsInfo = JsonMapper.ToObject<List<float>>(josnStr);
|
if (objsInfo != null && objsInfo.Count >= 6)
|
{
|
//只有一个打折币
|
var coin = UIUtility.CreateWidget(PushCoinModel.COINKEY + 4, PushCoinModel.COINKEY + 4);
|
var height = Mathf.Min(objsInfo[1], -1.3f);
|
coin.transform.SetParentEx(UI3DPushCoinStage.Instance.coinParent, new Vector3(objsInfo[0], height, posZ),
|
Quaternion.Euler(objsInfo[3], objsInfo[4], objsInfo[5]), new Vector3(2.5f, 0.16f, 2.5f));
|
}
|
|
}
|
}
|
|
}
|
}
|
|
}
|
|
|
|
|