123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520 |
- using System.Collections;
- using UnityEngine;
- using UI.ActivityGetYuanXiao;
- using FairyGUI;
- using System.Collections.Generic;
- using System.Threading.Tasks;
- namespace GFGGame
- {
- public class ActivityGetYuanXiaoView : BaseView
- {
- private UI_ActivityGetYuanXiaoUI _ui;
- private List<YuanXiaoItem> items = new List<YuanXiaoItem>();
- private PickUpGame _cfg;
- private int _activityID;
- private int _countTime;
- private int _score;
- private Dictionary<int, int> _collectDict;
- private Vector3 _catcherInitPos;
- private bool _firstIn = true;
- private bool _gamePause = false;
- private float _timer;
- private Dictionary<string, EffectUI> _effectUIDic = new Dictionary<string, EffectUI>();
- public override void Dispose()
- {
- // Clear Effect
- foreach (var v in _effectUIDic)
- {
- EffectUIPool.Recycle(v.Value);
- }
- _effectUIDic.Clear();
- if (_ui != null)
- {
- _ui.Dispose();
- }
- _ui = null;
- base.Dispose();
- }
- protected override void OnInit()
- {
- base.OnInit();
- packageName = UI_ActivityGetYuanXiaoUI.PACKAGE_NAME;
- _ui = UI_ActivityGetYuanXiaoUI.Create();
- viewCom = _ui.target;
- isReturnView = true;
- _ui.m_loaBg.url = ResPathUtil.GetBgImgPath("hd_yx_bg_2");
- _ui.m_collectList.itemRenderer = CollectListRenderer;
- _ui.m_btnBack.onClick.Add(OnBtnBack);
- AddEffect();
- }
- protected override void OnShown()
- {
- base.OnShown();
- object[] arr = viewData as object[];
- _cfg = (PickUpGame)arr[0];
- _activityID = (int)arr[1];
- if (_firstIn)
- {
- _firstIn = false;
- _catcherInitPos = _ui.m_catcher.target.position;
- }
- ShowGameView();
- }
- protected override void OnHide()
- {
- base.OnHide();
- HideGameView();
- }
- protected override void AddEventListener()
- {
- base.AddEventListener();
- EventAgent.AddEventListener(ConstMessage.ACTIVITY_GETYUANXIAO_RESET, ResetGame);
- }
- protected override void RemoveEventListener()
- {
- base.RemoveEventListener();
- EventAgent.RemoveEventListener(ConstMessage.ACTIVITY_GETYUANXIAO_RESET, ResetGame);
- }
- /// <summary>
- /// 初始化元宵的FGUI组件
- /// </summary>
- /// <returns></returns>
- YuanXiaoItem InitYuanXiaoItemUICom()
- {
- GComponent gcom = UIPackage.CreateObject("ActivityGetYuanXiao", "YuanXiaoItem").asCom;
- _ui.m_YuanXiaoParent.target.AddChild(gcom);
- YuanXiaoItem item = new YuanXiaoItem();
- item.CreateItem(gcom, _ui.m_catcher.m_catcher, this);
- return item;
- }
- public void ClearYuanXiaoList()
- {
- for (int i = 0; i < items.Count; i++)
- {
- items[i].Destroy();
- }
- items.Clear();
- }
- /// <summary>
- /// 从对象池中取出对象
- /// </summary>
- /// <param name="type"></param>
- /// <returns></returns>
- public YuanXiaoItem GetYuanXiaoItem(int type)
- {
- YuanXiaoItem item = items.Find(x => x.GetVisible() == false);
- if (item == null)
- {
- item = InitYuanXiaoItemUICom();
- }
- item.Init(type);
- return item;
- }
- public void RestoreYuanXiaoItem(YuanXiaoItem item)
- {
- item.SetVisible(false);
- }
- /// <summary>
- /// 生成元宵
- /// </summary>
- /// <param name="param"></param>
- public void CreateItems(object param)
- {
- if (!isShowing)
- {
- return;
- }
- int num = _cfg.resIdArr.Length;
- List<int> roadIndexs = GetRandomRoads(num);
- for (int i = 0; i < num; i++)
- {
- PickUpCfg pickUpCfg = PickUpCfgArray.Instance.GetCfgsByid(_cfg.resArr[0])[_cfg.resIdArr[i] - 1];
- float time = pickUpCfg.speed * 1f / 1000;
- YuanXiaoItem item = GetYuanXiaoItem(_cfg.resIdArr[i]);
- items.Add(item);
- int score = pickUpCfg.score;
- item.SetScore(score);
- Vector3 startPos = _ui.target.GetChild("start" + roadIndexs[i]).position;
- Vector3 endPos = startPos;
- endPos.y = _ui.m_end0.y;
- item.Move(startPos, endPos, time, () => RestoreYuanXiaoItem(item));
- }
- }
- /// <summary>
- /// 随机路径
- /// </summary>
- /// <param name="count"></param>
- /// <returns></returns>
- private List<int> GetRandomRoads(int count)
- {
- List<int> resultList = new List<int>();
- List<int> list = new List<int>();
- for (int i = 0; i < 4; i++)
- {
- list.Add(i);
- }
- for (int j = 0; j < count; j++)
- {
- int randomIndex = Random.Range(0, list.Count);
- resultList.Add(list[randomIndex]);
- list.RemoveAt(randomIndex);
- }
- return resultList;
- }
- /// <summary>
- /// 接住元宵
- /// </summary>
- /// <param name="item"></param>
- public void GetYuanXiao(YuanXiaoItem item)
- {
- // 回收组件
- RestoreYuanXiaoItem(item);
- if (_countTime == 0)
- {
- return;
- }
- // 显示得分标志
- _score += item.GetScore();
- _ui.m_score.text = _score.ToString();
- if (_ui.m_c1.selectedIndex == 0)
- {
- UpdateCollectProgress(item.GetYuanXiaoType());
- }
- else
- {
- UpdateScore();
- }
- ShowFlyScore(item.GetScore());
- _ui.m_catcher.m_getEffect.visible = false;
- _ui.m_catcher.m_getEffect.visible = true;
- }
- private void ShowFlyScore(int score)
- {
- UI_flyScore flyItem = UI_flyScore.Proxy(_ui.m_catcher.m_flyscore.target);
- flyItem.m_score.text = score.ToString();
- flyItem.m_t0.Stop();
- _ui.m_catcher.m_flyscore.target.visible = true;
- flyItem.m_t0.Play(() =>
- {
- _ui.m_catcher.m_flyscore.target.visible = false;
- });
- UI_flyScore.ProxyEnd();
- }
- public void SetDragRect()
- {
- _ui.m_catcher.target.visible = true;
- _ui.m_catcher.target.draggable = true;
- //_ui.m_catcher.target.dragBounds = new Rect(-_ui.m_catcher.target.width / 2, _ui.m_catcher.target.y, Stage.inst.width + _ui.m_catcher.target.width, 0);
- Vector2 dragLeftPos = _ui.m_startLeft.LocalToGlobal(Vector2.zero) / (UIContentScaler.scaleFactor);
- Vector2 dragRightPos = _ui.m_startRight.LocalToGlobal(Vector2.zero) / (UIContentScaler.scaleFactor);
- _ui.m_catcher.target.dragBounds = new Rect(dragLeftPos.x, _ui.m_catcher.target.y, _ui.m_startRight.x - _ui.m_startLeft.x, 0);
- }
- private void StartGame()
- {
- CreateItems(null);
- UpdateTime(null);
- Timers.inst.Add(1, 0, UpdateTime);
- Timers.inst.AddUpdate(UpdateGame);
- Timers.inst.Add(1, 0, CheckActivityEnd);
- }
- private async void PlayStartAnim()
- {
- GRoot.inst.touchable = false;
- _ui.m_startEffect.visible = false;
- _ui.m_startEffect.visible = true;
- await Task.Delay(820);
- GRoot.inst.touchable = true;
- StartGame();
- }
- private void UpdateTime(object param)
- {
- if (!isShowing)
- {
- return;
- }
- _ui.m_time.text = TimeUtil.FormattingTimeTo_mmss(_countTime * 1000);
- --_countTime;
- if (_countTime < 0)
- {
- Timers.inst.Remove(UpdateTime);
- bool win = true;
- if (_ui.m_c1.selectedIndex == 0)
- {
- foreach (var v in _collectDict)
- {
- if (v.Value > 0)
- {
- win = false;
- break;
- }
- }
- }
- else
- {
- if (_score < _cfg.targetScore)
- {
- win = false;
- }
- }
- GameEnd(win);
- }
- }
- private async void GameEnd(bool win)
- {
- //_ui.m_catcher.target.draggable = false;
- // 游戏结束时隐藏catcher,同时也作为游戏结束的标志
- _ui.m_catcher.target.visible = false;
- HideGameView();
- if (win)
- {
- ViewManager.Show<ModalStatusView>("加载中...");
- // 请求游戏结束协议
- await MiniGameProxy.ReqMiniGameEnd(_cfg.id, _cfg.type, 0, true, _activityID, false);
- ViewManager.Hide<ModalStatusView>();
- ViewManager.Show<ActivityGetYuanXiaoSuccessView>(_cfg);
- }
- else
- {
- ViewManager.Show<ActivityGetYuanXiaoFailView>();
- }
- }
- private void InitData()
- {
- _gamePause = false;
- _timer = 0;
- _countTime = _cfg.time;
- _score = 0;
- _collectDict = new Dictionary<int, int>();
- items = new List<YuanXiaoItem>();
- }
- private void InitView()
- {
- _ui.m_startEffect.visible = false;
- _ui.m_catcher.m_getEffect.visible = false;
- _ui.m_catcher.m_flyscore.target.visible = false;
- _ui.m_catcher.m_c2.selectedIndex = _cfg.id > PickUpGameArray.Instance.dataArray[0].id ? 1 : 0;
- _ui.m_time.text = TimeUtil.FormattingTimeTo_mmss(_countTime * 1000);
- _ui.m_score.text = _score.ToString();
- _ui.m_catcher.target.position = _catcherInitPos;
- InitTarget();
- }
- private void InitTarget()
- {
- // 分数
- if (_cfg.targetIdArr.Length == 0)
- {
- _ui.m_targetScore.text = _cfg.targetScore.ToString();
- _ui.m_c1.selectedIndex = 1;
- UpdateScore();
- }
- // 收集
- else
- {
- for (int i = 0; i < _cfg.targetIdArr.Length; i++)
- {
- _collectDict.Add(_cfg.targetIdArr[i][0], _cfg.targetIdArr[i][1]);
- }
- _ui.m_c1.selectedIndex = 0;
- _ui.m_collectList.numItems = _cfg.targetIdArr.Length;
- }
- }
- private void UpdateScore()
- {
- _ui.m_progress.SetVar("cur", _score.ToString()).SetVar("target", _cfg.targetScore.ToString()).FlushVars();
- if (_ui.m_catcher.target.visible && _score >= _cfg.targetScore)
- {
- GameEnd(true);
- }
- }
- private void UpdateCollectProgress(int collectType)
- {
- if (!_collectDict.ContainsKey(collectType))
- {
- return;
- }
- if (_collectDict[collectType] > 0)
- {
- --_collectDict[collectType];
- }
- _ui.m_collectList.numItems = _cfg.targetIdArr.Length;
- foreach (var v in _collectDict)
- {
- if (v.Value > 0)
- {
- return;
- }
- }
- if (_ui.m_catcher.target.visible)
- {
- GameEnd(true);
- }
- }
- private void CollectListRenderer(int index, GObject item)
- {
- UI_targetItem target = UI_targetItem.Proxy(item);
- target.m_iconType.url = string.Format("ui://ActivityGetYuanXiao/yx_mb_{0}", _cfg.targetIdArr[index][0]);
- int num = _collectDict[_cfg.targetIdArr[index][0]];
- if (num == 0)
- {
- target.m_c1.selectedIndex = 1;
- }
- else
- {
- target.m_c1.selectedIndex = 0;
- target.m_num.text = num.ToString();
- }
- UI_targetItem.ProxyEnd();
- }
- private void ShowGameView()
- {
- InitData();
- InitView();
- SetDragRect();
- PlayStartAnim();
- }
- private void HideGameView()
- {
- Timers.inst.Remove(UpdateTime);
- Timers.inst.Remove(UpdateGame);
- Timers.inst.Remove(CheckActivityEnd);
- ClearYuanXiaoList();
- }
- private void ResetGame()
- {
- HideGameView();
- ShowGameView();
- }
- private void OnBtnBack()
- {
- // 暂停游戏
- PauseGame();
- AlertMiniGame alertInfo = new AlertMiniGame();
- alertInfo.desc = "退出游戏不保存进度,不扣除任何次数和道具,是否退出?";
- alertInfo.btnConfirmClickAction = () =>
- {
- Hide();
- };
- alertInfo.btnCancelClickAction = () =>
- {
- ContinueGame();
- };
- ViewManager.Show<AlertMiniGameView>(alertInfo);
- }
- private void PauseGame()
- {
- _gamePause = true;
- Timers.inst.Remove(UpdateTime);
- for (int i = 0; i < items.Count; i++)
- {
- items[i].SetPause(true);
- }
- }
- private void ContinueGame()
- {
- _gamePause = false;
- Timers.inst.Add(1, 0, UpdateTime);
- for (int i = 0; i < items.Count; i++)
- {
- items[i].SetPause(false);
- }
- }
- /// <summary>
- /// 计时器,每隔一段时间创建掉落的元宵
- /// </summary>
- /// <param name="param"></param>
- private void UpdateGame(object param)
- {
- if (_gamePause)
- {
- return;
- }
- _timer += Time.deltaTime * 1000;
- if (_timer >= _cfg.dropTime)
- {
- CreateItems(null);
- _timer = 0;
- }
- }
- private void AddEffect()
- {
- _effectUIDic.Add("YXJ_Catch", EffectUIPool.CreateEffectUI(_ui.m_catcher.m_getEffect, "ui_Activity", "YXJ_Catch"));
- _effectUIDic.Add("YXJ_text_start", EffectUIPool.CreateEffectUI(_ui.m_startEffect, "ui_Activity", "YXJ_text_start"));
- _effectUIDic.Add("YXJ_gameing_bg_tx", EffectUIPool.CreateEffectUI(_ui.m_bgEffect, "ui_Activity", "YXJ_gameing_bg_tx"));
- }
- private void CheckActivityEnd(object param)
- {
- if (!ActivityGetYuanXiaoDataManager.Instance.CheckOpen())
- {
- _ui.m_catcher.target.visible = false;
- // 活动结束时强行退回主界面
- ViewManager.Show<MainUIView>(null, true);
- ViewManager.DeleteViewStackCountDown("MainUIView");
- }
- }
- }
- }
|