using System; using System.Collections; using System.Collections.Generic; using cfg; using DG.Tweening; using TMPro; using UnityEngine; using UnityEngine.UI; using Random = UnityEngine.Random; public class TourPanel : MonoBehaviour { [SerializeField] private Slider[] _sliders; [SerializeField] private RectTransform[] _sliderTra; [SerializeField] private RectTransform[] _rectTrans; [SerializeField] private RectTransform randomParent; [SerializeField] private RectTransform endTransform; [SerializeField] private RectTransform playerMove; [SerializeField] private RectTransform startPoint; [SerializeField] private GameObject eventObj; [SerializeField] private TextMeshProUGUI eventText; [SerializeField] private Button eventButton; [SerializeField] private float timeScale = 1; [SerializeField] private int testLangh; [SerializeField] private float[] lengh = new float[4]; [SerializeField] private bool playing = false; [SerializeField] private TextItem _textItem; [SerializeField] private EventSystem _travelEvent; [SerializeField] private TextMeshProUGUI speedText; [SerializeField] private TextMeshProUGUI timeText; [SerializeField] private TextMeshProUGUI pointsText; [SerializeField] private List _eventlist = new List(); private List _randomList = new List(); private int maxIndex = 0; private float nowLenght = 0; private float nowTime = 0; private int moveTime; private int speed; private int points; private float pointsRatio; private float pointsNow = 0; private float textTime=0; #if UNITY_EDITOR [InspectorButton] #endif public void Test() { } public void StartVoid() { BoxDebug.Log("开始旅行界面"); lengh[0] = (_rectTrans[1].position - _rectTrans[0].position).magnitude; lengh[1] = (_rectTrans[2].position - _rectTrans[1].position).magnitude; lengh[2] = (_rectTrans[3].position - _rectTrans[2].position).magnitude; lengh[3] = (_rectTrans[0].position - _rectTrans[3].position).magnitude; var levelData = JsonTab.Instance.tables.Level.Get(GameManager.ins.level); _eventlist = new List(); for (var i = 0; i < levelData.Nodelocation.Count; i++) { var data = new NodeEventData(); data.lenght = ((float)levelData.Nodelocation[i])/100*5000; _eventlist.Add(data); } for (int i = 0; i < levelData.Eventgroupid.Count; i++) { _eventlist[i].eventTeamID = levelData.Eventgroupid[i]; } for (int i = 0; i < _eventlist.Count; i++) { _eventlist[i].eventID = GetEventID(_eventlist[i].eventTeamID); } for (int i = 0; i < _randomList.Count; i++) { Destroy(_randomList[i]); } _randomList.Clear(); for (var i = 0; i < _eventlist.Count; i++) { var obj = Instantiate(endTransform.gameObject, randomParent); _randomList.Add(obj); Move(obj.transform, _eventlist[i].lenght); } BoxDebug.Log("初始化设置数据"); maxIndex = 0; nowLenght = 0; playerMove.position = startPoint.position; moveTime = ItemSystem.ins.moveTime; nowTime = 0; textTime = 0; timeText.text = Math.Round(moveTime - nowTime,1).ToString(); speed = ItemSystem.ins.speed; speedText.text = speed.ToString(); points = ItemSystem.ins.points; pointsNow = 0; eventText.text = ""; playing = true; eventObj.SetActive(false); } public void Move(Transform point,float savelengh) { if (savelengh>5000) { savelengh = 5000; } int index = 0; for (int i = 0; i < lengh.Length; i++) { if (savelengh-lengh[i]>0) { savelengh -= lengh[i]; index++; } else { break; } } switch (index) { case 0: point.position = _rectTrans[0].position + ((_rectTrans[1].position - _rectTrans[0].position)*(savelengh/lengh[0])); break; case 1: point.position = _rectTrans[1].position + ((_rectTrans[2].position - _rectTrans[1].position)*(savelengh/lengh[1])); break; case 2: point.position = _rectTrans[2].position + ((_rectTrans[3].position - _rectTrans[2].position)*(savelengh/lengh[2])); break; case 3: point.position = _rectTrans[3].position + ((_rectTrans[0].position - _rectTrans[3].position)*(savelengh/lengh[3])); break; } } public int GetEventID(int id) { var data = JsonTab.Instance.tables.Eventgroup.Get(id); List list = new List(); foreach (var eventID in data.Eventgroup) { var levelData=JsonTab.Instance.tables.Eventtype.Get(eventID); list.Add(levelData); } int maxLenght = 0; foreach (var eID in list) { maxLenght += eID.Weight; } int randomLenght = Random.Range(0, maxLenght); int index = 0; foreach (var bleng in list) { if (randomLenght-bleng.Weight>0) { randomLenght -= bleng.Weight; index++; } } return list[index].ID; } public float LenghRatio() { return MaxLengh() / 5000; } public float MaxLengh() { return lengh[0] + lengh[1] + lengh[2] + lengh[3]; } private void SetPanel(RectTransform point,float langh) { var newLangh = langh; var index = 0; for (var i = 0; i < _sliders.Length; i++) { if (newLangh > _sliders[i].maxValue) { newLangh -= _sliders[i].maxValue; index++; } else { break; } } var endPoint = newLangh / _sliders[index].maxValue; point.parent = _sliderTra[index]; point.rotation = Quaternion.identity; point.anchorMax = new Vector2(endPoint,1); point.anchorMin = new Vector2(endPoint, 0); point.anchoredPosition = new Vector2(0, 0); } private void Awake() { // JsonTab.Instance.tables.Level.Get() } // Start is called before the first frame update void Start() { } private void OnEnable() { eventButton.onClick.AddListener(ButtonEvent); } // Update is called once per frame void Update() { } private void OnDisable() { eventButton.onClick.RemoveListener(ButtonEvent); } private void ButtonEvent() { _travelEvent.EventID(_eventlist[0].eventID, MaxLengh()*JsonTab.Instance.tables.Level.Get(GameManager.ins.level).Target/10000, ref nowLenght,ref speed,ref moveTime,ref points,ref pointsRatio); BoxDebug.Log(_eventlist[0].eventID+" "+5000*JsonTab.Instance.tables.Level.Get(GameManager.ins.level).Target/10000+" "+nowLenght+" "+speed+" "+points+" "+pointsRatio); speedText.text = speed.ToString(); RemoveEventList(); playing = true; eventObj.SetActive(false); } private void FixedUpdate() { if (!playing) { return; } nowLenght += Time.deltaTime*speed*LenghRatio(); nowTime += Time.deltaTime; textTime += Time.deltaTime; pointsNow += Time.deltaTime * ((float)points); pointsText.text = Math.Round(pointsNow, 0).ToString(); timeText.text = Math.Round(moveTime - nowTime,1).ToString(); if (textTime > 5) { textTime = 0; var levelData = JsonTab.Instance.tables.Level.Get(GameManager.ins.level).TextID; var textTeamData = JsonTab.Instance.tables.DisplayTxtTeam.Get(levelData); int index = Random.Range(0, textTeamData.Name.Count); _textItem.SaveOnText(textTeamData.Name[index]); } if (nowTime>moveTime) { BoxDebug.Log("时间用完旅行结束"); playing = false; MainPanel.ins.ClosePanelEvent(); } if (nowLenght>=MaxLengh()) { BoxDebug.Log("到达终点"); nowLenght = MaxLengh(); MainPanel.ins.ClosePanelEvent(); playing = false; } Move(playerMove, nowLenght); foreach (var data in _eventlist) { if (data.lenght= 0; i--) { if (_eventlist[i].lenght < nowLenght) { _eventlist.Remove(_eventlist[i]); } } } } public class NodeEventData { public float lenght; public int eventTeamID; public int eventID; }