LuckyBoxStarView.cs 16 KB

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