LuckyBoxStarView.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. using System.Collections;
  2. using UnityEngine;
  3. using UI.LuckyBox;
  4. using System.Collections.Generic;
  5. using FairyGUI;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. using System;
  9. namespace GFGGame
  10. {
  11. public class LuckyBoxStarView : BaseWindow
  12. {
  13. private UI_LuckyBoxStarUI _ui;
  14. private List<GObject> comStars = new List<GObject>();
  15. private List<GObject> clickComStars = new List<GObject>();
  16. private List<GObject> notClickComStars = new List<GObject>();
  17. // private List<GameObject> _gameObjects = new List<GameObject>();
  18. // private List<GoWrapper> _wrappers = new List<GoWrapper>();
  19. // private List<GameObject> _gameObjects1 = new List<GameObject>();
  20. // private List<GoWrapper> _wrappers1 = new List<GoWrapper>();
  21. private Dictionary<int, List<GameObject>> dicGameobj = new Dictionary<int, List<GameObject>>();
  22. private Dictionary<int, List<GoWrapper>> dicWraper = new Dictionary<int, List<GoWrapper>>();
  23. private GObject curComStar;//当前选中的星星
  24. private Vector2 lastPos;//鼠标的上一个位置,每颗星星初始时默认为Vector2.right;
  25. private List<ItemData> _rewardList;
  26. private const int checkDistance = 40;//鼠标靠近星星周围加减40都算选中星星
  27. private const int imgLineWidth = 10;//线的原始长度
  28. private bool showGuide = false;
  29. public override void Dispose()
  30. {
  31. if (_ui != null)
  32. {
  33. _ui.Dispose();
  34. _ui = null;
  35. }
  36. base.Dispose();
  37. }
  38. protected override void OnHide()
  39. {
  40. int index = 0;
  41. GObject star = _ui.target.GetChild(string.Format("comStar{0}_{1}_{2}", _ui.m_ctrlBuyType.selectedIndex, _ui.m_ctrlRewardsType.selectedIndex, index));
  42. while (star != null && star.visible == true)
  43. {
  44. UI_ComStar comStar = UI_ComStar.Proxy(star);
  45. for (int i = comStar.target.numChildren - 1; i >= 0; i--)
  46. {
  47. if (comStar.target.GetChildAt(i).name == "imgLine") continue;
  48. comStar.target.RemoveChildAt(i);
  49. }
  50. index++;
  51. star = _ui.target.GetChild(string.Format("comStar{0}_{1}_{2}", _ui.m_ctrlBuyType.selectedIndex, _ui.m_ctrlRewardsType.selectedIndex, index));
  52. UI_ComStar.ProxyEnd();
  53. }
  54. foreach (int key in dicGameobj.Keys)
  55. {
  56. List<GameObject> value = dicGameobj[key];
  57. List<GoWrapper> wrappersValue = dicWraper[key];
  58. for (int i = 0; i < value.Count; i++)
  59. {
  60. SceneController.DestroyObjectFromView(value[i], wrappersValue[i]);
  61. }
  62. }
  63. notClickComStars.Clear();
  64. clickComStars.Clear();
  65. dicGameobj.Clear();
  66. dicWraper.Clear();
  67. Timers.inst.Remove(CheckGuide);
  68. }
  69. protected override void OnInit()
  70. {
  71. base.OnInit();
  72. _ui = UI_LuckyBoxStarUI.Create();
  73. this.viewCom = _ui.target;
  74. isfullScreen = true;
  75. _ui.m_btnBack.visible = false;
  76. _ui.m_btnBack.onClick.Add(OnClickBtnBack);
  77. }
  78. protected override void OnShown()
  79. {
  80. base.OnShown();
  81. _ui.target.onTouchBegin.Add(OnClickUIBegin);
  82. _ui.target.onTouchMove.Add(OnClickUIMove);
  83. _ui.target.onTouchEnd.Add(OnClickUIEnd);
  84. _ui.m_effEnd.visible = false;
  85. _rewardList = LuckyBoxDataManager.Instance.RewardList;
  86. _ui.m_ctrlBuyType.selectedIndex = _rewardList != null && _rewardList.Count > 1 ? 1 : 0;
  87. bool isLuckyBox = LuckyBoxDataManager.Instance.luckyBoxIds.IndexOf(LuckyBoxDataManager.Instance.currentBoxId) >= 0;
  88. _ui.m_ctrlRewardsType.selectedIndex = isLuckyBox ? (LuckyBoxDataManager.Instance.currentBoxId - 1) : 0;
  89. string resPath = string.Format("cj_tp_{0}", isLuckyBox ? LuckyBoxDataManager.Instance.currentBoxId : 1);
  90. _ui.m_bg.url = ResPathUtil.GetBgImgPath(resPath);
  91. ResetStartView();
  92. Timers.inst.AddUpdate(CheckGuide);
  93. }
  94. private void ResetStartView()
  95. {
  96. showGuide = GuideDataManager.IsGuideFinish(ConstGuideId.LUCKY_BOX_LINE) <= 0;
  97. curComStar = null;
  98. comStars.Clear();
  99. int index = 0;
  100. GObject star = _ui.target.GetChild(string.Format("comStar{0}_{1}_{2}", _ui.m_ctrlBuyType.selectedIndex, _ui.m_ctrlRewardsType.selectedIndex, index));
  101. while (star != null && star.visible == true)
  102. {
  103. UI_ComStar comStar = UI_ComStar.Proxy(star);
  104. comStar.m_imgLine.visible = true;
  105. comStar.m_c1.selectedIndex = 0;
  106. comStar.m_imgLine.width = imgLineWidth;
  107. comStar.m_imgLine.rotation = 0;
  108. GComponent gcom = CreateEffect(comStar, 1, "ui_ck"); ;
  109. gcom.visible = true;
  110. comStar.target.AddChildAt(gcom, 1);
  111. GComponent gcom1 = CreateEffect(comStar, 2, "ui_ck_dj");
  112. gcom1.visible = false;
  113. comStar.target.AddChildAt(gcom1, 2);
  114. GComponent gcom2 = CreateEffect(comStar, 3, "ui_ck_xs");
  115. gcom2.visible = false;
  116. comStar.target.AddChildAt(gcom2, 3);
  117. GComponent gcom3 = CreateEffect(comStar, 4, "ui_ck_dj_2");
  118. gcom3.visible = false;
  119. comStar.target.AddChildAt(gcom3, 4);
  120. star.data = new Vector2(comStar.target.x, comStar.target.y);
  121. comStars.Add(star);
  122. index++;
  123. star = _ui.target.GetChild(string.Format("comStar{0}_{1}_{2}", _ui.m_ctrlBuyType.selectedIndex, _ui.m_ctrlRewardsType.selectedIndex, index));
  124. UI_ComStar.ProxyEnd();
  125. }
  126. }
  127. private GComponent CreateEffect(UI_ComStar comStar, int index, string name)
  128. {
  129. GComponent gcom;
  130. if (comStar.target.numChildren > index)
  131. {
  132. gcom = comStar.target.GetChildAt(index).asCom;
  133. }
  134. else
  135. {
  136. gcom = UIPackage.CreateObject("LuckyBox", "ComHolder").asCom;
  137. string resPath = ResPathUtil.GetViewEffectPath("ui_ck", name);
  138. SceneController.AddObjectToView(null, null, gcom.GetChild("holder").asGraph, resPath, out GameObject gameObject, out GoWrapper wrapper);
  139. if (!dicGameobj.ContainsKey(index))
  140. {
  141. dicGameobj.Add(index, new List<GameObject>());
  142. dicWraper.Add(index, new List<GoWrapper>());
  143. }
  144. dicGameobj[index].Add(gameObject);
  145. dicWraper[index].Add(wrapper);
  146. }
  147. return gcom;
  148. }
  149. private void OnClickUIBegin(EventContext context)
  150. {
  151. context.CaptureTouch();
  152. InputEvent inputEvent = (InputEvent)context.data;
  153. Vector2 mousePos = this.viewCom.GlobalToLocal(new Vector2(inputEvent.x, inputEvent.y));
  154. CheckNearbyPos(mousePos);
  155. }
  156. private void OnClickUIMove(EventContext context)
  157. {
  158. InputEvent inputEvent = (InputEvent)context.data;
  159. Vector2 mousePos = this.viewCom.GlobalToLocal(new Vector2(inputEvent.x, inputEvent.y));
  160. CheckNearbyPos(mousePos);
  161. if (comStars.Count == 0)//所有星星都点亮了主动结束
  162. {
  163. this.OnClickUIEnd();
  164. }
  165. }
  166. //检测鼠标附近的星星
  167. private void CheckNearbyPos(Vector2 mousePos)
  168. {
  169. for (int i = comStars.Count - 1; i >= 0; i--)
  170. {
  171. Vector2 comStarPos = (Vector2)comStars[i].data;
  172. if (IsSamePos(mousePos, comStarPos))
  173. {
  174. if (curComStar != null)
  175. {
  176. SetCurComStarTransfrom(comStarPos);
  177. }
  178. UI_ComStar comStar = UI_ComStar.Proxy(comStars[i]);
  179. comStar.target.GetChildAt(2).asCom.visible = true;
  180. comStar.target.GetChildAt(1).asCom.visible = false;
  181. UI_ComStar.ProxyEnd();
  182. // comStar.m_c1.selectedIndex = 1;
  183. curComStar = comStars[i];
  184. lastPos = Vector2.right;
  185. clickComStars.Add(comStars[i]);
  186. comStars.RemoveAt(i);
  187. }
  188. else
  189. {
  190. if (curComStar != null)
  191. {
  192. Vector2 curPos = mousePos - (Vector2)curComStar.data;
  193. SetCurComStarTransfrom(mousePos);
  194. lastPos = curPos;
  195. }
  196. }
  197. }
  198. }
  199. private void SetCurComStarTransfrom(Vector2 targetPos)
  200. {
  201. Vector2 curPos = targetPos - (Vector2)curComStar.data;
  202. float angle = Vector3.Angle(lastPos, curPos); //求出两向量之间的夹角
  203. Vector3 normal = Vector3.Cross(lastPos, curPos);//叉乘求出法线向量
  204. angle *= Mathf.Sign(Vector3.Dot(normal, Vector3.forward)); //Mathf.Sign()求符号,Vector3.Dot()求方向,求法线向量与物体上方向向量点乘,结果为1或-1,修正旋转方向
  205. UI_ComStar comStar = UI_ComStar.Proxy(curComStar);
  206. comStar.m_imgLine.rotation += angle;
  207. comStar.m_imgLine.width = Vector2.Distance(targetPos, (Vector2)curComStar.data);
  208. UI_ComStar.ProxyEnd();
  209. }
  210. private void OnClickUIEnd()
  211. {
  212. if (clickComStars.Count <= 0) return;
  213. UI_ComStar comStar = UI_ComStar.Proxy(clickComStars[clickComStars.Count - 1]);
  214. comStar.m_imgLine.visible = false;
  215. UI_ComStar.ProxyEnd();
  216. if (showGuide && clickComStars.Count < 2)
  217. {
  218. ResetStartView();
  219. }
  220. else
  221. {
  222. CheckNotClickComStar();
  223. SetComStarDarken();
  224. RemoveListener();
  225. Timers.inst.Add(1f, 1, SetClickComStarAni);
  226. }
  227. }
  228. private void CheckNotClickComStar()
  229. {
  230. int index = 0;
  231. GObject star = _ui.target.GetChild(string.Format("comStar{0}_{1}_{2}", _ui.m_ctrlBuyType.selectedIndex, _ui.m_ctrlRewardsType.selectedIndex, index));
  232. while (star != null && star.visible == true)
  233. {
  234. if (clickComStars.IndexOf(star) < 0)
  235. {
  236. notClickComStars.Add(star);
  237. }
  238. index++;
  239. star = _ui.target.GetChild(string.Format("comStar{0}_{1}_{2}", _ui.m_ctrlBuyType.selectedIndex, _ui.m_ctrlRewardsType.selectedIndex, index));
  240. }
  241. }
  242. private void SetComStarDarken()
  243. {
  244. for (int i = 0; i < notClickComStars.Count; i++)
  245. {
  246. UI_ComStar notClickComStar = UI_ComStar.Proxy(notClickComStars[i]);
  247. notClickComStar.m_imgLine.visible = false;
  248. notClickComStar.target.GetChildAt(3).asCom.visible = true;
  249. notClickComStar.target.GetChildAt(1).asCom.visible = false;
  250. UI_ComStar.ProxyEnd();
  251. }
  252. }
  253. private void SetClickComStarAni(object param)
  254. {
  255. for (int i = 0; i < clickComStars.Count; i++)
  256. {
  257. UI_ComStar comStar = UI_ComStar.Proxy(clickComStars[i]);
  258. comStar.target.GetChildAt(4).asCom.visible = true;
  259. UI_ComStar.ProxyEnd();
  260. }
  261. Timers.inst.Add(0.5f, 1, SetEndEffect);
  262. }
  263. private void SetEndEffect(object param)
  264. {
  265. _ui.m_effEnd.visible = true;
  266. _ui.m_effEnd.SetPlaySettings(0, -1, 1, -1);
  267. Timers.inst.Add(0.6f, 1, ClickUIEnd);
  268. // ClickUIEnd(null);
  269. TryCompleteGuide();
  270. }
  271. private void ClickUIEnd(object param)
  272. {
  273. if (curComStar != null)
  274. {
  275. RemoveListener();
  276. ViewManager.Show<LuckyBoxBonusView>(new object[] { _rewardList });
  277. this.Hide();
  278. }
  279. }
  280. private bool IsSamePos(Vector2 mousePos, Vector2 comStarPos)
  281. {
  282. return (mousePos.x < comStarPos.x + checkDistance) && (mousePos.x > comStarPos.x - checkDistance) && (mousePos.y < comStarPos.y + checkDistance) && (mousePos.y > comStarPos.y - checkDistance);
  283. }
  284. private void RemoveListener()
  285. {
  286. _ui.target.onTouchBegin.Remove(OnClickUIBegin);
  287. _ui.target.onTouchMove.Remove(OnClickUIMove);
  288. _ui.target.onTouchEnd.Remove(OnClickUIEnd);
  289. }
  290. private void OnClickBtnBack()
  291. {
  292. this.Hide();
  293. RemoveListener();
  294. ViewManager.Show(ViewName.LUCKY_BOX_VIEW);
  295. }
  296. private void CheckGuide(object param)
  297. {
  298. if (GuideDataManager.IsGuideFinish(ConstGuideId.LUCKY_BOX_LINE) <= 0)
  299. {
  300. UpdateToCheckGuide(null);
  301. }
  302. else
  303. {
  304. Timers.inst.Remove(CheckGuide);
  305. }
  306. }
  307. protected override void UpdateToCheckGuide(object param)
  308. {
  309. if (!ViewManager.CheckIsTopView(this.viewCom)) return;
  310. GuideCfg cfg = GuideCfgArray.Instance.GetCfg(ConstGuideId.LUCKY_BOX_LINE);
  311. // GuideController.TryCompleteGuide(ConstGuideId.LUCKY_BOX, 4);
  312. GuideController.TryGuide(null, ConstGuideId.LUCKY_BOX_LINE, 1, "点击将星连接在一起。", -1, true, (int)(this.viewCom.height - 200), true);
  313. TryCompleteGuide();
  314. }
  315. protected override void TryCompleteGuide()
  316. {
  317. if (clickComStars.Count >= 2)
  318. {
  319. GuideCfg cfg = GuideCfgArray.Instance.GetCfg(ConstGuideId.LUCKY_BOX_LINE);
  320. GuideController.TryCompleteGuideIndex(cfg.id, 1);
  321. GuideController.TryCompleteGuide(ConstGuideId.LUCKY_BOX_LINE, 1);
  322. }
  323. else
  324. {
  325. GuideDataManager.SetGuideIndexState(GuideDataManager.currentGuideId, GuideDataManager.currentGuideIdIndex, 0);
  326. GuideDataManager.currentGuideIdIndex = 3;
  327. }
  328. }
  329. }
  330. }