using System; using System.Collections.Generic; using ET; using FairyGUI; using UI.MiniGame; using UnityEngine; namespace GFGGame { public class FlipGameView : BaseView { private UI_FlipGameUI _ui; //卡牌数量(配置) private int cardNum = 16; //当前卡牌数量 private int currentCardNum = 16; //当前翻开卡牌 //"state" 0:未翻开,1:已翻开,2:已消除 private List> cardList = new List>(); //消除所需数量(配置) 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; System.Random rand; //通关评价 private List CustemsNum = new List() { 10, 30, 60, }; private List CustemsName = new List() { "良好", "优秀", "卓越" }; //假数据 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(); 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); AlertUI.Show("是否退出游戏") .SetLeftButton(true, "取消", (object data) => { Timers.inst.Add(1.0f, 0, UpdateTime); Timers.inst.Add(0.1f, 0, UpdateBar); }) .SetRightButton(true, "确定", (object data) => { this.Hide(); }); } private void ListCardItem(int index, GObject item) { UI_cardItem cardItem = UI_cardItem.Proxy(item); Dictionary itemInfo = new Dictionary(); //备注的后续都读表 //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() { timeIndex = 0; currentGameTime = gameTime; currentCardNum = cardNum; barTime = (float)gameTime; needNum = 2; 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)CustemsNum[0]/(float)gameTime)*_ui.m_ScareBar.width,_ui.m_star1.position.y, _ui.m_star1.position.z); _ui.m_star2.SetPosition(((float)CustemsNum[1] / (float)gameTime) * _ui.m_ScareBar.width, _ui.m_star1.position.y, _ui.m_star1.position.z); _ui.m_star3.SetPosition(((float)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 cardInfo = (Dictionary)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); if (isPass) { ViewManager.Show(new object[] { isPass, typeof(FlipGameView).FullName, timeIndex }); } else { ViewManager.Show(new object[] { isPass, typeof(FlipGameView).FullName }); } } //洗牌 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; } } } }