WXGame/box1/Assets/Script/UI/TourPanel.cs
2024-11-29 21:37:01 +08:00

489 lines
16 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using cfg;
using DG.Tweening;
using Spine.Unity;
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 GameObject[] mapIcon;
[SerializeField] private RectTransform randomParent;
[SerializeField] private RectTransform endTransform;
[SerializeField] private RectTransform targtsTransform;
[SerializeField] private RectTransform playerMove;
[SerializeField] private RectTransform startPoint;
[SerializeField] private GameObject eventObj;
[SerializeField] private TextMeshProUGUI eventTitle;
[SerializeField] private TextMeshProUGUI eventText1;
[SerializeField] private TextMeshProUGUI eventText2;
[SerializeField] private GameObject event1Video;
[SerializeField] private GameObject event2Video;
[SerializeField] private Button eventButton1;
[SerializeField] private Button eventButton2;
[SerializeField] private Button close;
[SerializeField] private Button settingButton;
[SerializeField] private Button audioButton;
[SerializeField] private Button soundEffectsButton;
[SerializeField] private float timeScale = 1;
[SerializeField] private int testLangh;
[SerializeField] private float[] lengh = new float[4];
[SerializeField] private float lenght = 5000;
[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>();
[SerializeField] private GameObject settingObj;
private List<GameObject> _randomList = new List<GameObject>();
private List<TaskEvent> events;
private int maxIndex = 0;
private float nowLenght = 0;
private float nowTime = 0;
private float moveTime;
private float speed;
private float points;
private float pointsRatio;
private float pointsNow = 0;
private float textTime=0;
private bool targets = false;
private float targtsLanght;
#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);
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));
}
_eventlist = new List<NodeEventData>();
for (var i = 0; i < levelData.Nodelocation.Count; i++)
{
var data = new NodeEventData();
data.lenght = ((float)levelData.Nodelocation[i])/100*lenght;
_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);
_eventlist[i].eventIDvideo = GetEventVideoID(_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);
var data = JsonTab.Instance.tables.Eventgroup.Get(_eventlist[i].eventTeamID);
obj.GetComponent<Image>().sprite=Resources.Load<Sprite>("Icon/"+data.Icon);
obj.GetComponentInChildren<SkeletonGraphic>().Skeleton.SetSkin(data.Icon == "shijian" ? "box" : "coffer");
obj.GetComponentInChildren<SkeletonGraphic>().Skeleton.SetSlotsToSetupPose();
_randomList.Add(obj);
Move(obj.transform, _eventlist[i].lenght);
}
var chapterData = JsonTab.Instance.tables.Chapter.Get(DataManager.chapter);
for (int i = 0; i < chapterData.Contain.Count; i++)
{
if (DataManager.LevelOpen(chapterData.Contain[i])||chapterData.Contain[i]==GameManager.ins.level)
{
// mapIcon[i].SetActive(true);
mapIcon[i].GetComponent<Image>().color = Color.white;
}
else
{
// mapIcon[i].SetActive(false);
mapIcon[i].GetComponent<Image>().color = Color.gray;
}
}
Move(targtsTransform,targtsLanght);
InitDisplayData();
}
private void InitDisplayData()
{
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;
eventText1.text = "";
playing = true;
eventObj.SetActive(false);
}
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);
}
}
}
public void Move(Transform point,float savelengh)
{
var floatLenght=savelengh/lenght*MaxLengh();
if (floatLenght>MaxLengh())
{
floatLenght = MaxLengh();
}
int index = 0;
for (int i = 0; i < lengh.Length; i++)
{
if (floatLenght-lengh[i]>0)
{
floatLenght -= lengh[i];
index++;
}
else
{
break;
}
}
switch (index)
{
case 0:
point.position = _rectTrans[0].position + ((_rectTrans[1].position - _rectTrans[0].position)*(floatLenght/lengh[0]));
break;
case 1:
point.position = _rectTrans[1].position + ((_rectTrans[2].position - _rectTrans[1].position)*(floatLenght/lengh[1]));
break;
case 2:
point.position = _rectTrans[2].position + ((_rectTrans[3].position - _rectTrans[2].position)*(floatLenght/lengh[2]));
break;
case 3:
point.position = _rectTrans[3].position + ((_rectTrans[0].position - _rectTrans[3].position)*(floatLenght/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.Eventgroup1)
{
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 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);
}
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() / lenght;
}
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()
{
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);
}
// Update is called once per frame
void Update()
{
}
private void OnDisable()
{
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);
}
private void ButtonEvent(int index)
{
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);
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.fixedDeltaTime*speed;
BoxDebug.Log(nowLenght);
nowTime += Time.fixedDeltaTime;
textTime += Time.fixedDeltaTime;
pointsNow += Time.fixedDeltaTime * ((float)points);
pointsText.text = Math.Round(pointsNow, 0).ToString();
timeText.text = Math.Round(moveTime - nowTime,1).ToString();
GameManager.ins.SaveNowLenght(nowLenght);
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 (targets&&nowLenght>targtsLanght)
{
TaskEvent(5);
}
if (nowTime>moveTime)
{
DataManager.SetPointsData(GameManager.ins.level,(int)pointsNow);
TaskEvent(1);
BoxDebug.Log("时间用完旅行结束");
playing = false;
GameManager.ins.soundSystem.LoseAudio();
MainPanel.ins.Lose();
}
if (nowLenght>=lenght)
{
DataManager.SetPointsData(GameManager.ins.level,(int)pointsNow);
TaskEvent(1);
TaskEvent(2);
TaskEvent(3);
TaskEvent(6);
BoxDebug.Log("到达终点");
nowLenght = MaxLengh();
DataManager.SetLevelOpen(GameManager.ins.level,true);
MainPanel.ins.Win();
GameManager.ins.soundSystem.WinAudio();
playing = false;
}
Move(playerMove, nowLenght);
foreach (var data in _eventlist)
{
if (data.lenght<nowLenght)
{
playing = false;
eventObj.gameObject.SetActive(true);
eventTitle.text = JsonTab.Instance.tables.DisplayTxt.Get(JsonTab.Instance.tables.Eventgroup.Get(data.eventTeamID).EventDescribe).Text.Replace("/n","\n");
eventText1.text = JsonTab.Instance.tables.DisplayTxt.Get(JsonTab.Instance.tables.Eventtype.Get(data.eventID).Describe).Text.Replace("/n","\n");
eventText2.text = JsonTab.Instance.tables.DisplayTxt.Get(JsonTab.Instance.tables.Eventtype.Get(data.eventIDvideo).Describe).Text.Replace("/n","\n");
event1Video.SetActive(JsonTab.Instance.tables.Eventtype.Get(data.eventID).Advertisement==1);
event2Video.SetActive(JsonTab.Instance.tables.Eventtype.Get(data.eventIDvideo).Advertisement==1);
return;
}
}
}
public void AddTime(int time)
{
moveTime += time;
playing = true;
}
private void RemoveEventList()
{
for (int i = _eventlist.Count-1; i >= 0; i--)
{
if (_eventlist[i].lenght < nowLenght)
{
_eventlist.Remove(_eventlist[i]);
}
}
}
}
public class NodeEventData
{
public float lenght;
public int eventTeamID;
public int eventID;
public int eventIDvideo;
}
public class TaskEvent
{
public int id = 0;
public int type = -1;
public Action[] Action=new Action[5];
}