368 lines
16 KiB
C#
368 lines
16 KiB
C#
using UnityEngine;
|
||
using UnityEngine.UI;
|
||
using XCharts;
|
||
using XUGL;
|
||
using System.Linq;
|
||
using System.Collections;
|
||
using Mapbox.Unity.Map;
|
||
using DG.Tweening;
|
||
using System;
|
||
using Assets.Scenes.Ride.Scripts.Model;
|
||
using UnityEngine.SceneManagement;
|
||
using System.Collections.Generic;
|
||
using Assets.Cyp.Common;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace Assets.Scenes.Ride.Scripts
|
||
{
|
||
|
||
public class SingleUIManager : MonoBehaviour
|
||
{
|
||
#region UI control
|
||
GameObject target;
|
||
GameObject rightPanel;//右边列表
|
||
GameObject leftPanel;//左边列表
|
||
GameObject nextSlopePanel;//下一个坡度面板
|
||
GameObject currentSlopePanel;//当前坡度面板
|
||
GameObject selectPanel;//骑行模式选择
|
||
GameObject selectPlayer;//伴侣骑行选择窗口
|
||
GameObject reviewPanel;
|
||
GameObject titlePanel;
|
||
GameObject quitPanel;//退出界面
|
||
|
||
Text nextSlopeText;//下一个坡度
|
||
Text nextSlopeDistanceText;//下一个坡度距离
|
||
Text currentSlopeText;//当前坡度
|
||
Text currentSlopeDistanceText;//当前坡度已骑行距离
|
||
Text currentSlopeToatalDistanceText;//当前坡度总距离
|
||
Text speedTxt;//当前速度
|
||
Text powerTxt;//功率
|
||
Text timerTxt;//计时器
|
||
Text ditance;//当前骑行距离
|
||
Text totalDistance;//当前骑行距离
|
||
Text candance;//踏频
|
||
Text heartRate;//心率
|
||
Text countDownTxt;//倒计时5s
|
||
Text mapName;//路书名称
|
||
Text fps;//fps
|
||
Text totalClimb;
|
||
|
||
Button startBtn;//开始按钮
|
||
Button simpleBtn;//进入简约模式按钮
|
||
Button pauseButton;//暂停按钮
|
||
Button settingButton;//设置那妞
|
||
Button deviceButton;//设备按钮
|
||
Button exitButton;//退出按钮
|
||
Button singleButton;//单人骑行按钮
|
||
Button shadowButton;//影子选手按钮
|
||
Button addButton;//添加伴侣骑行
|
||
Button saveButton;//退出panel保存按钮
|
||
Button cancelQuitButton;//退出panel取消按钮
|
||
Button closeQuitButton;//退出panel关闭按钮
|
||
|
||
LineChart elevationChart;//海拔图
|
||
RawImage img;//海拔图头像
|
||
|
||
#endregion
|
||
|
||
#region 控制器
|
||
|
||
public PlayerController playerController;//当前用户
|
||
public CyclingController mainController;//主控制器
|
||
|
||
#endregion
|
||
|
||
private float timeRemaining = 1f;
|
||
private int count = 0;
|
||
|
||
void Awake()
|
||
{
|
||
#region 控件获取与事件绑定
|
||
fps = transform.Find("Panel/FPS").GetComponent<Text>();
|
||
leftPanel = transform.Find("Panel/ReviewPanel").gameObject;
|
||
rightPanel = transform.Find("Panel/NearPanel").gameObject;
|
||
startBtn = transform.Find("Panel/StartButton").GetComponent<Button>();
|
||
simpleBtn = transform.Find("Panel/SimpleButton").GetComponent<Button>();
|
||
ditance = transform.Find("Panel/CyclingComputerPanel/Distance").GetComponent<Text>();
|
||
titlePanel = transform.Find("Panel/TitlePanel").gameObject;
|
||
totalDistance = transform.Find("Panel/TitlePanel/TotalDistance").GetComponent<Text>();
|
||
candance = transform.Find("Panel/PowerPanel/Cadance").GetComponent<Text>();
|
||
heartRate = transform.Find("Panel/PowerPanel/Hrv").GetComponent<Text>();
|
||
nextSlopePanel = transform.Find("Panel/NextSlopePanel").gameObject;
|
||
currentSlopePanel = transform.Find("Panel/CurrentSlopePanel").gameObject;
|
||
nextSlopeText = transform.Find("Panel/NextSlopePanel/NextSlopeText").GetComponent<Text>();
|
||
nextSlopeDistanceText = transform.Find("Panel/NextSlopePanel/NextSlopeDistance").GetComponent<Text>();
|
||
currentSlopeText = transform.Find("Panel/CurrentSlopePanel/CurrentSlopeText").GetComponent<Text>();
|
||
currentSlopeDistanceText = transform.Find("Panel/CurrentSlopePanel/CurrentSlopeDistance").GetComponent<Text>();
|
||
currentSlopeToatalDistanceText = transform.Find("Panel/CurrentSlopePanel/TotalSlopeDistance").GetComponent<Text>();
|
||
speedTxt = transform.Find("Panel/CyclingComputerPanel/Speed").GetComponent<Text>();
|
||
powerTxt = transform.Find("Panel/PowerPanel/Power").GetComponent<Text>();
|
||
timerTxt = transform.Find("Panel/CyclingComputerPanel/Timer").GetComponent<Text>();
|
||
countDownTxt = transform.Find("Panel/CountDown").GetComponent<Text>();
|
||
mapName = transform.Find("Panel/TitlePanel/MapName").GetComponent<Text>();
|
||
totalClimb = transform.Find("Panel/CyclingComputerPanel/TotalClimb").GetComponent<Text>();
|
||
elevationChart = transform.Find("Panel/ElevationChart").GetComponent<LineChart>();
|
||
img = transform.Find("Panel/ElevationChart/HeadImage").GetComponent<RawImage>();
|
||
selectPanel = transform.Find("Panel/SelectPanel").gameObject;
|
||
selectPlayer = transform.Find("Panel/SelectPlayer").gameObject;
|
||
reviewPanel = transform.Find("Panel/ReviewPanel").gameObject;
|
||
quitPanel = transform.Find("Panel/QuitPanel").gameObject;
|
||
|
||
pauseButton = transform.Find("Panel/ToolBarPanel/StartOrPauseButton").GetComponent<Button>();
|
||
settingButton = transform.Find("Panel/ToolBarPanel/SettingButton").GetComponent<Button>();
|
||
deviceButton = transform.Find("Panel/ToolBarPanel/DeviceButton").GetComponent<Button>();
|
||
exitButton = transform.Find("Panel/ToolBarPanel/ExitButton").GetComponent<Button>();
|
||
singleButton = transform.Find("Panel/SelectPanel/SingleButton").GetComponent<Button>();
|
||
shadowButton = transform.Find("Panel/SelectPanel/ShadowButton").GetComponent<Button>();
|
||
addButton = transform.Find("Panel/ReviewPanel/SingleArea/AddButton").GetComponent<Button>();
|
||
saveButton = transform.Find("Panel/QuitPanel/SaveButton").GetComponent<Button>();
|
||
cancelQuitButton = transform.Find("Panel/QuitPanel/CancelButton").GetComponent<Button>();
|
||
closeQuitButton = transform.Find("Panel/QuitPanel/CloseButton").GetComponent<Button>();
|
||
|
||
target = transform.parent.Find("Target").gameObject;
|
||
|
||
startBtn.onClick.AddListener(StartRide);
|
||
simpleBtn.onClick.AddListener(ClearPanel);
|
||
pauseButton.onClick.AddListener(PauseRide);
|
||
settingButton.onClick.AddListener(ShowSettingPanel);
|
||
deviceButton.onClick.AddListener(ShowDevicePanel);
|
||
exitButton.onClick.AddListener(StopRide);
|
||
singleButton.onClick.AddListener(SingleRide);
|
||
shadowButton.onClick.AddListener(ShadowRide);
|
||
addButton.onClick.AddListener(ShowSelectPlayer);
|
||
saveButton.onClick.AddListener(SaveRide);
|
||
cancelQuitButton.onClick.AddListener(CancelQuit);
|
||
closeQuitButton.onClick.AddListener(CloseQuit);
|
||
#endregion
|
||
}
|
||
|
||
void Start()
|
||
{
|
||
playerController = FindObjectOfType<PlayerController>();
|
||
mainController = FindObjectOfType<CyclingController>();
|
||
var route = mainController.GetRoute();
|
||
mapName.text = route.RouteInstance.Name;
|
||
totalDistance.text = Math.Round(mainController.GetMapData().TotalDistance, 2).ToString() + "KM";//总里程
|
||
RenderChart();
|
||
startIndex = playerController.CurrentIndex;
|
||
}
|
||
// Update is called once per frame
|
||
float tt = 1f;
|
||
void Update()
|
||
{
|
||
tt -= Time.deltaTime;
|
||
while (tt < 0) {
|
||
fps.text = (1 / Time.deltaTime).ToString();
|
||
tt = 1;
|
||
}
|
||
//获取当前选中玩家绑定当前UI
|
||
if (playerController != null)
|
||
{
|
||
//绑定UI
|
||
speedTxt.text = $"{playerController.Speed}";
|
||
powerTxt.text = $"{playerController.Power}";
|
||
timerTxt.text = Helper.FormatTicks(playerController.TotalTicks);
|
||
ditance.text = Math.Round(playerController.TotalDistance, 2).ToString("f2")+"KM";
|
||
heartRate.text = $"{Math.Round(playerController.HeartRate, 0)}";
|
||
candance.text = $"{Math.Round(playerController.Cadance, 0)}";
|
||
//倒计时
|
||
if (count > 0)
|
||
{
|
||
timeRemaining -= Time.deltaTime;
|
||
if (timeRemaining <= 0)//定时器
|
||
{
|
||
count--;
|
||
if (count == 0)
|
||
{
|
||
playerController.SetStart();
|
||
countDownTxt.gameObject.SetActive(false);
|
||
}
|
||
timeRemaining = 1.0f;
|
||
}
|
||
}
|
||
countDownTxt.text = count.ToString();
|
||
//坡度相关数据
|
||
nextSlopeText.text = Math.Round(playerController.NextSlope, 1).ToString() + "%";
|
||
nextSlopeDistanceText.text ="-" + Math.Round(playerController.NextSlopeDistance, 0).ToString()+"M";
|
||
currentSlopeText.text = Math.Round(playerController.CurrentSlope, 1).ToString() + "%";
|
||
currentSlopeDistanceText.text = Math.Round(playerController.CurrentSlopeDistance, 0).ToString() + "M";
|
||
totalClimb.text = Math.Round(playerController.TotalClimb, 0).ToString();
|
||
//移动海拔图头像 TODO:移动所有人的头像
|
||
UpdateRealTimeChart();
|
||
//MoveChartMarkPoint();
|
||
}
|
||
}
|
||
|
||
private void LateUpdate()
|
||
{
|
||
if (target != null)
|
||
{
|
||
target.transform.position = playerController.transform.position; //new Vector3((float)Math.Round(player.transform.position.x,1), 0, (float)Math.Round(player.transform.position.z,1));
|
||
//target.transform.rotation = playerController.transform.rotation;//new Quaternion(playerController.transform.rotation.x, playerController.transform.rotation.y +1f, playerController.transform.rotation.z, playerController.transform.rotation.w);
|
||
target.transform.eulerAngles = new Vector3(0, playerController.transform.eulerAngles.y + 180f, 0);
|
||
}
|
||
}
|
||
private void SingleRide()
|
||
{
|
||
selectPanel.SetActive(false);
|
||
mainController.cyclingModel = CyclingModel.Single;
|
||
//显示单人骑行列表
|
||
reviewPanel.SetActive(true);
|
||
}
|
||
private void ShadowRide()
|
||
{
|
||
mainController.cyclingModel = CyclingModel.Review;
|
||
selectPanel.SetActive(false);
|
||
//显示影子选手骑行列表
|
||
selectPlayer.SetActive(true);
|
||
}
|
||
private void ShowSelectPlayer()
|
||
{
|
||
selectPlayer.SetActive(true);
|
||
}
|
||
private void StartRide()
|
||
{
|
||
//加个5秒钟倒计时
|
||
count = 5;
|
||
startBtn.gameObject.SetActive(false);
|
||
countDownTxt.gameObject.SetActive(true);
|
||
countDownTxt.text = count.ToString();
|
||
}
|
||
//暂停游戏
|
||
private void PauseRide()
|
||
{
|
||
playerController.SetPause();
|
||
startBtn.gameObject.SetActive(true);
|
||
}
|
||
private void SaveRide()
|
||
{
|
||
playerController.Upload();
|
||
//SceneManager.LoadScene("MainScene");
|
||
quitPanel.SetActive(false);
|
||
}
|
||
private void CancelQuit()
|
||
{
|
||
quitPanel.SetActive(false);
|
||
SceneManager.LoadScene("MainScene");
|
||
}
|
||
|
||
private void CloseQuit()
|
||
{
|
||
quitPanel.SetActive(false);
|
||
}
|
||
//结束游戏
|
||
private void StopRide()
|
||
{
|
||
quitPanel.SetActive(true);
|
||
}
|
||
//显示设备连接
|
||
private void ShowDevicePanel()
|
||
{
|
||
UIManager.ShowCustom(UIManager.Instance.DevicePanel, transform.Find("Panel").gameObject);
|
||
}
|
||
//显示设置
|
||
private void ShowSettingPanel()
|
||
{
|
||
}
|
||
int isSimple = 1;
|
||
|
||
private bool clearLock = true;
|
||
private void ClearPanel()
|
||
{
|
||
if (clearLock)
|
||
{
|
||
clearLock = false;
|
||
leftPanel.transform.DOLocalMove(new Vector3(leftPanel.transform.localPosition.x + isSimple * -430f, leftPanel.transform.localPosition.y, leftPanel.transform.localPosition.z), 1);
|
||
titlePanel.transform.DOLocalMove(new Vector3(titlePanel.transform.localPosition.x, titlePanel.transform.localPosition.y + isSimple * 180f, titlePanel.transform.localPosition.z), 1);
|
||
rightPanel.transform.DOLocalMove(new Vector3(rightPanel.transform.localPosition.x + isSimple * 330f, rightPanel.transform.localPosition.y, rightPanel.transform.localPosition.z), 1).onComplete += () =>
|
||
{
|
||
clearLock = true;
|
||
isSimple *= -1;
|
||
};
|
||
}
|
||
}
|
||
|
||
private int maxCacheDataNumber = 100;//海拔图最大点数
|
||
private int totalCount;//路线实际点数
|
||
private int startIndex;//选中玩家起始索引
|
||
private int interval = 50;//选中玩家移动索引个数
|
||
private float[] elevationList;
|
||
//初始化海拔图
|
||
void RenderChart()
|
||
{
|
||
elevationChart.ClearData();
|
||
elevationList = mainController.GetLineChartData();
|
||
totalCount = elevationList.Length;
|
||
maxCacheDataNumber = Math.Min(totalCount, maxCacheDataNumber);
|
||
elevationChart.SetMaxCache(maxCacheDataNumber);
|
||
//for (int i = 0; i < maxCacheDataNumber; i++)
|
||
//{
|
||
// elevationChart.AddData(0, elevationList[i]);
|
||
//}
|
||
//elevationChart.RefreshChart();
|
||
|
||
var endIndex = playerController.CurrentIndex;
|
||
var s = endIndex / maxCacheDataNumber;
|
||
var v = endIndex % maxCacheDataNumber;
|
||
|
||
maxCacheDataNumber = Math.Min(totalCount-s* maxCacheDataNumber, maxCacheDataNumber);
|
||
|
||
for (int i = 0; i < maxCacheDataNumber; i++)
|
||
{
|
||
initCount++;
|
||
if (initCount > maxCacheDataNumber) break;
|
||
AddOneData(s * maxCacheDataNumber + v + i);
|
||
}
|
||
|
||
elevationChart.RefreshChart();
|
||
if (App.CurrentUser != null)
|
||
{
|
||
Utils.DisplayImage(StartCoroutine, img, App.CurrentUser.WxHeadImg);
|
||
}
|
||
}
|
||
//长路线动态更新chart
|
||
private int initCount;
|
||
private float updateTime;
|
||
private int lastIndex;
|
||
private int chartAddNum;
|
||
void UpdateRealTimeChart()
|
||
{
|
||
var endIndex = playerController.CurrentIndex;
|
||
updateTime += Time.deltaTime;
|
||
if (updateTime >= 1)
|
||
{
|
||
int delta = endIndex - lastIndex;
|
||
if (delta >0 && initCount < totalCount)
|
||
{
|
||
lastIndex = endIndex;
|
||
AddOneData(initCount);
|
||
initCount++;
|
||
updateTime = 0;
|
||
chartAddNum++;
|
||
}
|
||
var v = (endIndex - chartAddNum) % maxCacheDataNumber;
|
||
MoveChartMarkPoint(v);
|
||
}
|
||
}
|
||
void AddOneData(int index)
|
||
{
|
||
string category = index.ToString();
|
||
elevationChart.AddData(0, elevationList[index]);
|
||
//elevationChart.AddXAxisData(category);
|
||
//elevationChart.AddXAxisData(index.ToString(), index);
|
||
|
||
}
|
||
|
||
void MoveChartMarkPoint(int index)
|
||
{
|
||
var dataPoints = elevationChart.series.list[0].dataPoints.OrderBy(c=>c.x).ToList();
|
||
if (dataPoints.Count > 0)
|
||
{
|
||
var pinLoction = dataPoints[index];
|
||
pinLoction.y = img.transform.localPosition.y;
|
||
img.transform.localPosition = pinLoction;
|
||
}
|
||
}
|
||
}
|
||
}
|