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 items = new List(); private PickUpGame _cfg; private int _activityID; private int _countTime; private int _score; private Dictionary _collectDict; private Vector3 _catcherInitPos; private bool _firstIn = true; private bool _gamePause = false; private float _timer; private Dictionary _effectUIDic = new Dictionary(); 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); } /// /// 初始化元宵的FGUI组件 /// /// 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(); } /// /// 从对象池中取出对象 /// /// /// 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); } /// /// 生成元宵 /// /// public void CreateItems(object param) { if (!isShowing) { return; } int num = _cfg.resIdArr.Length; List 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)); } } /// /// 随机路径 /// /// /// private List GetRandomRoads(int count) { List resultList = new List(); List list = new List(); 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; } /// /// 接住元宵 /// /// 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, Screen.width + _ui.m_catcher.target.width, 0); } private void StartGame() { CreateItems(null); UpdateTime(null); Timers.inst.Add(1, 0, UpdateTime); Timers.inst.AddUpdate(UpdateGame); } 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(); ViewManager.Show("加载中..."); // 请求游戏结束协议 await MiniGameProxy.ReqMiniGameEnd(_cfg.id, _cfg.type, 0, win, _activityID, false); ViewManager.Hide(); if (win) { ViewManager.Show(_cfg); } else { ViewManager.Show(); } } private void InitData() { _gamePause = false; _timer = 0; _countTime = _cfg.time; _score = 0; _collectDict = new Dictionary(); items = new List(); } 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); ClearYuanXiaoList(); } private void ResetGame() { HideGameView(); ShowGameView(); } private void OnBtnBack() { // 暂停游戏 PauseGame(); AlertMiniGame alertInfo = new AlertMiniGame(); alertInfo.desc = "确认是否退出游戏?退出游戏直接按失败结算"; alertInfo.btnConfirmClickAction = () => { Hide(); }; alertInfo.btnCancelClickAction = () => { ContinueGame(); }; ViewManager.Show(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); } } /// /// 计时器,每隔一段时间创建掉落的元宵 /// /// 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")); } } }