LuckyBoxStarView.cs 16 KB

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