123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463 |
- 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 = 4;
- //列数
- 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 List<int> cardArray = new List<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.0f;
- private bool StartHit = true;
- private bool firstHit = true;
- private bool canHit = true;
- private EffectUI _effectUI1;
- private EffectUI _effectUI2;
- private List<EffectUI> _effectFlipList = new List<EffectUI>();
- private List<EffectUI> _effectRemoveList = new List<EffectUI>();
- public override void Dispose()
- {
- EffectUIPool.Recycle(_effectUI1);
- _effectUI1 = null;
- EffectUIPool.Recycle(_effectUI2);
- _effectUI2 = null;
- DestroyObjectFromView();
- 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;
- isReturnView = true;
- _ui.m_cardList.itemRenderer = ListCardItem;
- _ui.m_back.onClick.Add(OnClickBtnBack);
- UIObjectFactory.SetPackageItemExtension("ui://MiniGame/CardComponent", typeof(Card));
- _effectUI1 = EffectUIPool.CreateEffectUI(_ui.m_barEffect, "ui_Activity", "YXJY_Game_JinDu");
- _effectUI2 = EffectUIPool.CreateEffectUI(_ui.m_titleEffec, "ui_Activity", "YXJY_Game_Text");
- }
- 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()
- {
- DestroyObjectFromView();
- cardList.Clear();
- Timers.inst.Remove(UpdateTime);
- Timers.inst.Remove(UpdateBar);
- Timers.inst.Remove(UpdateHit);
- Timers.inst.Remove(StartTime);
- Timers.inst.Remove(UpdateOneHit);
- base.OnHide();
- }
- private void DestroyObjectFromView()
- {
- for (int i = 0; i < _effectFlipList.Count; i++)
- {
- EffectUIPool.Recycle(_effectFlipList[i]);
- _effectFlipList[i] = null;
- }
- _effectFlipList.Clear();
- for (int i = 0; i < _effectRemoveList.Count; i++)
- {
- EffectUIPool.Recycle(_effectRemoveList[i]);
- _effectRemoveList[i] = null;
- }
- _effectRemoveList.Clear();
- }
- 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, true);
- if (!result) return;
- this.Hide();
- }).SetShowCheck(false);
- }
- private void ListCardItem(int index, GObject item)
- {
- UI_cardItem cardItem = UI_cardItem.Proxy(item);
- Dictionary<string, int> itemInfo = new Dictionary<string, int>();
- cardItem.target.visible = true;
- cardItem.target.alpha = 1;
- EffectUI _effectUI1 = EffectUIPool.CreateEffectUI(cardItem.m_flipEffect, "ui_Activity", "YXJY_Game_OpenCard");
- _effectFlipList.Add(_effectUI1);
- cardItem.m_flipEffect.visible = false;
- EffectUI _effectUI2 = EffectUIPool.CreateEffectUI(cardItem.m_removeEffect, "ui_Activity", "YXJY_Game_CardDie");
- _effectRemoveList.Add(_effectUI2);
- cardItem.m_removeEffect.visible = false;
- cardItem.m_card.GetChild("icon").icon = ResPathUtil.GetMiniGamePicPath(CardCfgArray.Instance.dataArray[cardArray[index]].resName,"FlipGame");
- if (cardItem.target.data == null)
- {
- cardItem.target.onClick.Add(OnClickCardItem);
- }
- Card _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;
- }
- }
- StartHit = true;
- firstHit = true;
- canHit = true;
- timeIndex = 0;
- currentGameTime = gameTime;
- cardNum = columns * rows;
- currentCardNum = cardNum;
- barTime = (float)gameTime;
- needNum = 2;
- activityGameDate = ActivityOpenCfgArray.Instance.GetCfgsBytype(ConstLimitTimeActivityType.ActLimitStlyc);
- rand = new System.Random();
- //洗牌
- RandomGetNum();
- RandomCardList(cardArray, cardArray.Count);
- //
- _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 + 10, _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);
- _ui.m_title.visible = false;
- _ui.m_titleEffec.visible = true;
- _ui.m_barEffect.x = _ui.m_ScareBar.width;
- _ui.m_cardList.touchable = true;
- //计时器
- 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)
- {
- if (!canHit)
- {
- return;
- }
- 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"]));
- if (StartHit)
- {
- StartHit = false;
- Timers.inst.Add(1.0f, 1, StartTime);
- }
- Card card = (Card)item.m_card;
- bool isSame = false;
- for (int i = 0; i < cardList.Count; i++)
- {
- if (cardInfo["index"] == cardList[i]["index"] && firstHit)
- {
- cardInfo["state"] = 0;
- //翻牌
- card.Turn();
- _ui.m_cardList.touchable = false;
- canHit = false;
- cardList.RemoveAt(0);
- Timers.inst.Add(0.5f, 1,UpdateOneHit);
- isSame = true;
- break;
- }
- else
- {
- if (cardInfo["index"] == cardList[i]["index"])
- {
- isSame = true;
- break;
- }
- }
- }
- firstHit = false;
- //翻牌
- if (!isSame)
- {
- cardInfo["state"] = 1;
- card.Turn();
- item.m_flipEffect.visible = true;
- cardList.Add(cardInfo);
- }
- UI_cardItem.ProxyEnd();
- }
- private void StartTime(object param = null)
- {
- _ui.m_cardList.touchable = false;
- canHit = false;
- UpdateCard();
- }
- //这里是处理已翻开的数量
- private void UpdateCard()
- {
- Timers.inst.Remove(StartTime);
- List<Dictionary<string, int>> removeList = new List<Dictionary<string, int>>();
- for (int i = 1; i < cardList.Count; i += 2)
- {
- if (cardList[i]["id"] == cardList[i - 1]["id"])
- {
- cardList[i]["state"] = 2;
- //消除
- UI_cardItem item1 = UI_cardItem.Proxy(_ui.m_cardList.GetChildAt(cardList[i]["index"]));
- item1.m_t0.Play();
- UI_cardItem.ProxyEnd();
- UI_cardItem item2 = UI_cardItem.Proxy(_ui.m_cardList.GetChildAt(cardList[i - 1]["index"]));
- item2.m_t0.Play();
- UI_cardItem.ProxyEnd();
- currentCardNum -= 2;
- removeList.Add(cardList[i - 1]);
- removeList.Add(cardList[i]);
- }
- }
- if (currentCardNum <= 0)
- {
- Gameover(true);
- return;
- }
- //先处理消除的列表
- for (int i = 0; i < removeList.Count; i++)
- {
- cardList.Remove(removeList[i]);
- }
- removeList.Clear();
- //后处理翻牌的列表
- for (int i = 1; i < cardList.Count; i++)
- {
- UI_cardItem item = UI_cardItem.Proxy(_ui.m_cardList.GetChildAt(cardList[i - 1]["index"]));
- //翻回去
- Card card = (Card)item.m_card;
- card.Turn();
- item.m_flipEffect.visible = false;
- cardList[i - 1]["state"] = 0;
- UI_cardItem.ProxyEnd();
- if (cardList.Count % 2 == 0 && i == cardList.Count - 1)
- {
- UI_cardItem carditem = UI_cardItem.Proxy(_ui.m_cardList.GetChildAt(cardList[i]["index"]));
- //翻回去
- Card cardLast = (Card)carditem.m_card;
- cardLast.Turn();
- carditem.m_flipEffect.visible = false;
- cardList[i]["state"] = 0;
- UI_cardItem.ProxyEnd();
- }
- }
- if (cardList.Count % 2 == 0)
- {
- cardList.Clear();
- }
- else
- {
- int j = cardList.Count;
- for (int i = 0; i < j - 1; i++)
- {
- cardList.RemoveAt(0);
- }
- }
- Timers.inst.Add(0.6f, 1, UpdateHit);
- }
- private void UpdateHit(object param = null)
- {
- StartHit = true;
- firstHit = true;
- canHit = true;
- _ui.m_cardList.touchable = true;
- Timers.inst.Remove(UpdateHit);
- }
- private void UpdateOneHit(object param = null)
- {
- canHit = true;
- _ui.m_cardList.touchable = true;
- Timers.inst.Remove(UpdateOneHit);
- }
- 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;
- _ui.m_barEffect.x -= (880.0f / (float)(gameTime * 10));
- }
- 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(List<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;
- }
- }
- private void RandomGetNum()
- {
- int numCount = CardCfgArray.Instance.dataArray.Length;
- List<int> tArray = new List<int>();
- for (int i = 0; i < numCount; i++)
- {
- tArray.Add(i);
- }
- RandomCardList(tArray, numCount);
- for (int i = 0; i < cardNum; i += 4)
- {
- cardArray[i] = tArray[i];
- cardArray[i + 1] = tArray[i];
- cardArray[i + 2] = tArray[i];
- cardArray[i + 3] = tArray[i];
- }
- }
- }
- }
|