powerfun-unity/Assets/Scripts/Scenes/VideoRide/AbstractVideoPlayer.cs

377 lines
12 KiB
C#
Raw Normal View History

2022-03-24 09:36:59 +08:00
using Assets.Scenes.Ride.Scripts;
using Assets.Scripts.Apis.Models;
2022-03-10 18:32:53 +08:00
using DG.Tweening;
using Mapbox.Utils;
using System;
using System.Collections;
2022-04-01 18:41:31 +08:00
using System.Linq;
2022-03-10 18:32:53 +08:00
using UnityEngine;
2022-03-30 18:41:06 +08:00
using UnityEngine.UI;
2022-03-10 18:32:53 +08:00
namespace Assets.Scripts.Scenes.VideoRide
{
public abstract class AbstractVideoPlayer : MonoBehaviour
{
protected Animator animator;
2022-03-24 09:36:59 +08:00
public GameObject head { get; set; }
2022-03-30 18:41:06 +08:00
protected Image ftpImage { get; set; }
protected Text headName { get; set; }
protected Text headWkg { get; set; }
2022-03-24 09:36:59 +08:00
public int UserId;
2022-03-30 18:41:06 +08:00
public string UserName;
public double weight;//体重
2022-03-10 18:32:53 +08:00
protected double bicycleWeight;//车重
protected double preSpeed;
2022-03-24 09:36:59 +08:00
public double speed;
public double power;
2022-03-10 18:32:53 +08:00
protected double elevation;
2022-03-25 09:57:30 +08:00
public double cadance;
2022-03-30 18:41:06 +08:00
public double wkg;
2022-03-25 09:57:30 +08:00
public int? heartRate { get; set; }
2022-03-10 18:32:53 +08:00
public int ticks;
2022-03-24 09:36:59 +08:00
public double totalDistance;
2022-03-30 18:41:06 +08:00
public double currentSlope;
2022-03-10 18:32:53 +08:00
protected double nextSlope;
protected double nextSlopeDistance;
public double distance;
2022-03-10 18:32:53 +08:00
protected double currentSlopeDistance;
protected double lastEndDistance;
2022-03-24 09:36:59 +08:00
public double totalClimb;
2022-03-31 18:40:19 +08:00
public Vector2d currentlatLon { get; set; }
2022-03-25 09:57:30 +08:00
public int currentIndex;
public float bearing = 0f;
2022-03-10 18:32:53 +08:00
protected MapDataModel mapData;
2022-03-24 09:36:59 +08:00
protected bool isHit = false;
protected RaycastHit hit;
2022-03-10 18:32:53 +08:00
float timer = 1f;
protected bool start = true;
protected Sequence sequence;
2022-03-10 18:32:53 +08:00
protected VideoGameManager manager { get; set; }
Camera camera;
float currenPlayerHeight;
Transform bone_bottle_2 { get; set; }
2022-03-25 09:57:30 +08:00
2022-03-10 18:32:53 +08:00
protected virtual void Start()
{
animator = GetComponent<Animator>();
manager = FindObjectOfType<VideoGameManager>();
mapData = manager.GetMapData();
ComputeNextSlope();//初始化坡度等
2022-03-31 18:40:19 +08:00
if (speed > 0)
{
animator.Play("rideLoop");
}
else
{
animator.Play("idle");
}
camera = Camera.main;
//bone_bottle_2
bone_bottle_2 = transform.Find("bone_cable_20");
2022-03-10 18:32:53 +08:00
}
protected virtual void Update()
2022-03-10 18:32:53 +08:00
{
timer -= Time.deltaTime;
2022-03-24 09:36:59 +08:00
CreateHeadImage();
2022-03-10 18:32:53 +08:00
while (timer <= 0)
{
try
{
2022-03-25 09:57:30 +08:00
ComputeNextSlope();//计算下一个坡度
ComputePlayer();//计算人物属性
if (GetStart())
2022-03-10 18:32:53 +08:00
{
ticks++;
2022-03-25 09:57:30 +08:00
Forward();
2022-03-10 18:32:53 +08:00
ComputeRecord();
ComputeVideo();
2022-03-24 09:36:59 +08:00
RayCastHit();
Turn();
2022-03-10 18:32:53 +08:00
}
ComputeAnimator();//控制动画
2022-03-10 18:32:53 +08:00
timer += 1f;
}
catch (Exception e)
{
power = 0;
speed = 0;
2022-03-24 09:36:59 +08:00
Debug.LogError(e.Message);
}
2022-03-10 18:32:53 +08:00
}
2022-03-24 09:36:59 +08:00
2022-03-10 18:32:53 +08:00
}
2022-03-10 18:32:53 +08:00
//人物碰撞
void OnCollisionEnter(Collision collision)
{
2022-03-24 09:36:59 +08:00
}
void RayCastHit()
{
Vector3 pos = transform.position;
Ray ray = new Ray(pos, new Vector3(0, 0, 1));
isHit = Physics.Raycast(ray, out hit, 1f, 1 << 0, QueryTriggerInteraction.Collide);
2022-03-10 18:32:53 +08:00
}
protected virtual bool GetStart()
{
return true;
}
2022-03-10 18:32:53 +08:00
//动画状态机
2022-03-24 09:36:59 +08:00
protected virtual void ComputeAnimator()
2022-03-10 18:32:53 +08:00
{
if (animator != null)
{
animator.SetFloat("preSpeed", (float)preSpeed);
animator.SetFloat("speed", (float)speed);
animator.SetFloat("grade", (float)currentSlope);
2022-03-24 09:36:59 +08:00
animator.SetFloat("power", (float)power);
animator.SetFloat("bearing", bearing);
2022-03-10 18:32:53 +08:00
}
}
2022-03-24 09:36:59 +08:00
protected virtual int GetCurrentFrame()
{
return manager.GetCurrentFrame();
}
2022-03-24 09:36:59 +08:00
private bool animated = false;
private float offset = 0;
//控制角色进行向左或向右
protected virtual void Turn()
{
2022-03-24 09:36:59 +08:00
var currentFrame = GetCurrentFrame();
2022-04-01 18:41:31 +08:00
var result = manager.mockDirection.Where(c => c.Start < currentFrame && c.End > currentFrame).FirstOrDefault();
if (result != null)
2022-03-24 09:36:59 +08:00
{
2022-04-01 18:41:31 +08:00
bearing = result.Value;
2022-03-24 09:36:59 +08:00
}
else
{
bearing = 0;
}
2022-03-24 09:36:59 +08:00
animator.SetFloat("bearing", bearing);
var headBack = ticks % 60 == 0 && speed > 0 && bearing == 0;
var drink = ticks % 125 == 0 && speed > 0 && bearing == 0;
animator.SetBool("headBack", headBack);
animator.SetBool("drinking", drink);
var waiting = ratio == 0 ? 1 : 1 / ratio;
if (Math.Abs(bearing) > 30)
2022-03-24 09:36:59 +08:00
{
2022-04-01 18:41:31 +08:00
if (!animated)
2022-03-24 09:36:59 +08:00
{
2022-04-01 18:41:31 +08:00
animated = true;
var tween = transform.DORotate(new Vector3(0, bearing, -bearing/5), waiting);
tween.onComplete += () =>
2022-04-01 18:41:31 +08:00
{
var zoffset = bearing / 10.5f;
var doMoveX = transform.DOMoveX(transform.position.x + zoffset, waiting);
doMoveX.onComplete += () =>
{
offset = -zoffset;
};
2022-04-01 18:41:31 +08:00
};
tween.SetEase(Ease.InOutSine).SetLoops(2, LoopType.Incremental);
2022-04-01 18:41:31 +08:00
}
2022-03-24 09:36:59 +08:00
}
else
2022-03-24 09:36:59 +08:00
{
2022-04-01 18:41:31 +08:00
if (offset != 0)
2022-03-24 09:36:59 +08:00
{
var cpoffset = offset;
transform.DORotate(new Vector3(0, bearing, 0), 1.2f * waiting).onComplete += () =>
2022-04-01 18:41:31 +08:00
{
transform.DOMoveX(transform.position.x + cpoffset, 1.2f * waiting).onComplete += () =>
{
animated = false;
};
2022-04-01 18:41:31 +08:00
};
offset = 0;
2022-03-24 09:36:59 +08:00
}
else
{
transform.DORotate(new Vector3(0, bearing, -bearing), waiting);
}
2022-04-01 18:41:31 +08:00
}
2022-03-10 18:32:53 +08:00
}
//计算人物当前属性
2022-03-24 09:36:59 +08:00
protected virtual void ComputePlayer()
{
if (power > 0)
{
preSpeed = speed;
speed = Helper.CalculateSpeed(elevation, currentSlope, power, weight, bicycleWeight);
}
else
{
speed = 0;
distance = 0;
}
2022-03-31 18:40:19 +08:00
//一旦有功率就开始骑行、否则暂停
2022-04-01 18:41:31 +08:00
if (!manager.IsQuit() && manager.CurrentPlayer.UserId == UserId && !manager.IsQuit())
2022-03-31 18:40:19 +08:00
{
if (power > 0)
{
manager.StartGame();
}
}
2022-03-24 09:36:59 +08:00
}
2022-03-10 18:32:53 +08:00
protected virtual void Forward()
2022-03-25 09:57:30 +08:00
{
2022-03-31 18:40:19 +08:00
try
{
if (mapData == null)
mapData = manager.GetMapData();
currentlatLon = manager.Along(totalDistance % mapData.TotalDistance);
////根据人物速度设置人物距离摄像头远近
//if (speed > 20 && speed < 30)
//{
// transform.DOMoveZ(3, 1f);
//}
//else if (speed > 30 && speed < 40)
//{
// transform.DOMoveZ(3.5f, 1f);
//}
//else if (speed > 40)
//{
// transform.DOMoveZ(4, 1f);
//}
//else
//{
// transform.DOMoveZ(3, 1f);
//}
2022-03-31 18:40:19 +08:00
}
catch (Exception e)
{
Debug.LogError(e);
}
2022-03-25 09:57:30 +08:00
}
protected float ratio = 1;
2022-03-25 09:57:30 +08:00
2022-03-31 18:40:19 +08:00
protected virtual void ComputeVideo()
{
mapData = manager.GetMapData();
2022-03-31 18:40:19 +08:00
if (currentIndex + 1 < mapData.List.Count)
{
ratio = (float)(speed / mapData.List[currentIndex + 1].Speed);
}
else
{
ratio = (float)(speed / mapData.List[currentIndex].Speed);
}
if (manager.CurrentPlayer != null && manager.CurrentPlayer.UserId != UserId)
return;
if (ratio > 1)
{
ratio = Math.Min(ratio, 1.2f);
}
if (ratio < 1)
{
ratio = Math.Max(ratio, 0.6f);
}
var info = animator.GetCurrentAnimatorClipInfo(0);
var currentClip = info.FirstOrDefault();
if (currentClip.clip != null && currentClip.clip.isLooping)
{
animator.speed = ratio;
}
2022-03-31 18:40:19 +08:00
manager.Play(ratio);
2022-03-31 18:40:19 +08:00
}
2022-03-10 18:32:53 +08:00
protected virtual void ComputeRecord() { }
//计算当前区段属性下一个区段属性
void ComputeNextSlope()
{
double sumDistance = 0;
2022-03-31 18:40:19 +08:00
mapData = manager.GetMapData();
2022-03-10 18:32:53 +08:00
if (mapData == null)
return;
var pointList = mapData.List;
int preIndex = 0;
for (int i = 0; i < pointList.Count; i++)
{
sumDistance += pointList[i].Distance;
//decimal left = (decimal)totalDistance * 1000;
decimal left = (decimal)totalDistance % (decimal)mapData.TotalDistance;//处理多圈的情况
left *= 1000;
2022-03-10 18:32:53 +08:00
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;
}
preIndex = currentIndex > 0 ? currentIndex - 1 : 0;//前一个索引
int nextIndex = currentIndex == pointList.Count - 1 ? currentIndex : currentIndex + 1; //计算下一个点的坡度和距离
elevation = pointList[currentIndex].Elevation;
currentSlope = pointList[currentIndex].Grade;
//CurrentDistance = pointList[currentIndex].Distance;
//计算下一个海拔和坡度&当前区间距离
nextSlope = pointList[nextIndex].Grade;
nextSlopeDistance = sumDistance - totalDistance * 1000;
//NextSlopeTotalDistance = pointList[nextIndex].Distance;
currentSlopeDistance = (totalDistance * 1000 - (sumDistance - pointList[currentIndex].Distance));
//计算累计爬升
totalClimb = 0;
for (int i = 1; i <= currentIndex; i++)
{
var diff = mapData.List[i].Elevation - mapData.List[i - 1].Elevation;
if (diff > 0)
{
totalClimb += diff;
}
}
}
2022-03-24 09:36:59 +08:00
public void CreateHeadImage()
{
if (head == null)
{
if (manager.GetHeadInfo() != null)
2022-03-30 18:41:06 +08:00
{
2022-03-24 09:36:59 +08:00
head = Instantiate(manager.GetHeadInfo(), manager.GetCanvasTransform());
2022-03-30 18:41:06 +08:00
ftpImage = head.transform.Find("ftp").GetComponent<Image>();
headName = head.transform.Find("name").GetComponent<Text>();
headWkg = head.transform.Find("wkg").GetComponent<Text>();
}
2022-03-24 09:36:59 +08:00
}
if (head != null)
{
//它们的乘积就是高度
Vector3 worldPosition = new Vector3(bone_bottle_2.position.x, bone_bottle_2.position.y+0.8f, bone_bottle_2.position.z);
2022-03-24 09:36:59 +08:00
var playerScreenPos = Camera.main.WorldToScreenPoint(worldPosition);
head.transform.position = playerScreenPos;
2022-03-30 18:41:06 +08:00
ftpImage.fillAmount = (float)(wkg / 6);
headName.text = UserName;
headWkg.text = $"{wkg}W/KG";
2022-03-24 09:36:59 +08:00
}
}
2022-03-10 18:32:53 +08:00
2022-03-24 09:36:59 +08:00
public void Destroy()
{
2022-03-31 18:40:19 +08:00
if (manager.CurrentPlayer != null && manager.CurrentPlayer.UserId == UserId)
{
manager.Pause();
}
2022-03-24 09:36:59 +08:00
head?.Destroy();
gameObject.Destroy();
}
2022-03-10 18:32:53 +08:00
}
}