LuckyBoxStarView.cs 16 KB

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