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

463 lines
14 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;
2024-11-26 15:44:50 +08:00
[SerializeField] private TextMeshProUGUI eventText1;
[SerializeField] private TextMeshProUGUI eventText2;
[SerializeField] private Button eventButton1;
[SerializeField] private Button eventButton2;
[SerializeField] private Button close;
[SerializeField] private Button settingButton;
[SerializeField] private Button audioButton;
[SerializeField] private Button soundEffectsButton;
2024-11-21 09:35:48 +08:00
[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];
2024-11-26 15:44:50 +08:00
[SerializeField] private float lenght = 5000;
2024-11-21 09:35:48 +08:00
[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>();
2024-11-26 15:44:50 +08:00
[SerializeField] private GameObject settingObj;
2024-11-21 09:35:48 +08:00
private List<GameObject> _randomList = new List<GameObject>();
2024-11-26 15:44:50 +08:00
private List<TaskEvent> events;
2024-11-21 09:35:48 +08:00
private int maxIndex = 0;
private float nowLenght = 0;
private float nowTime = 0;
2024-11-26 15:44:50 +08:00
private float moveTime;
2024-11-21 09:35:48 +08:00
2024-11-26 15:44:50 +08:00
private float speed;
private float points;
2024-11-21 09:35:48 +08:00
private float pointsRatio;
private float pointsNow = 0;
private float textTime=0;
2024-11-26 15:44:50 +08:00
private bool targets = false;
private float targtsLanght;
2024-11-21 09:35:48 +08:00
#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);
2024-11-26 15:44:50 +08:00
lenght = levelData.Length;
targtsLanght = (float)levelData.Length * ((float)levelData.Target / 10000);
targets = true;
events = new List<TaskEvent>();
events.Add(addTaskEvent(levelData.Maintasks));
foreach (var taskID in levelData.Additionaltasks)
{
events.Add(addTaskEvent(taskID));
}
2024-11-21 09:35:48 +08:00
_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();
2024-11-26 15:44:50 +08:00
data.lenght = ((float)levelData.Nodelocation[i])/100*lenght;
2024-11-21 09:35:48 +08:00
_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);
2024-11-26 15:44:50 +08:00
_eventlist[i].eventIDvideo = GetEventVideoID(_eventlist[i].eventTeamID);
2024-11-21 09:35:48 +08:00
}
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);
2024-11-26 15:44:50 +08:00
var data = JsonTab.Instance.tables.Eventgroup.Get(_eventlist[i].eventTeamID);
obj.GetComponent<Image>().sprite=Resources.Load<Sprite>("Icon/"+data.Icon);
2024-11-21 09:35:48 +08:00
_randomList.Add(obj);
Move(obj.transform, _eventlist[i].lenght);
}
2024-11-26 15:44:50 +08:00
InitDisplayData();
}
private void InitDisplayData()
{
2024-11-21 09:35:48 +08:00
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;
2024-11-26 15:44:50 +08:00
eventText1.text = "";
2024-11-21 09:35:48 +08:00
playing = true;
eventObj.SetActive(false);
}
2024-11-26 15:44:50 +08:00
private TaskEvent addTaskEvent(int id)
{
var data = JsonTab.Instance.tables.Task.Get(id);
TaskEvent taskEvent = new TaskEvent();
taskEvent.Action = new Action[7];
taskEvent.id = id;
taskEvent.type = data.Tasktype;
taskEvent.Action[data.Tasktype] = () =>
{
GameManager.ins.taskSystem.Event(taskEvent.id,nowTime);
};
return taskEvent;
}
private void TaskEvent(int index)
{
foreach (var task in events)
{
if (task.type==index)
{
GameManager.ins.taskSystem.Event(task.id,nowTime);
}
}
}
2024-11-21 09:35:48 +08:00
public void Move(Transform point,float savelengh)
{
2024-11-26 15:44:50 +08:00
var floatLenght=savelengh/lenght*MaxLengh();
if (floatLenght>MaxLengh())
2024-11-21 09:35:48 +08:00
{
2024-11-26 15:44:50 +08:00
floatLenght = MaxLengh();
2024-11-21 09:35:48 +08:00
}
int index = 0;
for (int i = 0; i < lengh.Length; i++)
{
2024-11-26 15:44:50 +08:00
if (floatLenght-lengh[i]>0)
2024-11-13 16:56:37 +08:00
{
2024-11-26 15:44:50 +08:00
floatLenght -= 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:
2024-11-26 15:44:50 +08:00
point.position = _rectTrans[0].position + ((_rectTrans[1].position - _rectTrans[0].position)*(floatLenght/lengh[0]));
2024-11-21 09:35:48 +08:00
break;
case 1:
2024-11-26 15:44:50 +08:00
point.position = _rectTrans[1].position + ((_rectTrans[2].position - _rectTrans[1].position)*(floatLenght/lengh[1]));
2024-11-21 09:35:48 +08:00
break;
case 2:
2024-11-26 15:44:50 +08:00
point.position = _rectTrans[2].position + ((_rectTrans[3].position - _rectTrans[2].position)*(floatLenght/lengh[2]));
2024-11-21 09:35:48 +08:00
break;
case 3:
2024-11-26 15:44:50 +08:00
point.position = _rectTrans[3].position + ((_rectTrans[0].position - _rectTrans[3].position)*(floatLenght/lengh[3]));
2024-11-21 09:35:48 +08:00
break;
}
}
public int GetEventID(int id)
{
var data = JsonTab.Instance.tables.Eventgroup.Get(id);
List<BEventtype> list = new List<BEventtype>();
2024-11-26 15:44:50 +08:00
foreach (var eventID in data.Eventgroup1)
2024-11-21 09:35:48 +08:00
{
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;
}
2024-11-26 15:44:50 +08:00
public int GetEventVideoID(int id)
{
var data = JsonTab.Instance.tables.Eventgroup.Get(id);
List<BEventtype> list = new List<BEventtype>();
foreach (var eventID in data.Eventgroup2)
{
var levelData=JsonTab.Instance.tables.Eventtype.Get(eventID);
list.Add(levelData);
}
2024-11-21 09:35:48 +08:00
2024-11-26 15:44:50 +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;
}
2024-11-21 09:35:48 +08:00
public float LenghRatio()
{
2024-11-26 15:44:50 +08:00
return MaxLengh() / lenght;
2024-11-21 09:35:48 +08:00
}
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()
{
2024-11-26 15:44:50 +08:00
eventButton1.onClick.AddListener(ButtonEvent1);
eventButton2.onClick.AddListener(ButtonEvent2);
close.onClick.AddListener(CloseUI);
settingButton.onClick.AddListener(SetSettingsObj);
audioButton.onClick.AddListener(AudioEvent);
soundEffectsButton.onClick.AddListener(SoundEffects);
settingObj.SetActive(false);
2024-11-21 09:35:48 +08:00
}
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()
{
2024-11-26 15:44:50 +08:00
eventButton1.onClick.RemoveListener(ButtonEvent1);
eventButton2.onClick.RemoveListener(ButtonEvent2);
close.onClick.RemoveListener(CloseUI);
settingButton.onClick.RemoveListener(SetSettingsObj);
audioButton.onClick.RemoveListener(AudioEvent);
soundEffectsButton.onClick.RemoveListener(SoundEffects);
}
public void CloseUI()
{
MainPanel.ins.ClosePanelEvent();
GameManager.ins.soundSystem.EventAudio();
}
private void AudioEvent()
{
GameManager.ins.soundSystem.StopAudioSource();
}
private void SoundEffects()
{
GameManager.ins.soundSystem.EventAudioSource();
}
private void SetSettingsObj()
{
settingObj.SetActive(!settingObj.activeSelf);
}
private void ButtonEvent1()
{
ButtonEvent(1);
}
private void ButtonEvent2()
{
ButtonEvent(2);
2024-11-21 09:35:48 +08:00
}
2024-11-26 15:44:50 +08:00
private void ButtonEvent(int index)
2024-11-21 09:35:48 +08:00
{
2024-11-26 15:44:50 +08:00
GameManager.ins.soundSystem.EventAudio();
_travelEvent.EventID(index==1? _eventlist[0].eventID:_eventlist[0].eventIDvideo,lenght*JsonTab.Instance.tables.Level.Get(GameManager.ins.level).Target/10000, ref nowLenght,ref speed,ref moveTime,ref points,ref pointsRatio);
2024-11-21 09:35:48 +08:00
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;
}
2024-11-26 15:44:50 +08:00
nowLenght += Time.deltaTime*speed;
2024-11-21 09:35:48 +08:00
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();
2024-11-26 15:44:50 +08:00
GameManager.ins.SaveNowLenght(nowLenght);
2024-11-21 09:35:48 +08:00
if (textTime > 5)
{
textTime = 0;
var levelData = JsonTab.Instance.tables.Level.Get(GameManager.ins.level).TextID;
2024-11-26 15:44:50 +08:00
BoxDebug.Log(levelData);
2024-11-21 09:35:48 +08:00
var textTeamData = JsonTab.Instance.tables.DisplayTxtTeam.Get(levelData);
int index = Random.Range(0, textTeamData.Name.Count);
_textItem.SaveOnText(textTeamData.Name[index]);
}
2024-11-26 15:44:50 +08:00
if (targets&&nowLenght>targtsLanght)
{
TaskEvent(5);
}
2024-11-21 09:35:48 +08:00
if (nowTime>moveTime)
{
2024-11-26 15:44:50 +08:00
DataManager.SetPointsData(GameManager.ins.level,(int)pointsNow);
TaskEvent(1);
2024-11-21 09:35:48 +08:00
BoxDebug.Log("时间用完旅行结束");
playing = false;
2024-11-26 15:44:50 +08:00
GameManager.ins.soundSystem.LoseAudio();
MainPanel.ins.Lose();
2024-11-21 09:35:48 +08:00
}
2024-11-26 15:44:50 +08:00
if (nowLenght>=lenght)
2024-11-21 09:35:48 +08:00
{
2024-11-26 15:44:50 +08:00
DataManager.SetPointsData(GameManager.ins.level,(int)pointsNow);
TaskEvent(1);
TaskEvent(2);
TaskEvent(3);
TaskEvent(6);
2024-11-21 09:35:48 +08:00
BoxDebug.Log("到达终点");
nowLenght = MaxLengh();
2024-11-21 10:26:47 +08:00
DataManager.SetLevelOpen(GameManager.ins.level,true);
2024-11-26 15:44:50 +08:00
MainPanel.ins.Win();
GameManager.ins.soundSystem.WinAudio();
2024-11-21 09:35:48 +08:00
playing = false;
}
Move(playerMove, nowLenght);
foreach (var data in _eventlist)
{
if (data.lenght<nowLenght)
{
playing = false;
eventObj.gameObject.SetActive(true);
2024-11-26 15:44:50 +08:00
eventText1.text = JsonTab.Instance.tables.DisplayTxt.Get(JsonTab.Instance.tables.Eventtype.Get(data.eventID).Describe).Text;
eventText2.text = JsonTab.Instance.tables.DisplayTxt.Get(JsonTab.Instance.tables.Eventtype.Get(data.eventIDvideo).Describe).Text;
2024-11-21 09:35:48 +08:00
return;
}
}
}
2024-11-26 15:44:50 +08:00
public void AddTime(int time)
{
moveTime += time;
playing = true;
}
2024-11-21 09:35:48 +08:00
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;
2024-11-26 15:44:50 +08:00
public int eventIDvideo;
}
public class TaskEvent
{
public int id = 0;
public int type = -1;
public Action[] Action=new Action[5];
2024-11-21 09:35:48 +08:00
}