278 lines
10 KiB
C#
278 lines
10 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;
|
||
|
||
namespace Assets.Scenes.Ride.Scripts
|
||
{
|
||
|
||
public class SingleUIManager : MonoBehaviour
|
||
{
|
||
#region UI control
|
||
[SerializeField]
|
||
Text Fps;//开始按钮
|
||
[SerializeField]
|
||
Button startBtn;//开始按钮
|
||
[SerializeField]
|
||
Button simpleBtn;//进入简约模式按钮
|
||
[SerializeField]
|
||
Text ditance;//当前骑行距离
|
||
[SerializeField]
|
||
Text totalDistance;//当前骑行距离
|
||
[SerializeField]
|
||
Text candance;//踏频
|
||
[SerializeField]
|
||
Text heartRate;//心率
|
||
[SerializeField]
|
||
GameObject rightPanel;//右边列表
|
||
[SerializeField]
|
||
GameObject leftPanel;//左边列表
|
||
[SerializeField]
|
||
GameObject nextSlopePanel;//下一个坡度面板
|
||
[SerializeField]
|
||
GameObject currentSlopePanel;//当前坡度面板
|
||
[SerializeField]
|
||
Text nextSlopeText;//下一个坡度
|
||
[SerializeField]
|
||
Text nextSlopeDistanceText;//下一个坡度距离
|
||
[SerializeField]
|
||
Text currentSlopeText;//当前坡度
|
||
[SerializeField]
|
||
Text currentSlopeDistanceText;//当前坡度距离
|
||
[SerializeField]
|
||
Text speedTxt;//当前速度
|
||
[SerializeField]
|
||
Text powerTxt;//功率
|
||
[SerializeField]
|
||
Text timerTxt;//计时器
|
||
[SerializeField]
|
||
Text countDownTxt;//倒计时5s
|
||
[SerializeField]
|
||
Text mapName;//路书名称
|
||
[SerializeField] LineChart elevationChart;//海拔图
|
||
[SerializeField] RawImage img;//当前用户头像
|
||
[SerializeField]
|
||
Button StartOrPauseButton;//暂停按钮
|
||
[SerializeField]
|
||
Button SettingButton;//设置那妞
|
||
[SerializeField]
|
||
Button DeviceButton;//设备按钮
|
||
[SerializeField]
|
||
Button ExitButton;//退出按钮
|
||
|
||
[SerializeField]
|
||
GameObject target;//开始按钮
|
||
#endregion
|
||
|
||
#region 控制器
|
||
|
||
PlayerController playerController;//当前用户
|
||
CyclingController mainController;//主控制器
|
||
|
||
#endregion
|
||
|
||
private float timeRemaining = 1f;
|
||
private int count = 0;
|
||
|
||
void Awake()
|
||
{
|
||
startBtn.onClick.AddListener(StartRide);
|
||
simpleBtn.onClick.AddListener(ClearPanel);
|
||
StartOrPauseButton.onClick.AddListener(PauseRide);
|
||
SettingButton.onClick.AddListener(ShowSettingPanel);
|
||
DeviceButton.onClick.AddListener(ShowDevicePanel);
|
||
ExitButton.onClick.AddListener(StopRide);
|
||
}
|
||
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";
|
||
//移动海拔图头像 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 = new Quaternion(playerController.transform.rotation.x, playerController.transform.rotation.y + 180, playerController.transform.rotation.z, playerController.transform.rotation.w);
|
||
}
|
||
}
|
||
|
||
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 StopRide()
|
||
{
|
||
//TODO:主动保存或者舍弃保存
|
||
//切换到路数列表
|
||
SceneManager.LoadScene("MainScene");
|
||
|
||
}
|
||
//显示设备连接
|
||
private void ShowDevicePanel()
|
||
{
|
||
}
|
||
//显示设置
|
||
private void ShowSettingPanel()
|
||
{
|
||
}
|
||
int isSimple = 1;
|
||
//TODO:简约模式
|
||
private void ClearPanel()
|
||
{
|
||
leftPanel.transform.DOLocalMove(new Vector3(leftPanel.transform.localPosition.x + isSimple * -430f, leftPanel.transform.localPosition.y, leftPanel.transform.localPosition.z), 1);
|
||
rightPanel.transform.DOLocalMove(new Vector3(rightPanel.transform.localPosition.x+ isSimple* 330f, rightPanel.transform.localPosition.y, rightPanel.transform.localPosition.z), 1);
|
||
//elevationChart.transform.DOMoveX(elevationChart.transform.position.x + isSimple * 286.1764f, 1);
|
||
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();
|
||
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)
|
||
{
|
||
elevationChart.AddData(0, elevationList[index]);
|
||
//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;
|
||
}
|
||
}
|
||
}
|
||
}
|