LuckyBoxStarView.cs 16 KB

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