MergeGameView.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. using System;
  2. using System.Collections.Generic;
  3. using ET;
  4. using FairyGUI;
  5. using UI.MiniGame;
  6. using UnityEngine;
  7. namespace GFGGame
  8. {
  9. public class MergeGameView : BaseWindow
  10. {
  11. private UI_MergeGameUI _ui;
  12. private List<List<GComponent>> itemList = new List<List<GComponent>>();
  13. private List<List<int>> idList = new List<List<int>>();
  14. private int itemNum;
  15. private int maxLv;
  16. private System.Random rand;
  17. private int _gameID;
  18. private int _res;
  19. //当前拖拽的index;
  20. private int indexID;
  21. //开始拖拽的坐标
  22. private float startX;
  23. private float startY;
  24. private int indexX;
  25. private int indexY;
  26. //当前任务id;后台获取数据
  27. private int taskId;
  28. //当前点击id信息
  29. private int materialID;
  30. private int propID = 3000020;
  31. public override void Dispose()
  32. {
  33. if (_ui != null)
  34. {
  35. _ui.Dispose();
  36. _ui = null;
  37. }
  38. base.Dispose();
  39. }
  40. protected override void OnInit()
  41. {
  42. base.OnInit();
  43. packageName = UI_MergeGameUI.PACKAGE_NAME;
  44. _ui = UI_MergeGameUI.Create();
  45. this.viewCom = _ui.target;
  46. isfullScreen = true;
  47. _ui.m_backBtn.onClick.Add(Hide);
  48. _ui.m_rewardGet.onClick.Add(SubmitTask);
  49. _ui.m_MergeTipBtn.onClick.Add(ShowMergeRouteTips);
  50. _ui.m_shopBtn.onClick.Add(ShowShopBtn);
  51. _ui.m_sellBtn.target.onClick.Add(OnClickSellBtn);
  52. _ui.target.onClick.Add(OnClickHideTips);
  53. _ui.m_btnProp.target.onClick.Add(OnItemTips);
  54. }
  55. protected override void OnShown()
  56. {
  57. base.OnShown();
  58. //进入这个界面的时候向后台获取任务id和历史id列表
  59. UpdateView();
  60. UpdateList();
  61. UpdateTask();
  62. UpdateItemData();
  63. }
  64. protected override void OnHide()
  65. {
  66. for (int i = 0; i < itemNum; i++)
  67. {
  68. for (int j = 0; j < itemNum; j++)
  69. {
  70. itemList[i][j].Dispose();
  71. itemList[i][j] = null;
  72. }
  73. }
  74. idList.Clear();
  75. itemList.Clear();
  76. base.OnHide();
  77. }
  78. private void UpdateData()
  79. {
  80. }
  81. private void UpdateView()
  82. {
  83. itemNum = 5;
  84. taskId = MiniGameDateManager.Instance.taskID;
  85. _res = MergeGameArray.Instance.GetCfg(taskId).res;
  86. maxLv = 5;
  87. materialID = 0;
  88. rand = new System.Random();
  89. _ui.m_rewardGet.visible = false;
  90. _ui.m_sellBtn.target.GetChild("sellGetNum").text = "";
  91. //_ui.m_newMaterial.target.visible = false;
  92. _ui.m_name.text = "";
  93. _ui.m_desc.text = "";
  94. _ui.m_bg.url = ResPathUtil.GetBgImgPath("jbnf_xyxbj");
  95. }
  96. private void UpdateList()
  97. {
  98. for (int i = 0; i < itemNum; i++)
  99. {
  100. itemList.Add(new List<GComponent>());
  101. idList.Add(new List<int>());
  102. for (int j = 0; j < itemNum; j++)
  103. {
  104. idList[i].Add(j);
  105. itemList[i].Add(UIPackage.CreateObject(UI_MergeGameUI.PACKAGE_NAME, "MergeGameItemUI").asCom);
  106. _ui.m_rangeDown.AddChild(itemList[i][j]);
  107. //对两个按钮单独处理
  108. if (i == itemNum - 1)
  109. {
  110. itemList[i][j].data = -1;
  111. idList[i][j] = -1;
  112. itemList[i][j].SetPosition((_ui.m_rangeDown.width / 10.0f) * (1 + (j * 2.0f)), (_ui.m_rangeDown.height / 10) * (1 + (i * 2.0f)), 0);
  113. if (j == 0)
  114. {
  115. itemList[i][j].GetChild("icon").icon = "ui://MiniGame/jbnf_doubao_icon";
  116. itemList[i][j].onClick.Add(OnClickAFuBtn);
  117. continue;
  118. }
  119. if ( j == itemNum - 1)
  120. {
  121. itemList[i][j].GetChild("icon").icon = "ui://MiniGame/jbnf_afu_icon";
  122. itemList[i][j].onClick.Add(OnClickDouBaoBtn);
  123. continue;
  124. }
  125. }
  126. //表示id;这里后续要改
  127. idList[i][j] = 0;
  128. if (i == 1 && j == 2)
  129. {
  130. idList[i][j] = 1;
  131. }
  132. if(i== 2 && j== 2)
  133. {
  134. idList[i][j] = 1;
  135. }
  136. //
  137. itemList[i][j].data = idList[i][j];
  138. itemList[i][j].draggable = true;
  139. itemList[i][j].onDragStart.Add(onDragStart);
  140. itemList[i][j].onDragEnd.Add(onDragEnd);
  141. itemList[i][j].onClick.Add(OnClickItem);
  142. itemList[i][j].dragBounds = new Rect(_ui.m_rangeTop.x + 90, _ui.m_rangeTop.y + 90, _ui.m_rangeTop.width, _ui.m_rangeTop.height);
  143. itemList[i][j].SetPosition( (_ui.m_rangeDown.width / 10.0f) * (1+ (j * 2.0f)), (_ui.m_rangeDown.height / 10) * (1+(i * 2.0f)) , 0);
  144. if (idList[i][j] == 0)
  145. {
  146. itemList[i][j].GetChild("icon").icon = "";
  147. itemList[i][j].draggable = false;
  148. }
  149. else
  150. {
  151. itemList[i][j].GetChild("icon").icon = ResPathUtil.GetMiniGamePicPath(MergeCfgArray.Instance.GetCfgsByidAndmaterialId(_res, idList[i][j])[0].resName, "MergeGame");
  152. }
  153. }
  154. }
  155. }
  156. private void onDragStart(EventContext context)
  157. {
  158. GObject item = context.sender as GObject;
  159. int itemInfo = (int)(context.sender as GObject).data;
  160. indexY = Math.Min(itemNum - 1, (int)(item.x / (_ui.m_rangeDown.width / 5)));
  161. indexX = Math.Min(itemNum - 1, (int)(item.y / (_ui.m_rangeDown.height / 5)));
  162. startX = item.x;
  163. startY = item.y;
  164. indexID = itemInfo;
  165. _ui.m_rangeDown.RemoveChild(item);
  166. _ui.m_rangeTop.AddChild(item);
  167. }
  168. private void onDragEnd(EventContext context)
  169. {
  170. GObject item = context.sender as GObject;
  171. int endIndexY = Math.Min(itemNum - 1 , (int)(item.x / (_ui.m_rangeDown.width/5)));
  172. int endIndexX = Math.Min(itemNum - 1 ,(int)(item.y / (_ui.m_rangeDown.height/5)));
  173. int itemEndInfo = (int)itemList[endIndexX][endIndexY].data;
  174. //合成规则:相同合成,但是材料只有五级,5和5不能合成6, 6属于另一类材料的一级
  175. if (itemEndInfo == indexID && (indexX != endIndexX || indexY != endIndexY) && (itemEndInfo % maxLv != 0))
  176. {
  177. indexID = 0;
  178. idList[indexX][indexY] = 0;
  179. itemList[indexX][indexY].data = 0;
  180. itemList[indexX][indexY].GetChild("icon").icon = "";
  181. idList[endIndexX][endIndexY] = itemEndInfo + 1;
  182. itemList[endIndexX][endIndexY].data = itemEndInfo + 1;
  183. itemList[endIndexX][endIndexY].GetChild("icon").icon = ResPathUtil.GetMiniGamePicPath(MergeCfgArray.Instance.GetCfgsByidAndmaterialId(_res, itemEndInfo + 1)[0].resName, "MergeGame");
  184. ShowNewMaterialView(endIndexX, endIndexY);
  185. UI_MergeGameItemUI itemUI = UI_MergeGameItemUI.Proxy(itemList[endIndexX][endIndexY]);
  186. itemUI.m_t0.Play();
  187. UI_MergeGameItemUI.ProxyEnd();
  188. if(InspectionTasks())
  189. {
  190. _ui.m_rewardGet.visible = true;
  191. }
  192. else
  193. {
  194. _ui.m_rewardGet.visible = false;
  195. }
  196. }
  197. if (itemEndInfo != indexID && itemEndInfo == 0)
  198. {
  199. idList[indexX][indexY] = 0;
  200. itemList[indexX][indexY].data = 0;
  201. itemList[indexX][indexY].GetChild("icon").icon = "";
  202. idList[endIndexX][endIndexY] = indexID;
  203. itemList[endIndexX][endIndexY].data = indexID;
  204. itemList[endIndexX][endIndexY].GetChild("icon").icon = ResPathUtil.GetMiniGamePicPath(MergeCfgArray.Instance.GetCfgsByidAndmaterialId(_res, indexID)[0].resName, "MergeGame");
  205. itemEndInfo = indexID;
  206. indexID = 0;
  207. }
  208. item.x = startX;
  209. item.y = startY;
  210. if(indexID == 0)
  211. {
  212. item.draggable = false;
  213. }
  214. if(itemEndInfo > 0 )
  215. {
  216. itemList[endIndexX][endIndexY].draggable = true;
  217. }
  218. startX = 0;
  219. startY = 0;
  220. indexX = 0;
  221. indexY = 0;
  222. indexID = 0;
  223. _ui.m_rangeTop.RemoveChild(item);
  224. _ui.m_rangeDown.AddChild(item);
  225. }
  226. private void OnClickItem(EventContext context)
  227. {
  228. GObject item = context.sender as GObject;
  229. int id = (int)item.data;
  230. if(id <= 0)
  231. {
  232. return;
  233. }
  234. indexY = Math.Min(itemNum - 1, (int)(item.x / (_ui.m_rangeDown.width / 5)));
  235. indexX = Math.Min(itemNum - 1, (int)(item.y / (_ui.m_rangeDown.height / 5)));
  236. materialID = id;
  237. _ui.m_name.text = id.ToString();
  238. _ui.m_desc.text = id.ToString();
  239. ItemCfg itemReward = ItemCfgArray.Instance.GetCfg(MergeCfgArray.Instance.GetCfgsByidAndmaterialId(_res, id)[0].bonusSellArr[0][0]);
  240. _ui.m_sellBtn.target.GetChild("sellReward").icon = ResPathUtil.GetCommonGameResPath(itemReward.res);
  241. _ui.m_sellBtn.target.GetChild("sellGetNum").text = MergeCfgArray.Instance.GetCfgsByidAndmaterialId(_res, id)[0].bonusSellArr[0][1].ToString();
  242. }
  243. private void OnClickSellBtn()
  244. {
  245. if(materialID == 0)
  246. {
  247. return;
  248. }
  249. //移除
  250. _ui.m_name.text = "";
  251. _ui.m_desc.text = "";
  252. _ui.m_sellBtn.target.GetChild("sellReward").icon = "";
  253. _ui.m_sellBtn.target.GetChild("sellGetNum").text = "";
  254. materialID = 0;
  255. idList[indexX][indexY] = 0;
  256. itemList[indexX][indexY].data = 0;
  257. UI_MergeGameItemUI itemUI = UI_MergeGameItemUI.Proxy(itemList[indexX][indexY]);
  258. itemUI.m_t2.Play();
  259. itemUI.m_t3.Play();
  260. UI_MergeGameItemUI.ProxyEnd();
  261. indexX = 0;
  262. indexY = 0;
  263. //获得奖励
  264. }
  265. private void OnClickAFuBtn()
  266. {
  267. bool gameOver = false;
  268. bool isRand = false;
  269. int materialID = 0;
  270. for (int i = 0; i < itemNum; i++)
  271. {
  272. for (int j = 0; j < itemNum; j++)
  273. {
  274. if (idList[i][j] == 0)
  275. {
  276. gameOver = true;
  277. break;
  278. }
  279. }
  280. }
  281. if (!gameOver)
  282. {
  283. return;
  284. }
  285. if(MergeGameArray.Instance.GetCfg(taskId).materialId1Arr.Length > 1)
  286. {
  287. isRand = true;
  288. }
  289. else
  290. {
  291. isRand = false;
  292. materialID = MergeGameArray.Instance.GetCfg(taskId).materialId1Arr[0];
  293. }
  294. while (true)
  295. {
  296. int x = rand.Next(0, itemNum);
  297. int y = rand.Next(0, itemNum);
  298. int num;
  299. int randNum = rand.Next(0, 2);
  300. if (randNum == 0)
  301. {
  302. num = 1;
  303. }
  304. else
  305. {
  306. num = 6;
  307. }
  308. if(!isRand)
  309. {
  310. num = materialID;
  311. }
  312. if (idList[x][y] == 0)
  313. {
  314. idList[x][y] = num;
  315. itemList[x][y].data = num;
  316. itemList[x][y].GetChild("icon").icon = ResPathUtil.GetMiniGamePicPath(MergeCfgArray.Instance.GetCfgsByidAndmaterialId(_res, num)[0].resName, "MergeGame");
  317. itemList[x][y].draggable = true;
  318. UI_MergeGameItemUI itemUI = UI_MergeGameItemUI.Proxy(itemList[x][y]);
  319. itemUI.m_t1.Play();
  320. UI_MergeGameItemUI.ProxyEnd();
  321. break;
  322. }
  323. }
  324. }
  325. private void OnClickDouBaoBtn()
  326. {
  327. bool gameOver = false;
  328. bool isRand = false;
  329. int materialID = 0;
  330. for (int i = 0; i < itemNum; i++)
  331. {
  332. for (int j = 0; j < itemNum; j++)
  333. {
  334. if (idList[i][j] == 0)
  335. {
  336. gameOver = true;
  337. break;
  338. }
  339. }
  340. }
  341. if (!gameOver)
  342. {
  343. return;
  344. }
  345. if (MergeGameArray.Instance.GetCfg(taskId).materialId2Arr.Length > 1)
  346. {
  347. isRand = true;
  348. }
  349. else
  350. {
  351. isRand = false;
  352. materialID = MergeGameArray.Instance.GetCfg(taskId).materialId2Arr[0];
  353. }
  354. while (true)
  355. {
  356. int x = rand.Next(0, itemNum);
  357. int y = rand.Next(0, itemNum);
  358. int num;
  359. int randNum = rand.Next(0, 2);
  360. if (randNum == 0)
  361. {
  362. num = 11;
  363. }
  364. else
  365. {
  366. num = 16;
  367. }
  368. if (!isRand)
  369. {
  370. num = materialID;
  371. }
  372. if (idList[x][y] == 0)
  373. {
  374. idList[x][y] = num;
  375. itemList[x][y].data = num;
  376. itemList[x][y].draggable = true;
  377. itemList[x][y].GetChild("icon").icon = ResPathUtil.GetMiniGamePicPath(MergeCfgArray.Instance.GetCfgsByidAndmaterialId(_res, num)[0].resName, "MergeGame");
  378. UI_MergeGameItemUI itemUI = UI_MergeGameItemUI.Proxy(itemList[x][y]);
  379. itemUI.m_t1.Play();
  380. UI_MergeGameItemUI.ProxyEnd();
  381. break;
  382. }
  383. }
  384. }
  385. private void UpdateTask()
  386. {
  387. _ui.m_rewardNum.text = String.Format("+{0}", MergeGameArray.Instance.GetCfg(taskId).bonusWinArr[0][1]);
  388. ItemCfg itemReward = ItemCfgArray.Instance.GetCfg(MergeGameArray.Instance.GetCfg(taskId).bonusWinArr[0][0]);
  389. _ui.m_rewardIcon.url = ResPathUtil.GetCommonGameResPath(itemReward.res);
  390. for (int k = 0; k < MergeGameArray.Instance.GetCfg(taskId).targetIdArr.Length; k++)
  391. {
  392. int targetID = MergeGameArray.Instance.GetCfg(taskId).targetIdArr[k][0];
  393. int targetNum = MergeGameArray.Instance.GetCfg(taskId).targetIdArr[k][1];
  394. switch (k)
  395. {
  396. case 0:
  397. _ui.m_rewardOne.target.visible = true;
  398. _ui.m_rewardTwo.target.visible = false;
  399. _ui.m_rewardOne.m_icon.url = ResPathUtil.GetMiniGamePicPath(MergeCfgArray.Instance.GetCfgsByidAndmaterialId(_res, targetID)[0].resName, "MergeGame");
  400. _ui.m_rewardOne.m_Count.text = targetNum.ToString();
  401. break;
  402. case 1:
  403. _ui.m_rewardTwo.target.visible = true;
  404. _ui.m_rewardTwo.m_icon.url = ResPathUtil.GetMiniGamePicPath(MergeCfgArray.Instance.GetCfgsByidAndmaterialId(_res, targetID)[0].resName, "MergeGame");
  405. _ui.m_rewardTwo.m_Count.text = targetNum.ToString();
  406. break;
  407. default:
  408. break;
  409. }
  410. }
  411. if (InspectionTasks())
  412. {
  413. _ui.m_rewardGet.visible = true;
  414. }
  415. else
  416. {
  417. _ui.m_rewardGet.visible = false;
  418. }
  419. }
  420. //提交任务
  421. private void SubmitTask()
  422. {
  423. for(int k = 0;k< MergeGameArray.Instance.GetCfg(taskId).targetIdArr.Length; k++)
  424. {
  425. int targetID = MergeGameArray.Instance.GetCfg(taskId).targetIdArr[k][0];
  426. int targetNum = MergeGameArray.Instance.GetCfg(taskId).targetIdArr[k][1];
  427. for (int i = 0; i < itemNum; i++)
  428. {
  429. for (int j = 0; j < itemNum; j++)
  430. {
  431. if(idList[i][j] == targetID)
  432. {
  433. idList[i][j] = 0;
  434. itemList[i][j].data = 0;
  435. UI_MergeGameItemUI itemUI = UI_MergeGameItemUI.Proxy(itemList[i][j]);
  436. itemUI.m_t2.Play();
  437. itemUI.m_t3.Play();
  438. UI_MergeGameItemUI.ProxyEnd();
  439. targetNum--;
  440. if(targetNum == 0)
  441. {
  442. break;
  443. }
  444. }
  445. }
  446. if (targetNum == 0)
  447. {
  448. break;
  449. }
  450. }
  451. }
  452. //向后台发送任务ID,获得奖励
  453. //taskId = MiniGameDateManager.Instance.taskID;
  454. //暂用
  455. taskId++;
  456. //
  457. UpdateTask();
  458. }
  459. //检查是否达到提交要求
  460. private bool InspectionTasks()
  461. {
  462. bool isTarged = true;
  463. for (int k = 0; k < MergeGameArray.Instance.GetCfg(taskId).targetIdArr.Length; k++)
  464. {
  465. int targetID = MergeGameArray.Instance.GetCfg(taskId).targetIdArr[k][0];
  466. int targetNum = MergeGameArray.Instance.GetCfg(taskId).targetIdArr[k][1];
  467. int currentNum = 0;
  468. for (int i = 0; i < itemNum; i++)
  469. {
  470. for (int j = 0; j < itemNum; j++)
  471. {
  472. if (idList[i][j] == targetID)
  473. {
  474. currentNum++;
  475. if (currentNum == targetNum)
  476. {
  477. break;
  478. }
  479. }
  480. }
  481. if (currentNum == targetNum)
  482. {
  483. break;
  484. }
  485. }
  486. if (currentNum == targetNum && isTarged)
  487. {
  488. isTarged = true;
  489. }
  490. else
  491. {
  492. isTarged = false;
  493. }
  494. }
  495. return isTarged;
  496. }
  497. private void ShowNewMaterialView(int i ,int j)
  498. {
  499. //判断是否是新材料。再显示
  500. _ui.m_newMaterial.target.x = itemList[i][j].x + _ui.m_rangeDown.x;
  501. _ui.m_newMaterial.target.y = itemList[i][j].y + _ui.m_rangeDown.y;
  502. _ui.m_newMaterial.target.visible = true;
  503. _ui.m_newMaterial.target.GetChild("icon").icon = ResPathUtil.GetMiniGamePicPath(MergeCfgArray.Instance.GetCfgsByidAndmaterialId(_res, idList[i][j])[0].resName,"MergeGame");
  504. UI_MergeGameItemUI itemUI = UI_MergeGameItemUI.Proxy(_ui.m_newMaterial.target);
  505. itemUI.m_t0.Play();
  506. UI_MergeGameItemUI.ProxyEnd();
  507. }
  508. private void OnClickHideTips()
  509. {
  510. if(_ui.m_newMaterial.target.visible == true)
  511. {
  512. _ui.m_newMaterial.target.visible = false;
  513. }
  514. }
  515. private void ShowMergeRouteTips()
  516. {
  517. ViewManager.Show<MergeGameMaterialTipView>(new object[]{ _res });
  518. }
  519. private void ShowShopBtn()
  520. {
  521. ViewManager.Show<StoreView>(new object[] { ConstStoreTabId.STORE_EXCHANGE, ConstStoreSubId.STORE_ACTIVITY_EXCHANGE });
  522. }
  523. private void UpdateItemData()
  524. {
  525. ItemCfg itemcfg = ItemCfgArray.Instance.GetCfg(propID);
  526. _ui.m_btnProp.target.icon = ResPathUtil.GetIconPath(itemcfg.res,"png");
  527. ItemData item;
  528. if (BagDataManager.Instance.GetBagData().TryGetValue(propID, out item))
  529. {
  530. _ui.m_btnProp.target.title = item.num.ToString();
  531. }
  532. else
  533. {
  534. _ui.m_btnProp.target.title = "0";
  535. }
  536. }
  537. private void OnItemTips()
  538. {
  539. object[] sourceDatas = new object[] { propID, null };
  540. GoodsItemTipsController.ShowItemTips(propID, sourceDatas);
  541. }
  542. }
  543. }