LuckyBoxStarView.cs 12 KB

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