MergeGameView.cs 20 KB

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