LuckyBoxStarView.cs 16 KB

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