LuckyBoxStarView.cs 16 KB

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