123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345 |
- using System;
- using System.Collections.Generic;
- using ET;
- using FairyGUI;
- using UI.MiniGame;
- using UnityEngine;
- namespace GFGGame
- {
- public class FlipGameView : BaseWindow
- {
- private UI_FlipGameUI _ui;
- //卡牌数量(配置)
- private int cardNum = 16;
- //当前卡牌数量
- private int currentCardNum = 16;
- //当前翻开卡牌
- //"state" 0:未翻开,1:已翻开,2:已消除
- private List<Dictionary<string, int>> cardList = new List<Dictionary<string, int>>();
- //消除所需数量(配置)
- private int needNum;
- //游戏结束时长(配置)
- private int gameTime = 120;
- //游戏当前时长倒计时
- private int currentGameTime = 120;
- //当前游戏时长
- private int timeIndex = 0;
- //进度条
- private float barTime = 120.0f;
- //行数
- private int rows = 0;
- //列数
- private int columns = 4;
- private int gameID;
- private CardGame gameDate;
- System.Random rand;
- //通关评价
- private List<int> CustemsNum = new List<int>()
- {
- 10,
- 30,
- 60,
- };
- private List<string> CustemsName = new List<string>()
- {
- "良好",
- "优秀",
- "卓越"
- };
- private List<ActivityOpenCfg> activityGameDate;
- //假数据
- private int[] cardArray = new int[] { 1,1, 2,2, 3, 3 , 4, 4 , 5, 5 , 6, 6 , 8, 8 , 8, 8 };
- Card _c1 = new Card();
- private float waitTime = 0.6f;
- public override void Dispose()
- {
- if (_ui != null)
- {
- _ui.Dispose();
- _ui = null;
- }
- base.Dispose();
- }
- protected override void OnInit()
- {
- base.OnInit();
- packageName = UI_FlipGameUI.PACKAGE_NAME;
- _ui = UI_FlipGameUI.Create();
- this.viewCom = _ui.target;
- isfullScreen = true;
- _ui.m_cardList.itemRenderer = ListCardItem;
- _ui.m_back.onClick.Add(OnClickBtnBack);
- UIObjectFactory.SetPackageItemExtension("ui://MiniGame/CardComponent", typeof(Card));
- }
- protected override void OnShown()
- {
- base.OnShown();
- if ((this.viewData as object[]).Length != 0 && this.viewData != null)
- {
- gameID = (int)(this.viewData as object[])[0];
- }
- else
- {
- gameID = 10001;
- }
- gameDate = CardGameArray.Instance.GetCfg(gameID);
- UpdateView();
- UpdateList();
- }
- protected override void OnHide()
- {
- _ui.m_cardList.RemoveChildrenToPool();
- base.OnHide();
- cardList.Clear();
- Timers.inst.Remove(UpdateTime);
- Timers.inst.Remove(canTouch);
- Timers.inst.Remove(UpdateCard);
- Timers.inst.Remove(UpdateBar);
- }
- private void OnClickBtnBack()
- {
- Timers.inst.Remove(UpdateTime);
- Timers.inst.Remove(UpdateBar);
- string exitTip;
- if(gameDate.bonusLoseArr.Length == 0)
- {
- exitTip = "退出游戏不保存进度,不扣除任何次数和道具,是否退出?";
- }
- else
- {
- exitTip = "退出游戏会按失败结算,获得80%的奖励,是否退出?";
- }
- AlertUI.Show(exitTip)
- .SetLeftButton(true, "取消", (object data) =>
- {
- Timers.inst.Add(1.0f, 0, UpdateTime);
- Timers.inst.Add(0.1f, 0, UpdateBar);
- })
- .SetRightButton(true, "确定", async (object data) =>
- {
- var result = await MiniGameProxy.ReqMiniGameEnd(gameID, gameDate.type, timeIndex, false, activityGameDate[0].id);
- if (!result) return;
- this.Hide();
- });
- }
- private void ListCardItem(int index, GObject item)
- {
- UI_cardItem cardItem = UI_cardItem.Proxy(item);
- Dictionary<string, int> itemInfo = new Dictionary<string, int>();
- //备注的后续都读表
- //cardItem.m_icon.url = "";
- if (cardItem.target.data == null)
- {
- cardItem.target.onClick.Add(OnClickCardItem);
- }
- _c1 = (Card)cardItem.m_card;
- _c1.opened = false;
- _c1.SetPerspective();
- itemInfo.Add("index", index);
- itemInfo.Add("id", cardArray[index]);
- itemInfo.Add("state", 0);
- cardItem.m_id.text = itemInfo["id"].ToString();
- cardItem.target.data = itemInfo;
- UI_cardItem.ProxyEnd();
- }
- private void UpdateView()
- {
- gameTime = CardGameArray.Instance.GetCfg(gameID).time;
- for(int i = 0;i<3;i++)
- {
- switch (i)
- {
- case 0:
- CustemsNum[0] = gameDate.timeStar1;
- break;
- case 1:
- CustemsNum[1] = gameDate.timeStar2;
- break;
- case 2:
- CustemsNum[2] = gameDate.timeStar3;
- break;
- }
- }
- timeIndex = 0;
- currentGameTime = gameTime;
- currentCardNum = cardNum;
- barTime = (float)gameTime;
- needNum = 2;
- activityGameDate = ActivityOpenCfgArray.Instance.GetCfgsBytype(ConstLimitTimeActivityType.ActLimitStlyc);
- rand = new System.Random();
- //洗牌
- RandomCardList(cardArray, cardArray.Length);
- //
- _ui.m_timeText.text = string.Format("{0}", currentGameTime);
- _ui.m_ScareBar.max = gameTime;
- _ui.m_ScareBar.min = 0;
- _ui.m_ScareBar.value = gameTime;
- _ui.m_star1.SetPosition(((float)(gameTime - CustemsNum[0])/(float)gameTime) * _ui.m_ScareBar.width + 70,_ui.m_star1.position.y, _ui.m_star1.position.z);
- _ui.m_star2.SetPosition(((float)(gameTime - CustemsNum[1]) / (float)gameTime) * _ui.m_ScareBar.width, _ui.m_star1.position.y, _ui.m_star1.position.z);
- _ui.m_star3.SetPosition(((float)(gameTime - CustemsNum[2]) / (float)gameTime) * _ui.m_ScareBar.width, _ui.m_star1.position.y, _ui.m_star1.position.z);
-
- //计时器
- Timers.inst.Add(1.0f, 0, UpdateTime);
- //进度条计时器
- Timers.inst.Add(0.1f, 0, UpdateBar);
- }
- private void UpdateList()
- {
- _ui.m_cardList.columnCount = columns;
- _ui.m_cardList.numItems = cardNum;
- }
- private void OnClickCardItem(EventContext context)
- {
- GObject cardItem = context.sender as GObject;
- Dictionary<string, int> cardInfo = (Dictionary<string, int>)cardItem.data;
- UI_cardItem item = UI_cardItem.Proxy(_ui.m_cardList.GetChildAt(cardInfo["index"]));
- Card card = (Card)item.m_card;
- if (cardList.Count == 0)
- {
- //当前翻开列表为空,直接处理翻开,添加
- cardInfo["state"] = 1;
- //翻牌
- card.Turn();
- _ui.m_cardList.touchable = false;
- Timers.inst.Add(waitTime, 1, canTouch);
- //
- cardList.Add(cardInfo);
- }
- else
- {
- bool isLast = false;
- for (int i = 0; i < cardList.Count; i++)
- {
- //点击到已翻开的就翻回去,
- if (cardInfo["index"] == cardList[i]["index"])
- {
- //翻牌
- card.Turn();
- _ui.m_cardList.touchable = false;
- Timers.inst.Add(waitTime, 1, canTouch);
- //
- cardList.RemoveAt(i);
- isLast = true;
- break;
- }
- }
- if (!isLast)
- {
- //翻牌
- card.Turn();
- _ui.m_cardList.touchable = false;
- cardInfo["state"] = 1;
- cardList.Add(cardInfo);
- Timers.inst.Add(waitTime, 1, UpdateCard);
- }
- }
- UI_cardItem.ProxyEnd();
- }
- //这里是处理已翻开的数量
- private void UpdateCard(object param = null)
- {
- bool isClear = true;
- for (int i = 1; i < cardList.Count; i++)
- {
- if(cardList[0]["id"] != cardList[i]["id"])
- {
- isClear = false;
- break;
- }
- isClear = true;
- }
- if(isClear)
- {
- if (cardList.Count == needNum)
- {
- for (int i = 0; i < cardList.Count; i++)
- {
- cardList[i]["state"] = 2;
- //消除
- _ui.m_cardList.GetChildAt(cardList[i]["index"]).visible = false;
- }
- currentCardNum -= cardList.Count;
- cardList.Clear();
- if(currentCardNum <= 0)
- {
- Gameover(true);
- }
- }
- _ui.m_cardList.touchable = true;
- }
- else
- {
- _ui.m_cardList.touchable = false;
- //= UI_cardItem.Proxy(_ui.m_cardList.GetChildAt(cardList[0]["index"]));
- for (int i = 0; i < cardList.Count; i++)
- {
- UI_cardItem item = UI_cardItem.Proxy(_ui.m_cardList.GetChildAt(cardList[i]["index"]));
- //翻回去
- Card card = (Card)item.m_card;
- card.Turn();
- //card = null;
- UI_cardItem.ProxyEnd();
- }
- Timers.inst.Add(waitTime, 1, canTouch);
- cardList.Clear();
- }
- }
- private void UpdateTime(object param = null)
- {
- timeIndex++;
- currentGameTime--;
- //_ui.m_ScareBar.value = currentGameTime;
- _ui.m_timeText.text = string.Format ("{0}", currentGameTime);
- if(currentGameTime <= 0)
- {
- Gameover(false);
- }
- }
- private void UpdateBar(object param = null)
- {
- barTime -= 0.1f;
- _ui.m_ScareBar.value = barTime;
- }
- private void canTouch(object param = null)
- {
- _ui.m_cardList.touchable = true;
- }
- private void Gameover(bool isPass = false)
- {
- Timers.inst.Remove(UpdateTime);
- Timers.inst.Remove(UpdateBar);
- ViewManager.Show<ResultTipsView>(new object[] { isPass, gameDate.type, timeIndex, gameDate.id });
- }
- //洗牌
- private void RandomCardList(int[] array, int length)
- {
- int index;
- int value;
- for (int i = length - 1; i >= 0; i--)
- {
- index = rand.Next(0, i + 1);
- value = array[i];
- array[i] = array[index];
- array[index] = value;
- }
- }
- }
- }
|