318 lines
8.6 KiB
C#
Raw Normal View History

using Assets.Scenes.Ride.Scripts.Model.CyclingModels;
using Assets.Scripts.Apis.Models;
using ChartAndGraph;
using DG.Tweening;
2021-04-01 11:01:29 +08:00
using GeoJSON.Net.Geometry;
using Mapbox.Unity.Map;
using Mapbox.Utils;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TurfCS;
using UnityEngine;
using UnityEngine.UI;
2021-04-01 11:01:29 +08:00
namespace Assets.Scenes.Ride.Scripts
{
public abstract class AbstractPlayer: MonoBehaviour
{
[Header("Character")]
[SerializeField]
GameObject character;
[SerializeField]
Animator characterAnimator;
2021-04-15 10:13:01 +08:00
protected AbstractMap map;
//AbstractMap map;
public bool IsShowInfo { get; set; }
2021-04-01 11:01:29 +08:00
#region
public Vector3 nextPos;
Vector3 prePos = Vector3.zero;
2021-04-01 11:01:29 +08:00
float timer = 1.0f;//计时器
#endregion
#region
protected Vector2d nextlatlong; //下一个点的坐标
protected Vector2d currentlatlong; //当前坐标
2021-04-15 17:09:35 +08:00
//protected bool isStart;//开始或者暂停
2021-04-21 14:08:45 +08:00
//public bool isQuit;//true 中途退出 或者到达终点
2021-04-01 11:01:29 +08:00
protected bool isMajor;//是否是主人公
protected MapDataModel mapData;
2021-05-08 18:36:59 +08:00
//protected int userId;
2021-04-01 11:01:29 +08:00
protected DateTime startTime;//开始骑行时间
protected DateTime endTime;//结束骑行时间
protected double weight;//体重
protected double bicycleWeight;//车重
protected double speed;
protected double power;
protected double elevation;
protected double cadance;
protected int heartRate;
protected int ticks;
protected double totalDistance;
protected double currentSlope;
protected double nextSlope;
protected double nextSlopeDistance;
protected double distance;
protected double currentSlopeDistance;
protected double lastEndDistance;
2021-04-15 10:13:01 +08:00
protected double totalClimb;
2021-04-01 11:01:29 +08:00
2021-04-15 10:13:01 +08:00
public double TotalClimb { get => totalClimb; }
2021-05-08 18:36:59 +08:00
public int UserId { set; get; }//{ get => userId; }
2021-04-01 11:01:29 +08:00
public double Speed { get => speed; }
public double Power { get => power; }
public double Cadance { get => cadance; }
public double HeartRate { get => heartRate; }
public int TotalTicks { get => ticks; }
public double TotalDistance { get => totalDistance; }
2021-04-23 19:28:35 +08:00
public double Distance { get => distance; }
2021-04-01 11:01:29 +08:00
public double CurrentSlope { get => currentSlope; }
public double NextSlope { get => nextSlope; }
public double NextSlopeDistance { get => nextSlopeDistance; }
public double CurrentSlopeDistance { get => currentSlopeDistance; }
public double Elevation { get => elevation; }
public double LastEndDistance { get => lastEndDistance; }
public Vector2d Currentlatlong { get => currentlatlong; }
2021-04-01 11:01:29 +08:00
#endregion
#region UI对象
2021-04-01 11:01:29 +08:00
//UI对象
protected GameObject headPanel;
protected GraphChartBase graph;
protected ChartDataSourceScript chartDataSourceScript;
#endregion
2021-04-01 11:01:29 +08:00
void Start()
{
Init();
}
void Update()
{
Excute();
AfterExcute();
}
protected virtual void AfterExcute()
{
}
2021-04-15 10:13:01 +08:00
#region
//初始化骑行数据
protected CyclingController mainController;
protected BaseCycling cyclingExcutor;
2021-04-01 11:01:29 +08:00
protected virtual void Init()
{
2021-04-21 14:08:45 +08:00
characterAnimator = GetComponentInChildren<Animator>();
mainController = transform.parent.GetComponent<CyclingController>();
2021-04-15 10:13:01 +08:00
map = transform.parent.Find("Map").GetComponent<AbstractMap>();
2021-04-01 11:01:29 +08:00
mapData = mainController.GetMapData();//获取路书信息
//获取海拔图UI对象
var graphObject = transform.parent.Find("SingleUI/Panel/GraphChart");
chartDataSourceScript = graphObject.GetComponent<ChartDataSourceScript>();
graph = graphObject.GetComponent<GraphChartBase>();
2021-04-01 11:01:29 +08:00
}
protected virtual void Excute()
{
//CamControl();
2021-04-01 11:01:29 +08:00
timer -= Time.deltaTime;
if (timer <= 0)//定时器 一秒执行一次
{
BeforeRun();
2021-04-01 11:01:29 +08:00
Run();
timer = 1.0f;
}
}
2021-04-27 20:28:03 +08:00
public bool stopRecord = false;
2021-04-01 11:01:29 +08:00
//骑行中
protected virtual void Run()
{
if (mapData == null)
return;
2021-04-01 11:01:29 +08:00
ComputeNextSlope();//计算下一个坡度相关数据
MoveGraphHead();//移动海拔图头像
2021-04-21 14:08:45 +08:00
//人物动画控制
if (characterAnimator != null)
2021-04-21 14:08:45 +08:00
{
characterAnimator.SetFloat("Speed", (float)speed);
2021-05-14 19:26:15 +08:00
characterAnimator.SetFloat("Slope", (float)currentSlope);
if (currentSlope > 5)
{
characterAnimator.speed = 0.5f;
}
else
{
characterAnimator.speed = 1f;
}
2021-05-14 19:26:15 +08:00
if (totalDistance >= mapData.TotalDistance)
2021-04-21 14:08:45 +08:00
{
characterAnimator.SetBool("ReachEnd", true);//到达终点
}
}
2021-04-21 14:08:45 +08:00
//开始骑行
if (GetStart())
2021-04-01 11:01:29 +08:00
{
2021-04-15 17:09:35 +08:00
ticks++;
2021-04-01 11:01:29 +08:00
Compute();//接受蓝牙设备数据计算
2021-04-21 14:08:45 +08:00
2021-04-15 17:29:32 +08:00
if (totalDistance > mapData.TotalDistance)
2021-04-01 11:01:29 +08:00
{
2021-04-15 17:29:32 +08:00
distance = totalDistance - mapData.TotalDistance;
2021-04-01 11:01:29 +08:00
totalDistance = mapData.TotalDistance;
2021-04-27 20:28:03 +08:00
stopRecord = true;
StartCoroutine("LateUpload");
2021-04-01 11:01:29 +08:00
}
2021-04-15 17:29:32 +08:00
//数据处理
2021-04-27 20:28:03 +08:00
nextPos = map.GeoToWorldPosition(currentlatlong);//下一个点
nextPos.y += 1.35f;//提高y轴让人物站在地图上面
2021-04-15 17:29:32 +08:00
prePos = transform.localPosition;//当前点
thisRotation = transform.localRotation;
2021-04-21 14:08:45 +08:00
//移动动画控制
if (distance > 0)
2021-04-15 17:29:32 +08:00
{
StartCoroutine(MoveTo());//移动
}
}
2021-04-12 17:35:56 +08:00
}
//游戏开始开关
public virtual bool GetStart()
{
return mainController.isStart;
}
protected virtual void BeforeRun()
2021-04-12 17:35:56 +08:00
{
2021-04-01 11:01:29 +08:00
}
//计算功率 速度 当前骑行总里程M心率 踏频 等
protected virtual void Compute()
{
2021-04-27 20:28:03 +08:00
2021-04-01 11:01:29 +08:00
}
//当前用户调用来上传骑行记录
2021-04-15 10:13:01 +08:00
public virtual void Upload()
2021-04-01 11:01:29 +08:00
{
}
#endregion
#region
IEnumerator LateUpload()
{
yield return new WaitForSeconds(1);
Upload();
StopCoroutine("LateUpload");
}
2021-04-01 11:01:29 +08:00
public int CurrentIndex;
2021-04-15 10:13:01 +08:00
private int index;
2021-04-23 19:28:35 +08:00
public double CurrentDistance;//当前所处区间距离
2021-04-27 15:15:51 +08:00
public double NextSlopeTotalDistance;
2021-04-01 11:01:29 +08:00
//当前距离所在的海拔/坡度/距离 下一个点的坡度以及剩余距离
void ComputeNextSlope()
{
double sumDistance = 0;
2021-04-19 18:03:04 +08:00
if (mapData == null)
return;
2021-04-01 11:01:29 +08:00
var pointList = mapData.List;
2021-04-15 17:09:35 +08:00
int preIndex = 0;
2021-04-01 11:01:29 +08:00
for (int i = 0; i < pointList.Count; i++)
{
sumDistance += pointList[i].Distance;
if (totalDistance * 1000 <= sumDistance)
{
index = i;
break;
}
}
2021-04-27 15:15:51 +08:00
preIndex = index > 0 ? index - 1:0;//前一个索引
CurrentIndex = index;//当前索引
int nextIndex = index == pointList.Count - 1 ? index : index + 1; //计算下一个点的坡度和距离
PreElevation = pointList[preIndex].Elevation; //计算上一个海拔
PreSlope = pointList[preIndex].Grade; //计算上一个坡度
//计算当前海拔和坡度&当前区间距离
elevation = pointList[CurrentIndex].Elevation;
currentSlope = pointList[CurrentIndex].Grade;
CurrentDistance = pointList[CurrentIndex].Distance;
//计算下一个海拔和坡度&当前区间距离
2021-04-01 11:01:29 +08:00
nextSlope = pointList[nextIndex].Grade;
nextSlopeDistance = sumDistance - totalDistance * 1000;
2021-04-27 15:15:51 +08:00
NextSlopeTotalDistance = pointList[nextIndex].Distance;
currentSlopeDistance = (totalDistance * 1000 - (sumDistance - pointList[index].Distance));// CurrentDistance-( totalDistance * 1000 - (sumDistance - pointList[index].Distance));
2021-04-01 11:01:29 +08:00
}
2021-04-25 13:33:43 +08:00
public double PreElevation;
public double PreSlope;
2021-04-27 20:28:03 +08:00
2021-04-01 11:01:29 +08:00
#endregion
#region
Quaternion thisRotation;
public Quaternion currentRotation;
2021-04-01 11:01:29 +08:00
IEnumerator LookAtNextPos()
{
if (prePos != nextPos)
2021-04-01 11:01:29 +08:00
{
Quaternion neededRotation = Quaternion.LookRotation(prePos - nextPos);
float t = 0;
while (t < 1.0f)
{
t += Time.deltaTime / 0.5f;
currentRotation = Quaternion.Slerp(thisRotation, neededRotation, t);
character.transform.rotation = Quaternion.Euler(0, currentRotation.eulerAngles.y, 0);
yield return null;
}
2021-04-01 11:01:29 +08:00
}
}
Vector3 deltaPos = Vector3.zero;
Vector3 previousPos = Vector3.zero;
void CamControl()
{
deltaPos = transform.position - previousPos;
deltaPos.y = 0;
Camera.main.transform.position = Vector3.Lerp(Camera.main.transform.position, Camera.main.transform.position + deltaPos, Time.time);
previousPos = transform.position;
}
public Vector3 currentPos = new Vector3(0,0,0);
2021-04-01 11:01:29 +08:00
//人物移动控制
IEnumerator MoveTo()
{
StartCoroutine(LookAtNextPos());//转向
2021-04-01 11:01:29 +08:00
//让人物移动分点增加动画的流畅度
float t = 0;
while (t < 1)
{
t += Time.deltaTime;
2021-04-12 17:35:56 +08:00
2021-04-01 11:01:29 +08:00
Vector3 v = Vector3.Lerp(prePos, nextPos, t);
currentPos = v;
transform.localPosition = v;
2021-04-12 17:35:56 +08:00
yield return new WaitForEndOfFrame();
2021-04-01 11:01:29 +08:00
}
}
#endregion
#region UI行为
protected virtual void MoveGraphHead()
{
}
#endregion
2021-04-01 11:01:29 +08:00
}
}