LuckyBoxStarView.cs 17 KB

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