using System; using System.Collections.Generic; using ET; using FairyGUI; using UI.MiniGame; using UnityEngine; namespace GFGGame { public class MergeGameView : BaseWindow { private UI_MergeGameUI _ui; private List> itemList = new List>(); private List> idList = new List>(); private int itemNum; private int maxLv; private System.Random rand; private int _gameID; private int _res; //当前拖拽的index; private int indexID; //开始拖拽的坐标 private float startX; private float startY; private int indexX; private int indexY; //当前任务id;后台获取数据 private int taskId; //当前点击id信息 private int materialID; private int propID = 3000020; public override void Dispose() { if (_ui != null) { _ui.Dispose(); _ui = null; } base.Dispose(); } protected override void OnInit() { base.OnInit(); packageName = UI_MergeGameUI.PACKAGE_NAME; _ui = UI_MergeGameUI.Create(); this.viewCom = _ui.target; isfullScreen = true; _ui.m_backBtn.onClick.Add(Hide); _ui.m_rewardGet.onClick.Add(SubmitTask); _ui.m_MergeTipBtn.onClick.Add(ShowMergeRouteTips); _ui.m_shopBtn.onClick.Add(ShowShopBtn); _ui.m_sellBtn.target.onClick.Add(OnClickSellBtn); _ui.target.onClick.Add(OnClickHideTips); _ui.m_btnProp.target.onClick.Add(OnItemTips); } protected override void OnShown() { base.OnShown(); //进入这个界面的时候向后台获取任务id和历史id列表 UpdateView(); UpdateList(); UpdateTask(); UpdateItemData(); } protected override void OnHide() { for (int i = 0; i < itemNum; i++) { for (int j = 0; j < itemNum; j++) { itemList[i][j].Dispose(); itemList[i][j] = null; } } idList.Clear(); itemList.Clear(); base.OnHide(); } private void UpdateData() { } private void UpdateView() { itemNum = 5; taskId = MiniGameDateManager.Instance.taskID; _res = MergeGameArray.Instance.GetCfg(taskId).resArr[0]; maxLv = 5; materialID = 0; rand = new System.Random(); _ui.m_rewardGet.visible = false; _ui.m_sellBtn.target.GetChild("sellGetNum").text = ""; //_ui.m_newMaterial.target.visible = false; _ui.m_name.text = ""; _ui.m_desc.text = ""; _ui.m_bg.url = ResPathUtil.GetBgImgPath("jbnf_xyxbj"); } private void UpdateList() { for (int i = 0; i < itemNum; i++) { itemList.Add(new List()); idList.Add(new List()); for (int j = 0; j < itemNum; j++) { idList[i].Add(j); itemList[i].Add(UIPackage.CreateObject(UI_MergeGameUI.PACKAGE_NAME, "MergeGameItemUI").asCom); _ui.m_rangeDown.AddChild(itemList[i][j]); //对两个按钮单独处理 if (i == itemNum - 1) { itemList[i][j].data = -1; idList[i][j] = -1; itemList[i][j].SetPosition((_ui.m_rangeDown.width / 10.0f) * (1 + (j * 2.0f)), (_ui.m_rangeDown.height / 10) * (1 + (i * 2.0f)), 0); if (j == 0) { itemList[i][j].GetChild("icon").icon = "ui://MiniGame/jbnf_doubao_icon"; itemList[i][j].onClick.Add(OnClickAFuBtn); continue; } if ( j == itemNum - 1) { itemList[i][j].GetChild("icon").icon = "ui://MiniGame/jbnf_afu_icon"; itemList[i][j].onClick.Add(OnClickDouBaoBtn); continue; } } //表示id;这里后续要改 idList[i][j] = 0; if (i == 1 && j == 2) { idList[i][j] = 1; } if(i== 2 && j== 2) { idList[i][j] = 1; } // itemList[i][j].data = idList[i][j]; itemList[i][j].draggable = true; itemList[i][j].onDragStart.Add(onDragStart); itemList[i][j].onDragEnd.Add(onDragEnd); itemList[i][j].onClick.Add(OnClickItem); itemList[i][j].dragBounds = new Rect(_ui.m_rangeTop.x + 90, _ui.m_rangeTop.y + 90, _ui.m_rangeTop.width, _ui.m_rangeTop.height); itemList[i][j].SetPosition( (_ui.m_rangeDown.width / 10.0f) * (1+ (j * 2.0f)), (_ui.m_rangeDown.height / 10) * (1+(i * 2.0f)) , 0); if (idList[i][j] == 0) { itemList[i][j].GetChild("icon").icon = ""; itemList[i][j].draggable = false; } else { itemList[i][j].GetChild("icon").icon = ResPathUtil.GetMiniGamePicPath(MergeCfgArray.Instance.GetCfgsByidAndmaterialId(_res, idList[i][j])[0].resName, "MergeGame"); } } } } private void onDragStart(EventContext context) { GObject item = context.sender as GObject; int itemInfo = (int)(context.sender as GObject).data; indexY = Math.Min(itemNum - 1, (int)(item.x / (_ui.m_rangeDown.width / 5))); indexX = Math.Min(itemNum - 1, (int)(item.y / (_ui.m_rangeDown.height / 5))); startX = item.x; startY = item.y; indexID = itemInfo; _ui.m_rangeDown.RemoveChild(item); _ui.m_rangeTop.AddChild(item); } private void onDragEnd(EventContext context) { GObject item = context.sender as GObject; int endIndexY = Math.Min(itemNum - 1 , (int)(item.x / (_ui.m_rangeDown.width/5))); int endIndexX = Math.Min(itemNum - 1 ,(int)(item.y / (_ui.m_rangeDown.height/5))); int itemEndInfo = (int)itemList[endIndexX][endIndexY].data; //合成规则:相同合成,但是材料只有五级,5和5不能合成6, 6属于另一类材料的一级 if (itemEndInfo == indexID && (indexX != endIndexX || indexY != endIndexY) && (itemEndInfo % maxLv != 0)) { indexID = 0; idList[indexX][indexY] = 0; itemList[indexX][indexY].data = 0; itemList[indexX][indexY].GetChild("icon").icon = ""; idList[endIndexX][endIndexY] = itemEndInfo + 1; itemList[endIndexX][endIndexY].data = itemEndInfo + 1; itemList[endIndexX][endIndexY].GetChild("icon").icon = ResPathUtil.GetMiniGamePicPath(MergeCfgArray.Instance.GetCfgsByidAndmaterialId(_res, itemEndInfo + 1)[0].resName, "MergeGame"); ShowNewMaterialView(endIndexX, endIndexY); UI_MergeGameItemUI itemUI = UI_MergeGameItemUI.Proxy(itemList[endIndexX][endIndexY]); itemUI.m_t0.Play(); UI_MergeGameItemUI.ProxyEnd(); if(InspectionTasks()) { _ui.m_rewardGet.visible = true; } else { _ui.m_rewardGet.visible = false; } } if (itemEndInfo != indexID && itemEndInfo == 0) { idList[indexX][indexY] = 0; itemList[indexX][indexY].data = 0; itemList[indexX][indexY].GetChild("icon").icon = ""; idList[endIndexX][endIndexY] = indexID; itemList[endIndexX][endIndexY].data = indexID; itemList[endIndexX][endIndexY].GetChild("icon").icon = ResPathUtil.GetMiniGamePicPath(MergeCfgArray.Instance.GetCfgsByidAndmaterialId(_res, indexID)[0].resName, "MergeGame"); itemEndInfo = indexID; indexID = 0; } item.x = startX; item.y = startY; if(indexID == 0) { item.draggable = false; } if(itemEndInfo > 0 ) { itemList[endIndexX][endIndexY].draggable = true; } startX = 0; startY = 0; indexX = 0; indexY = 0; indexID = 0; _ui.m_rangeTop.RemoveChild(item); _ui.m_rangeDown.AddChild(item); } private void OnClickItem(EventContext context) { GObject item = context.sender as GObject; int id = (int)item.data; if(id <= 0) { return; } indexY = Math.Min(itemNum - 1, (int)(item.x / (_ui.m_rangeDown.width / 5))); indexX = Math.Min(itemNum - 1, (int)(item.y / (_ui.m_rangeDown.height / 5))); materialID = id; _ui.m_name.text = id.ToString(); _ui.m_desc.text = id.ToString(); ItemCfg itemReward = ItemCfgArray.Instance.GetCfg(MergeCfgArray.Instance.GetCfgsByidAndmaterialId(_res, id)[0].bonusSellArr[0][0]); _ui.m_sellBtn.target.GetChild("sellReward").icon = ResPathUtil.GetCommonGameResPath(itemReward.res); _ui.m_sellBtn.target.GetChild("sellGetNum").text = MergeCfgArray.Instance.GetCfgsByidAndmaterialId(_res, id)[0].bonusSellArr[0][1].ToString(); } private void OnClickSellBtn() { if(materialID == 0) { return; } //移除 _ui.m_name.text = ""; _ui.m_desc.text = ""; _ui.m_sellBtn.target.GetChild("sellReward").icon = ""; _ui.m_sellBtn.target.GetChild("sellGetNum").text = ""; materialID = 0; idList[indexX][indexY] = 0; itemList[indexX][indexY].data = 0; UI_MergeGameItemUI itemUI = UI_MergeGameItemUI.Proxy(itemList[indexX][indexY]); itemUI.m_t2.Play(); itemUI.m_t3.Play(); UI_MergeGameItemUI.ProxyEnd(); indexX = 0; indexY = 0; //获得奖励 } private void OnClickAFuBtn() { bool gameOver = false; bool isRand = false; int materialID = 0; for (int i = 0; i < itemNum; i++) { for (int j = 0; j < itemNum; j++) { if (idList[i][j] == 0) { gameOver = true; break; } } } if (!gameOver) { return; } if(MergeGameArray.Instance.GetCfg(taskId).materialId1Arr.Length > 1) { isRand = true; } else { isRand = false; materialID = MergeGameArray.Instance.GetCfg(taskId).materialId1Arr[0]; } while (true) { int x = rand.Next(0, itemNum); int y = rand.Next(0, itemNum); int num; int randNum = rand.Next(0, 2); if (randNum == 0) { num = 1; } else { num = 6; } if(!isRand) { num = materialID; } if (idList[x][y] == 0) { idList[x][y] = num; itemList[x][y].data = num; itemList[x][y].GetChild("icon").icon = ResPathUtil.GetMiniGamePicPath(MergeCfgArray.Instance.GetCfgsByidAndmaterialId(_res, num)[0].resName, "MergeGame"); itemList[x][y].draggable = true; UI_MergeGameItemUI itemUI = UI_MergeGameItemUI.Proxy(itemList[x][y]); itemUI.m_t1.Play(); UI_MergeGameItemUI.ProxyEnd(); break; } } } private void OnClickDouBaoBtn() { bool gameOver = false; bool isRand = false; int materialID = 0; for (int i = 0; i < itemNum; i++) { for (int j = 0; j < itemNum; j++) { if (idList[i][j] == 0) { gameOver = true; break; } } } if (!gameOver) { return; } if (MergeGameArray.Instance.GetCfg(taskId).materialId2Arr.Length > 1) { isRand = true; } else { isRand = false; materialID = MergeGameArray.Instance.GetCfg(taskId).materialId2Arr[0]; } while (true) { int x = rand.Next(0, itemNum); int y = rand.Next(0, itemNum); int num; int randNum = rand.Next(0, 2); if (randNum == 0) { num = 11; } else { num = 16; } if (!isRand) { num = materialID; } if (idList[x][y] == 0) { idList[x][y] = num; itemList[x][y].data = num; itemList[x][y].draggable = true; itemList[x][y].GetChild("icon").icon = ResPathUtil.GetMiniGamePicPath(MergeCfgArray.Instance.GetCfgsByidAndmaterialId(_res, num)[0].resName, "MergeGame"); UI_MergeGameItemUI itemUI = UI_MergeGameItemUI.Proxy(itemList[x][y]); itemUI.m_t1.Play(); UI_MergeGameItemUI.ProxyEnd(); break; } } } private void UpdateTask() { _ui.m_rewardNum.text = String.Format("+{0}", MergeGameArray.Instance.GetCfg(taskId).bonusWinArr[0][1]); ItemCfg itemReward = ItemCfgArray.Instance.GetCfg(MergeGameArray.Instance.GetCfg(taskId).bonusWinArr[0][0]); _ui.m_rewardIcon.url = ResPathUtil.GetCommonGameResPath(itemReward.res); for (int k = 0; k < MergeGameArray.Instance.GetCfg(taskId).targetIdArr.Length; k++) { int targetID = MergeGameArray.Instance.GetCfg(taskId).targetIdArr[k][0]; int targetNum = MergeGameArray.Instance.GetCfg(taskId).targetIdArr[k][1]; switch (k) { case 0: _ui.m_rewardOne.target.visible = true; _ui.m_rewardTwo.target.visible = false; _ui.m_rewardOne.m_icon.url = ResPathUtil.GetMiniGamePicPath(MergeCfgArray.Instance.GetCfgsByidAndmaterialId(_res, targetID)[0].resName, "MergeGame"); _ui.m_rewardOne.m_Count.text = targetNum.ToString(); break; case 1: _ui.m_rewardTwo.target.visible = true; _ui.m_rewardTwo.m_icon.url = ResPathUtil.GetMiniGamePicPath(MergeCfgArray.Instance.GetCfgsByidAndmaterialId(_res, targetID)[0].resName, "MergeGame"); _ui.m_rewardTwo.m_Count.text = targetNum.ToString(); break; default: break; } } if (InspectionTasks()) { _ui.m_rewardGet.visible = true; } else { _ui.m_rewardGet.visible = false; } } //提交任务 private void SubmitTask() { for(int k = 0;k< MergeGameArray.Instance.GetCfg(taskId).targetIdArr.Length; k++) { int targetID = MergeGameArray.Instance.GetCfg(taskId).targetIdArr[k][0]; int targetNum = MergeGameArray.Instance.GetCfg(taskId).targetIdArr[k][1]; for (int i = 0; i < itemNum; i++) { for (int j = 0; j < itemNum; j++) { if(idList[i][j] == targetID) { idList[i][j] = 0; itemList[i][j].data = 0; UI_MergeGameItemUI itemUI = UI_MergeGameItemUI.Proxy(itemList[i][j]); itemUI.m_t2.Play(); itemUI.m_t3.Play(); UI_MergeGameItemUI.ProxyEnd(); targetNum--; if(targetNum == 0) { break; } } } if (targetNum == 0) { break; } } } //向后台发送任务ID,获得奖励 //taskId = MiniGameDateManager.Instance.taskID; //暂用 taskId++; // UpdateTask(); } //检查是否达到提交要求 private bool InspectionTasks() { bool isTarged = true; for (int k = 0; k < MergeGameArray.Instance.GetCfg(taskId).targetIdArr.Length; k++) { int targetID = MergeGameArray.Instance.GetCfg(taskId).targetIdArr[k][0]; int targetNum = MergeGameArray.Instance.GetCfg(taskId).targetIdArr[k][1]; int currentNum = 0; for (int i = 0; i < itemNum; i++) { for (int j = 0; j < itemNum; j++) { if (idList[i][j] == targetID) { currentNum++; if (currentNum == targetNum) { break; } } } if (currentNum == targetNum) { break; } } if (currentNum == targetNum && isTarged) { isTarged = true; } else { isTarged = false; } } return isTarged; } private void ShowNewMaterialView(int i ,int j) { //判断是否是新材料。再显示 _ui.m_newMaterial.target.x = itemList[i][j].x + _ui.m_rangeDown.x; _ui.m_newMaterial.target.y = itemList[i][j].y + _ui.m_rangeDown.y; _ui.m_newMaterial.target.visible = true; _ui.m_newMaterial.target.GetChild("icon").icon = ResPathUtil.GetMiniGamePicPath(MergeCfgArray.Instance.GetCfgsByidAndmaterialId(_res, idList[i][j])[0].resName,"MergeGame"); UI_MergeGameItemUI itemUI = UI_MergeGameItemUI.Proxy(_ui.m_newMaterial.target); itemUI.m_t0.Play(); UI_MergeGameItemUI.ProxyEnd(); } private void OnClickHideTips() { if(_ui.m_newMaterial.target.visible == true) { _ui.m_newMaterial.target.visible = false; } } private void ShowMergeRouteTips() { ViewManager.Show(new object[]{ _res }); } private void ShowShopBtn() { ViewManager.Show(new object[] { ConstStoreTabId.STORE_EXCHANGE, ConstStoreSubId.STORE_ACTIVITY_EXCHANGE }); } private void UpdateItemData() { ItemCfg itemcfg = ItemCfgArray.Instance.GetCfg(propID); _ui.m_btnProp.target.icon = ResPathUtil.GetIconPath(itemcfg.res,"png"); ItemData item; if (BagDataManager.Instance.GetBagData().TryGetValue(propID, out item)) { _ui.m_btnProp.target.title = item.num.ToString(); } else { _ui.m_btnProp.target.title = "0"; } } private void OnItemTips() { object[] sourceDatas = new object[] { propID, null }; GoodsItemTipsController.ShowItemTips(propID, sourceDatas); } } }