WXGame/box1/Assets/Script/UI/TourPanel.cs

307 lines
9.3 KiB
C#
Raw Normal View History

2024-11-08 18:21:41 +08:00
using System;
using System.Collections;
using System.Collections.Generic;
2024-11-21 09:35:48 +08:00
using cfg;
using DG.Tweening;
using TMPro;
2024-11-08 18:21:41 +08:00
using UnityEngine;
using UnityEngine.UI;
2024-11-21 09:35:48 +08:00
using Random = UnityEngine.Random;
2024-11-08 18:21:41 +08:00
public class TourPanel : MonoBehaviour
{
[SerializeField] private Slider[] _sliders;
2024-11-13 16:56:37 +08:00
[SerializeField] private RectTransform[] _sliderTra;
2024-11-21 09:35:48 +08:00
[SerializeField] private RectTransform[] _rectTrans;
[SerializeField] private RectTransform randomParent;
2024-11-13 16:56:37 +08:00
[SerializeField] private RectTransform endTransform;
2024-11-21 09:35:48 +08:00
[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;
2024-11-13 16:56:37 +08:00
[SerializeField] private int testLangh;
2024-11-21 09:35:48 +08:00
[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<NodeEventData> _eventlist = new List<NodeEventData>();
private List<GameObject> _randomList = new List<GameObject>();
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
2024-11-13 16:56:37 +08:00
[InspectorButton]
2024-11-21 09:35:48 +08:00
#endif
2024-11-13 16:56:37 +08:00
public void Test()
2024-11-08 18:21:41 +08:00
{
2024-11-21 09:35:48 +08:00
2024-11-13 16:56:37 +08:00
}
2024-11-21 09:35:48 +08:00
public void StartVoid()
2024-11-13 16:56:37 +08:00
{
2024-11-21 09:35:48 +08:00
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<NodeEventData>();
for (var i = 0; i < levelData.Nodelocation.Count; i++)
2024-11-08 18:21:41 +08:00
{
2024-11-21 09:35:48 +08:00
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)
2024-11-13 16:56:37 +08:00
{
2024-11-21 09:35:48 +08:00
savelengh -= lengh[i];
2024-11-13 16:56:37 +08:00
index++;
}
else
{
break;
}
2024-11-08 18:21:41 +08:00
}
2024-11-21 09:35:48 +08:00
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<BEventtype> list = new List<BEventtype>();
foreach (var eventID in data.Eventgroup)
{
var levelData=JsonTab.Instance.tables.Eventtype.Get(eventID);
list.Add(levelData);
}
2024-11-13 16:56:37 +08:00
2024-11-21 09:35:48 +08:00
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];
2024-11-13 16:56:37 +08:00
}
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);
2024-11-08 18:21:41 +08:00
}
private void Awake()
{
// JsonTab.Instance.tables.Level.Get()
}
// Start is called before the first frame update
void Start()
{
}
2024-11-21 09:35:48 +08:00
private void OnEnable()
{
eventButton.onClick.AddListener(ButtonEvent);
}
2024-11-08 18:21:41 +08:00
// Update is called once per frame
void Update()
{
}
2024-11-21 09:35:48 +08:00
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<nowLenght)
{
playing = false;
eventObj.gameObject.SetActive(true);
eventText.text = JsonTab.Instance.tables.Eventtype.Get(data.eventID).Describe;
return;
}
}
}
private void RemoveEventList()
{
for (int i = _eventlist.Count-1; i >= 0; i--)
{
if (_eventlist[i].lenght < nowLenght)
{
_eventlist.Remove(_eventlist[i]);
}
}
}
2024-11-08 18:21:41 +08:00
}
2024-11-21 09:35:48 +08:00
public class NodeEventData
{
public float lenght;
public int eventTeamID;
public int eventID;
}