AR骑行和对战

This commit is contained in:
lishuo committed 2022-08-17 18:28:28 +08:00
1 parent 341d386196
commit 3bcb3ee83e
51 files changed
+146686 -4950

No files matched your search

@@ -159,9 +159,10 @@ namespace Assets.Scripts.Scenes.VideoRide
}
protected CustomRange pre { get; set; }
protected CustomRange next { get; set; }
public CustomRange next { get; set; }
protected float t { get; set; }
protected float t { get; set; }
protected Vector3 currentRotation;
protected virtual void Turn()
{
//控制人物的转向
@@ -179,12 +180,13 @@ namespace Assets.Scripts.Scenes.VideoRide
break;
}
}
t = next.KeyFrame - pre.KeyFrame == 0 ? 1 : (float)(currentFrame - pre.KeyFrame) / (float)(next.KeyFrame - pre.KeyFrame);
var p = new Vector3(0, pre.RotationY, pre.RotationZ);
var q = new Vector3(0, next.RotationY, next.RotationZ);
t = next.KeyFrame - pre.KeyFrame == 0 ? 1 : (float)(currentFrame - pre.KeyFrame) / (float)(next.KeyFrame - pre.KeyFrame);
var rt = Vector3.Lerp(p, q, t);
transform.DORotate(rt, 0, RotateMode.Fast);
currentRotation = Vector3.Lerp(p, q, t);
transform.DORotate(currentRotation, 0, RotateMode.Fast);
}
//计算人物当前属性
@@ -280,7 +282,6 @@ namespace Assets.Scripts.Scenes.VideoRide
decimal right = (decimal)sumDistance;
if (left <= right)
{
Debug.Log($"当前索引{i}");
currentIndex = i;
break;
}
@@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Assets.Scenes.Ride.Scripts;
using DG.Tweening;
using UnityEngine;
@@ -10,9 +12,21 @@ namespace Assets.Scripts.Scenes.VideoRide
public Transform target;
public double diff;
public int frames;
private void Start()
{
base.Start();
ydic.Add(0, -1);
ydic.Add(1555, 0);
}
protected override void Update()
{
base.Update();
//如果当前用户是被观察则控制视频播放
if (manager.CurrentPlayer.UserId == UserId)
{
manager.Play(manager.CurrentPlayer.totalDistance);
}
}
//计算
@@ -24,22 +38,72 @@ namespace Assets.Scripts.Scenes.VideoRide
//var left = totalDistance % mapData.TotalDistance;
//var right = current.totalDistance % mapData.TotalDistance;
//diff = (left - right) * 1000;
if (Math.Abs(diff) > 100)
if (Math.Abs(diff*1000) > 150)
{
Destroy();
}
}
}
private const int rate = 20;// z轴与帧数的比例 1 :20
private const int zrate = 20;// z轴与帧数的比例 1 :20 与真实距离 1:2
private const int yrate = 3;// y轴与帧数的比例 1 :3
private const int xrate = 20;// x轴与帧数的比例 1 :20
private Dictionary<int, float> ydic = new Dictionary<int, float>();
//人物向前超车 右边超车
protected override void Turn()
{
base.Turn();
//计算前后位移
var current = manager.CurrentPlayer;
var interval = this.currentFrame - manager.GetCurrentFrame();
var z = current.transform.position.z + interval / rate;
transform.DOMoveZ((float)z, 1f);
var z = current.transform.position.z + interval / zrate;//diff * 500;//
transform.DOMoveZ((float)z,1f);
//计算上下位移
var targetIndex = manager.CurrentPlayer.currentIndex;
var pointList = mapData.List;
var currentHeight = pointList[currentIndex].Elevation;//当前海拔,
var targetHeight = pointList[targetIndex].Elevation;//目标海拔
var heightOffset = (currentHeight - targetHeight);
var maxEl = pointList.Max(c => c.Elevation);
var minEl = pointList.Min(c => c.Elevation);
var y = diff != 0 ? heightOffset / (diff * 1000) * z-1 : 0;
var target = manager.CurrentPlayer.transform;
if (diff > 0)
{
var h = heightOffset / (diff*1000);
var height = h * (z - target.position.z) - 1;
//if (heightOffset < 0)
//{
// heightOffset *= 2;
//}
//else
//{
// heightOffset /= 100;
//}
if (height != 0)
{
transform.DOMoveY((float)height, 1f);
}
else
{
transform.DOMoveY(-1, 1f);
}
}
////计算左右倾斜角度
var targetLatlon = manager.CurrentPlayer.currentlatLon; //目标gps
var bearing = TurfHelper.Bearing(targetLatlon.ToArray(), currentlatLon.ToArray());
//currentRotation.y += (float)bearing;
//transform.DORotate(currentRotation, 1, RotateMode.Fast);
//计算左右位移
//x =(z - z1)/tanp + x1;
bearing = (next.RotationY + manager.CurrentPlayer.next.RotationY) * Math.PI / 180;//转化为弧度值
var x =(z - target.position.z) * Math.Tan(bearing) + target.position.x;
Debug.Log($"当前海拔差{heightOffset} 当前角度{bearing} 距离{(z - target.position.z)}*Tan(bearing){Math.Tanh(bearing)}+目标x{target.position.x}=当前x{x}");
transform.DOMoveX((float)x, 1f);
//x轴位移
//var x = bearing + current.bearing;
//transform.DOMove(new Vector3(x, transform.position.y, (float)z), 1); // ((float)z, 1f);
@@ -85,7 +149,7 @@ namespace Assets.Scripts.Scenes.VideoRide
{
var result = manager.mockDirection.Where(c => c.Start < currentFrame && c.End > currentFrame).FirstOrDefault();
bearing = result?.RotationY ?? 0;
animator.SetFloat("bearing", bearing);
//animator.SetFloat("bearing", bearing);
}
}
protected override int GetCurrentFrame()
@@ -74,7 +74,7 @@ namespace Assets.Scripts.Scenes.VideoRide
public DateTime startTime { get; set; }
public int FrameKM { get; private set; }//一米对应的帧数
public CyclingModel cyclingModel;
public BaseCycling cyclingController;
public BaseCycling cyclingController { get; set; }
public List<CustomRange> mockDirection = new List<CustomRange>();
private double endDistance { get; set; }
public List<float> slots = new List<float>();
@@ -140,7 +140,6 @@ namespace Assets.Scripts.Scenes.VideoRide
yield return null;
videoLoading.Init();//初始化loading页面
}
public int GetFrameKm()
{
var s = mediaPlayer.Info.GetDurationFrames();
@@ -158,15 +157,16 @@ namespace Assets.Scripts.Scenes.VideoRide
if (cyclingController == null)
return;
cyclingController.Run(null);
var onlineRiders = cyclingController.riders;
try
{
CreateOnlineUser(onlineRiders);
}
catch (Exception e)
{
Debug.Log(e.Message);
}
//TODO:发布单人骑行暂时注释
//var onlineRiders = cyclingController.riders;
//try
//{
// CreateOnlineUser(onlineRiders);
//}
//catch (Exception e)
//{
// Debug.Log(e.Message);
//}
timer += 1f;
}
}
@@ -186,6 +186,8 @@ namespace Assets.Scripts.Scenes.VideoRide
//创建当前线路其他选手
private void CreateOnlineUser(List<BaseRider> list)
{
if (list == null)
return;
//观察者模式随机选择当前骑行的人作为观察对象
if (_aRMode == ARMode.INSPECT && CurrentPlayer == null)
{
@@ -243,7 +245,7 @@ namespace Assets.Scripts.Scenes.VideoRide
if (currentPlayerInfo != null)
{
var diff = item.EndDistance % mapData.TotalDistance - currentPlayerInfo.EndDistance % mapData.TotalDistance;
if (Math.Abs(diff * 1000) <= 100)
if (Math.Abs(diff * 1000) <= 150)
{
GameObject obj;
if (currentPlayerInfo.Sex == 1)
@@ -274,6 +276,10 @@ namespace Assets.Scripts.Scenes.VideoRide
InitCurrentPlayer();
ContinueAsync();
}
else
{
SetCyclingModel(CyclingModel.Single);
}
}
//初始化人物模型
public void InitCurrentPlayer()
@@ -365,7 +371,9 @@ namespace Assets.Scripts.Scenes.VideoRide
private void MockDirection()
{
var arr = mapRoute.ARConfig.Replace("\r\n", " ").Split(' ');
//var arr = mapRoute.ARConfig.Replace("\r\n", " ").Split(' ');
var text = Resources.Load<TextAsset>("UI/direction");
var arr = text.text.Replace("\r\n", " ").Split(' ');
foreach (var item in arr)
{
if (string.IsNullOrEmpty(item))
@@ -521,7 +529,8 @@ namespace Assets.Scripts.Scenes.VideoRide
{
var mapApi = ConfigHelper.mapApi;
int routeId = App.RouteIdParam;
mapData = mapApi.GetData(routeId,20);//获取路书地理数据
if(routeId > 0)
mapData = mapApi.GetData(routeId,0);//获取路书地理数据
}
return mapData;
}
@@ -270,12 +270,12 @@ namespace Assets.Scripts.Scenes.VideoRide
//进入观察模式
private void WatchHandler(BaseEventData data)
{
manager.SetCurrentMode(VideoGameManager.ARMode.INSPECT);
var canvasGroup = transform.GetComponent<CanvasGroup>();
var ui = manager.GetCanvasTransform();
canvasGroup.DOFade(0, 1).onComplete += () => {
gameObject.SetActive(false);
ui.Find("Panel").gameObject.SetActive(true);
ui.gameObject.SetActive(true);
manager.SetCurrentMode(VideoGameManager.ARMode.INSPECT);
};
}
protected void Cancel(BaseEventData baseEvent)
@@ -72,7 +72,10 @@ namespace Assets.Scripts.Scenes.VideoRide
{
mapName.text = manager.GetMapRoute().Name;
#if UNITY_EDITOR
infoText.gameObject.SetActive(true);
infoText.text = $"帧数:{manager.GetCurrentFrame()}";
#endif
if (manager.CurrentPlayer != null)
{
power.text = Math.Round(manager.CurrentPlayer.power).ToString();
@@ -230,6 +233,8 @@ namespace Assets.Scripts.Scenes.VideoRide
}
public void SaveAndShowResult()
{
if (manager._aRMode == VideoGameManager.ARMode.INSPECT)
SceneManager.LoadScene("MainScene");
videoPlayer = FindObjectOfType<VideoPlayer>();
videoPlayer?.Upload();
//show result