LuckyBoxStarView.cs 16 KB

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