123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199 |
- using System.Collections;
- using UnityEngine;
- using UI.LuckyBox;
- using System.Collections.Generic;
- using FairyGUI;
- using System.Threading;
- using System.Threading.Tasks;
- namespace GFGGame
- {
- public class LuckyBoxStarView : BaseWindow
- {
- private UI_LuckyBoxStarUI _ui;
- private List<GObject> comStars = new List<GObject>();
- private GameObject[] _effectObjs;
- private GoWrapper[] _wrappers;
- private GObject curComStar;//当前选中的星星
- private Vector2 lastPos;//鼠标的上一个位置,每颗星星初始时默认为Vector2.right;
- private List<ItemData> _rewardList;
- private const int checkDistance = 40;//鼠标靠近星星周围加减40都算选中星星
- private const int imgLineWidth = 10;//线的原始长度
- private bool isGuide = false;
- public override void Dispose()
- {
- base.Dispose();
- }
- protected override void OnInit()
- {
- base.OnInit();
- _ui = UI_LuckyBoxStarUI.Create();
- this.viewCom = _ui.target;
- isfullScreen = true;
- _ui.m_btnBack.onClick.Add(OnClickBtnBack);
- }
- protected override void OnShown()
- {
- base.OnShown();
- _ui.target.onTouchBegin.Add(OnClickUIBegin);
- _ui.target.onTouchMove.Add(OnClickUIMove);
- _ui.target.onTouchEnd.Add(OnClickUIEnd);
- _rewardList = LuckyBoxDataManager.Instance.RewardList;
- _ui.m_ctrlBuyType.selectedIndex = _rewardList != null && _rewardList.Count > 1 ? 1 : 0;
- _ui.m_ctrlRewardsType.selectedIndex = (int)viewData;
- ResetStartView();
- }
- private void ResetStartView()
- {
- isGuide = GuideController.TryGuideLuckyBoxStar();
- curComStar = null;
- comStars.Clear();
- int index = 0;
- GObject star = _ui.target.GetChild(string.Format("comStar{0}_{1}_{2}", _ui.m_ctrlBuyType.selectedIndex, _ui.m_ctrlRewardsType.selectedIndex, index));
- while (star != null && star.visible == true)
- {
- UI_ComStar comStar = UI_ComStar.Proxy(star);
- comStar.m_c1.selectedIndex = 0;
- comStar.m_imgLine.width = imgLineWidth;
- comStar.m_imgLine.rotation = 0;
- star.data = new Vector2(comStar.target.x, comStar.target.y);
- comStars.Add(star);
- index++;
- star = _ui.target.GetChild(string.Format("comStar{0}_{1}_{2}", _ui.m_ctrlBuyType.selectedIndex, _ui.m_ctrlRewardsType.selectedIndex, index));
- }
- }
- private void OnClickUIBegin(EventContext context)
- {
- context.CaptureTouch();
- InputEvent inputEvent = (InputEvent)context.data;
- Vector2 mousePos = this.viewCom.GlobalToLocal(new Vector2(inputEvent.x, inputEvent.y));
- CheckNearbyPos(mousePos);
- }
- private void OnClickUIMove(EventContext context)
- {
- InputEvent inputEvent = (InputEvent)context.data;
- Vector2 mousePos = this.viewCom.GlobalToLocal(new Vector2(inputEvent.x, inputEvent.y));
- CheckNearbyPos(mousePos);
- if (comStars.Count == 0)//所有星星都点亮了主动结束
- {
- this.OnClickUIEnd();
- }
- }
- //检测鼠标附近的星星
- private void CheckNearbyPos(Vector2 mousePos)
- {
- for (int i = comStars.Count - 1; i >= 0; i--)
- {
- Vector2 comStarPos = (Vector2)comStars[i].data;
- if (IsSamePos(mousePos, comStarPos))
- {
- if (curComStar != null)
- {
- SetCurComStarTransfrom(comStarPos);
- }
- UI_ComStar comStar = UI_ComStar.Proxy(comStars[i]);
- comStar.m_c1.selectedIndex = 1;
- curComStar = comStars[i];
- lastPos = Vector2.right;
- comStars.RemoveAt(i);
- }
- else
- {
- if (curComStar != null)
- {
- Vector2 curPos = mousePos - (Vector2)curComStar.data;
- SetCurComStarTransfrom(mousePos);
- lastPos = curPos;
- }
- }
- }
- }
- private void SetCurComStarTransfrom(Vector2 targetPos)
- {
- Vector2 curPos = targetPos - (Vector2)curComStar.data;
- float angle = Vector3.Angle(lastPos, curPos); //求出两向量之间的夹角
- Vector3 normal = Vector3.Cross(lastPos, curPos);//叉乘求出法线向量
- angle *= Mathf.Sign(Vector3.Dot(normal, Vector3.forward)); //Mathf.Sign()求符号,Vector3.Dot()求方向,求法线向量与物体上方向向量点乘,结果为1或-1,修正旋转方向
- UI_ComStar comStar = UI_ComStar.Proxy(curComStar);
- comStar.m_imgLine.rotation += angle;
- comStar.m_imgLine.width = Vector2.Distance(targetPos, (Vector2)curComStar.data);
- }
- private void OnClickUIEnd()
- {
- if (isGuide && comStars.Count > 0)
- {
- ResetStartView();
- }
- else
- {
- Timers.inst.Add(0.3f, 1, ClickUIEnd);
- GuideController.HideGuide();
- GuideController.TryCompleteGuide(ConstGuideId.MAIN_UI_BTN_ZHAI_XING);
- }
- }
- private void ClickUIEnd(object param)
- {
- if (curComStar != null)
- {
- RemoveListener();
- ViewManager.Show(ViewName.LUCKY_BOX_CARD_VIEW, new object[] { _rewardList, FirstGetCardViewType.JUMP });
- this.Hide();
- }
- }
- private bool IsSamePos(Vector2 mousePos, Vector2 comStarPos)
- {
- return (mousePos.x < comStarPos.x + checkDistance) && (mousePos.x > comStarPos.x - checkDistance) && (mousePos.y < comStarPos.y + checkDistance) && (mousePos.y > comStarPos.y - checkDistance);
- }
- private void RemoveListener()
- {
- _ui.target.onTouchBegin.Remove(OnClickUIBegin);
- _ui.target.onTouchMove.Remove(OnClickUIMove);
- _ui.target.onTouchEnd.Remove(OnClickUIEnd);
- }
- private void OnClickBtnBack()
- {
- this.Hide();
- RemoveListener();
- ViewManager.Show(ViewName.LUCKY_BOX_VIEW);
- }
- protected override void OnHide()
- {
- base.OnHide();
- }
- }
- }
|