powerfun-unity/Assets/TestPlayerController.cs
2022-03-04 16:12:15 +08:00

109 lines
3.4 KiB
C#

using Assets.Scenes.Ride.Scripts;
using Assets.Scenes.Ride.Scripts.Model;
using Assets.Scripts;
using Assets.Scripts.Apis.Models;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
using DG.Tweening;
using UnityEngine.Video;
public class TestPlayerController : MonoBehaviour
{
UnityWebRequest request = null;
MapDataModel mapData;
MapRoute mapRoute;
int ticks = 0;
float timer = 1f;
double heartRate = 0;
public double power = 800;
double weight = 60;
double bicycleWeight = 10;
double elevation = 0f;
double currentSlope = 0f;
double speed = 0f;
double distance = 0f;
double totalDistance = 0f;
double bearingInterval = 0f;//线路当前转向
Animator animator;
VideoPlayer videoPlayer;
// Start is called before the first frame update
void Start()
{
//测试数据
var routeId = 1255;
mapData = ConfigHelper.mapApi.GetData(routeId);//获取路书地理数据
mapRoute = ConfigHelper.mapApi.GetById(routeId).data;
animator = transform.GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
timer -= Time.deltaTime;
while (timer <= 0)
{
//var animInfo = animator.GetCurrentAnimatorStateInfo(0);
//if (animInfo.normalizedTime < 0.6)
//{
// if (speed > 30)
// {
// animator.Play("New State Right");
// }
// else if (speed > 15)
// {
// animator.Play("RideLoop_Medium_01 1");
// }
// else if (speed == 0)
// {
// animator.Play("OnBicycle_01_Idle 0");
// videoPlayer.Pause();
// }
// else
// {
// animator.Play("RideLoop_01");
// }
//}
ticks++;
ComputeNextSlope();
speed = Helper.CalculateSpeed(elevation, currentSlope, power, weight, bicycleWeight);
distance = Math.Round(speed / 3600, 5, MidpointRounding.AwayFromZero);
totalDistance += distance;
Debug.Log($"ticks:{ticks} speed:{speed} totalDistance:{totalDistance} bearing:{bearingInterval}");
timer += 1f;
transform.DORotate(new Vector3(0f, (float)(180 + bearingInterval), 0f), 1);
}
}
int currentIndex = 0;
void ComputeNextSlope()
{
double sumDistance = 0;
if (mapData == null)
return;
var pointList = mapData.List;
for (int i = 0; i < pointList.Count; i++)
{
sumDistance += pointList[i].Distance;
decimal left = (decimal)totalDistance * 1000;
decimal right = (decimal)sumDistance;
if (left <= right)
{
currentIndex = i;
break;
}
}
var DOUBLE_DELTA = 1E-6;
if (Math.Abs(totalDistance - mapData.TotalDistance) < DOUBLE_DELTA)
{
currentIndex = pointList.Count - 1;
}
elevation = pointList[currentIndex].Elevation;
currentSlope = pointList[currentIndex].Grade;
var preindex = currentIndex - 1 >= 0 ? currentIndex - 1 : 0;
bearingInterval = pointList[currentIndex].Bearing - pointList[preindex].Bearing;
}
}