LuckyBoxStarView.cs 15 KB

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