AR骑行人物控制代码重构&build混淆代码

This commit is contained in:
lishuo committed 2023-02-02 18:41:55 +08:00
1 parent 7822584cdd
commit 2c9440f656
78 files changed
+562 -8031

No files matched your search

@@ -4,168 +4,165 @@ using UnityEngine;
using System;
using System.Linq;
namespace Assets.Scenes.Ride.Scripts
public class GameEvent { string key { get; set; } }
public class EventQueueSystem : MonoSingleton<EventQueueSystem>
{
public class GameEvent { string key { get; set; } }
public delegate void EventDelegate<T>(T e) where T : GameEvent;
public class EventQueueSystem : MonoSingleton<EventQueueSystem>
private delegate void InternalEventDelegate(GameEvent e);
private Dictionary<Type, InternalEventDelegate> delegates = new Dictionary<Type, InternalEventDelegate>();
private Dictionary<Delegate, InternalEventDelegate> delegateLookup = new Dictionary<Delegate, InternalEventDelegate>();
private Dictionary<InternalEventDelegate, Delegate> delegateLookOnce = new Dictionary<InternalEventDelegate, Delegate>();
private Queue eventQueue = new Queue();
public bool bLimitQueueProcessing = false;
public float limitQueueTime = 1f;
//注册侦听事件(持续)
public static void AddListener<T>(EventDelegate<T> del) where T : GameEvent
{
public delegate void EventDelegate<T>(T e) where T : GameEvent;
private delegate void InternalEventDelegate(GameEvent e);
private Dictionary<Type, InternalEventDelegate> delegates = new Dictionary<Type, InternalEventDelegate>();
private Dictionary<Delegate, InternalEventDelegate> delegateLookup = new Dictionary<Delegate, InternalEventDelegate>();
private Dictionary<InternalEventDelegate, Delegate> delegateLookOnce = new Dictionary<InternalEventDelegate, Delegate>();
private Queue eventQueue = new Queue();
public bool bLimitQueueProcessing = false;
public float limitQueueTime = 1f;
//注册侦听事件(持续)
public static void AddListener<T>(EventDelegate<T> del) where T : GameEvent
if (!HasListener(del))
{
if (!HasListener(del))
{
Debug.Log(30);
Instance.AddDelegate(del);
}
}
//注册侦听事件(一次)
public static void AddListenerOnce<T>(EventDelegate<T> del) where T : GameEvent
{
var result = Instance.AddDelegate(del);
if (result != null)
Instance.delegateLookOnce[result] = del;
}
//判定侦听事件是否存在
public static bool HasListener<T>(EventDelegate<T> del) where T : GameEvent
{
return Instance.delegateLookup.Keys.Where(c => c.GetType() == del.GetType()).Any();
//return Instance.delegateLookup.ContainsKey(del);
}
//移除侦听事件
public static void RemoveListener<T>(EventDelegate<T> del) where T : GameEvent
{
if (Instance == null)
return;
if (Instance.delegateLookup.TryGetValue(del, out InternalEventDelegate eventDelegate))
{
if (Instance.delegates.TryGetValue(typeof(T), out InternalEventDelegate temp))
{
temp -= eventDelegate;
if (temp == null)
Instance.delegates.Remove(typeof(T));
else
Instance.delegates[typeof(T)] = temp;
}
Instance.delegateLookup.Remove(del);
keyList.Clear();
}
}
public static void RemoveAll()
{
if (Instance != null)
{
Instance.delegates.Clear();
Instance.delegateLookup.Clear();
Instance.delegateLookOnce.Clear();
}
}
private InternalEventDelegate AddDelegate<T>(EventDelegate<T> del) where T : GameEvent
{
if (delegateLookup.ContainsKey(del))
return null;
void eventDelegate(GameEvent e) => del((T)e);
delegateLookup[del] = eventDelegate;
if (delegates.TryGetValue(typeof(T), out InternalEventDelegate temp))
delegates[typeof(T)] = temp += eventDelegate;
else
delegates[typeof(T)] = eventDelegate;
return eventDelegate;
}
//单个事件触发
private static void TriggerEvent(GameEvent e)
{
var type = e.GetType();
if (Instance.delegates.TryGetValue(type, out InternalEventDelegate eventDelegate))
{
eventDelegate.Invoke(e);
//移除单一侦听
foreach (InternalEventDelegate item in Instance.delegates[type].GetInvocationList())
{
if (Instance.delegateLookOnce.TryGetValue(item, out Delegate temp))
{
Instance.delegates[type] -= item;
if (Instance.delegates[type] == null)
Instance.delegates.Remove(type);
Instance.delegateLookup.Remove(temp);
Instance.delegateLookOnce.Remove(item);
}
}
}
}
//外部调用的推入事件队列接口
public static void QueueEvent(GameEvent e)
{
if (!Instance.delegates.ContainsKey(e.GetType()))
return;
Instance.eventQueue.Enqueue(e);
}
private static Dictionary<string,GameEvent> keyList = new Dictionary<string, GameEvent>();
public static void QueueEventOnce(GameEvent e,string key)
{
if (!Instance.delegates.ContainsKey(e.GetType()))
return;
if (keyList.ContainsKey(key))
return;
keyList.Add(key,e);
Instance.eventQueue.Enqueue(e);
}
float timer = 0.0f;
//事件队列触发处理
void Update()
{
//float timer = 0.0f;
//while (eventQueue.Count > 0)
//{
// if (bLimitQueueProcessing)
// if (timer > limitQueueTime)
// return;
// var e = eventQueue.Dequeue() as GameEvent;
// TriggerEvent(e);
// if (bLimitQueueProcessing)
// timer += Time.deltaTime;
//}
timer += Time.deltaTime;
while (timer >= 2 && eventQueue.Count > 0)
{
var e = eventQueue.Dequeue() as GameEvent;
TriggerEvent(e);
timer = 0;
}
}
private void OnApplicationQuit()
{
RemoveAll();
eventQueue.Clear();
}
public static void ClearQueue()
{
if (Instance != null)
Instance.eventQueue.Clear();
Debug.Log(30);
Instance.AddDelegate(del);
}
}
}
//注册侦听事件(一次)
public static void AddListenerOnce<T>(EventDelegate<T> del) where T : GameEvent
{
var result = Instance.AddDelegate(del);
if (result != null)
Instance.delegateLookOnce[result] = del;
}
//判定侦听事件是否存在
public static bool HasListener<T>(EventDelegate<T> del) where T : GameEvent
{
return Instance.delegateLookup.Keys.Where(c => c.GetType() == del.GetType()).Any();
//return Instance.delegateLookup.ContainsKey(del);
}
//移除侦听事件
public static void RemoveListener<T>(EventDelegate<T> del) where T : GameEvent
{
if (Instance == null)
return;
if (Instance.delegateLookup.TryGetValue(del, out InternalEventDelegate eventDelegate))
{
if (Instance.delegates.TryGetValue(typeof(T), out InternalEventDelegate temp))
{
temp -= eventDelegate;
if (temp == null)
Instance.delegates.Remove(typeof(T));
else
Instance.delegates[typeof(T)] = temp;
}
Instance.delegateLookup.Remove(del);
keyList.Clear();
}
}
public static void RemoveAll()
{
if (Instance != null)
{
Instance.delegates.Clear();
Instance.delegateLookup.Clear();
Instance.delegateLookOnce.Clear();
}
}
private InternalEventDelegate AddDelegate<T>(EventDelegate<T> del) where T : GameEvent
{
if (delegateLookup.ContainsKey(del))
return null;
void eventDelegate(GameEvent e) => del((T)e);
delegateLookup[del] = eventDelegate;
if (delegates.TryGetValue(typeof(T), out InternalEventDelegate temp))
delegates[typeof(T)] = temp += eventDelegate;
else
delegates[typeof(T)] = eventDelegate;
return eventDelegate;
}
//单个事件触发
private static void TriggerEvent(GameEvent e)
{
var type = e.GetType();
if (Instance.delegates.TryGetValue(type, out InternalEventDelegate eventDelegate))
{
eventDelegate.Invoke(e);
//移除单一侦听
foreach (InternalEventDelegate item in Instance.delegates[type].GetInvocationList())
{
if (Instance.delegateLookOnce.TryGetValue(item, out Delegate temp))
{
Instance.delegates[type] -= item;
if (Instance.delegates[type] == null)
Instance.delegates.Remove(type);
Instance.delegateLookup.Remove(temp);
Instance.delegateLookOnce.Remove(item);
}
}
}
}
//外部调用的推入事件队列接口
public static void QueueEvent(GameEvent e)
{
if (!Instance.delegates.ContainsKey(e.GetType()))
return;
Instance.eventQueue.Enqueue(e);
}
private static Dictionary<string, GameEvent> keyList = new Dictionary<string, GameEvent>();
public static void QueueEventOnce(GameEvent e, string key)
{
if (!Instance.delegates.ContainsKey(e.GetType()))
return;
if (keyList.ContainsKey(key))
return;
keyList.Add(key, e);
Instance.eventQueue.Enqueue(e);
}
float timer = 0.0f;
//事件队列触发处理
void Update()
{
//float timer = 0.0f;
//while (eventQueue.Count > 0)
//{
// if (bLimitQueueProcessing)
// if (timer > limitQueueTime)
// return;
// var e = eventQueue.Dequeue() as GameEvent;
// TriggerEvent(e);
// if (bLimitQueueProcessing)
// timer += Time.deltaTime;
//}
timer += Time.deltaTime;
while (timer >= 2 && eventQueue.Count > 0)
{
var e = eventQueue.Dequeue() as GameEvent;
TriggerEvent(e);
timer = 0;
}
}
private void OnApplicationQuit()
{
RemoveAll();
eventQueue.Clear();
}
public static void ClearQueue()
{
if (Instance != null)
Instance.eventQueue.Clear();
}
}
@@ -473,5 +473,26 @@ namespace Assets.Scenes.Ride.Scripts
}
return startTime;
}
//截图
public static void CaptureCamera(Camera camera, Rect rect, string fileName)
{
byte[] bytes = CaptureCameraReturnByte(camera, rect);
System.IO.File.WriteAllBytes(fileName, bytes);
}
private static byte[] CaptureCameraReturnByte(Camera camera, Rect rect)
{
RenderTexture rt = new RenderTexture((int)rect.width, (int)rect.height, 0);
camera.targetTexture = rt;
camera.Render();
RenderTexture.active = rt;
Texture2D screenShot = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGB24, false);
screenShot.ReadPixels(rect, 0, 0);
screenShot.Apply();
camera.targetTexture = null;
RenderTexture.active = null;
GameObject.Destroy(rt);
return screenShot.EncodeToJPG();
}
}
}
@@ -5,62 +5,60 @@ using System.Text;
using System.Threading.Tasks;
using UnityEngine;
namespace Assets.Scenes.Ride.Scripts
public abstract class MonoSingleton<T> : MonoBehaviour where T : MonoBehaviour
{
public abstract class MonoSingleton<T> : MonoBehaviour where T : MonoBehaviour
private static T instance = null;
private static readonly object locker = new object();
private static bool bAppQuitting;
public static T Instance
{
private static T instance = null;
private static readonly object locker = new object();
private static bool bAppQuitting;
public static T Instance
get
{
get
if (bAppQuitting)
{
if (bAppQuitting)
{
instance = null;
return instance;
}
instance = null;
return instance;
}
lock (locker)
lock (locker)
{
if (instance == null)
{
instance = FindObjectOfType<T>();
if (FindObjectsOfType<T>().Length > 1)
{
Debug.LogError("不应该存在多个单例!");
return instance;
}
if (instance == null)
{
instance = FindObjectOfType<T>();
if (FindObjectsOfType<T>().Length > 1)
{
Debug.LogError("不应该存在多个单例!");
return instance;
}
if (instance == null)
{
var singleton = new GameObject();
instance = singleton.AddComponent<T>();
singleton.name = "(singleton)" + typeof(T);
singleton.hideFlags = HideFlags.None;
DontDestroyOnLoad(singleton);
}
else
DontDestroyOnLoad(instance.gameObject);
var singleton = new GameObject();
instance = singleton.AddComponent<T>();
singleton.name = "(singleton)" + typeof(T);
singleton.hideFlags = HideFlags.None;
DontDestroyOnLoad(singleton);
}
instance.hideFlags = HideFlags.None;
return instance;
else
DontDestroyOnLoad(instance.gameObject);
}
instance.hideFlags = HideFlags.None;
return instance;
}
}
}
private void Awake()
{
bAppQuitting = false;
}
private void Awake()
{
bAppQuitting = false;
}
private void OnDestroy()
{
bAppQuitting = true;
}
private void OnDestroy()
{
bAppQuitting = true;
}
}
@@ -1,68 +1,63 @@
using Assets.Scenes.Ride.Scripts;
using Assets.Scripts.Apis.Models;
using DG.Tweening;
using Mapbox.Utils;
using System;
using System;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;
using Assets.Core;
using Assets.Scripts.Apis.Models;
using DG.Tweening;
using Mapbox.Utils;
using Assets.Scenes.Ride.Scripts;
namespace Assets.Scripts.Scenes.VideoRide
{
public abstract class AbstractVideoPlayer : MonoBehaviour, IRider
{
public double Diff { get;set; }
protected int frames;
protected int currentFrame { get; set; }
protected double preTime;
protected Animator animator;
protected Animator animator { get; set; }
public GameObject head { get; set; }
protected Image ftpImage { get; set; }
protected Text headName { get; set; }
protected Text headWkg { get; set; }
public int userId { get; set; }
public string userName { get; set; }
public double weight;//体重
protected double bicycleWeight;//车重
protected double preSpeed;
public double speed;
public double power;
protected double elevation;
public double cadance;
public double wkg;
public int? heartRate { get; set; }
public int ticks;
public double totalDistance { get; set; }
public double currentSlope;
protected double nextSlope;
protected double nextSlopeDistance;
public double distance;
protected double currentSlopeDistance;
protected double lastEndDistance;
public double totalClimb;
public double power;
public double elevation { get; set; }
public double cadance { get; set; }
public double wkg { get; set; }
public int? heartRate { get; set; }
public double totalDistance { get; set; }
public double currentSlope { get; set; }
protected double nextSlope { get; set; }
protected double nextSlopeDistance { get; set; }
protected double currentSlopeDistance { get; set; }
public double totalClimb { get; set; }
public Vector2d currentlatLon { get; set; }
public int currentIndex;
public float bearing = 0f;
public int currentIndex { get; set; }
public int ticks { get; set; }
protected MapDataModel mapData;
private float timer = 0f;
protected bool start = true;
protected Sequence sequence;
protected VideoGameManager manager { get; set; }
float currenPlayerHeight;
Transform bone_bottle_2 { get; set; }
public double Speed { get; set; }
public double StartDistance { get; set; }
public double PreDistance { get; set; }
public double EndDistance { get; set; }
public double OnlineSpeed { get; set; }
public int UserId => this.userId;
public string UserName => this.userName;
public bool IsStart = false;
public double mockpower { get; set; }
protected float ratio = 1;
protected virtual void Start()
{
@@ -74,74 +69,57 @@ namespace Assets.Scripts.Scenes.VideoRide
}
protected virtual void Update()
{
timer -= Time.deltaTime;
CreateHeadImage();
while (timer <= 0)
{
try
{
ComputeNextSlope();//计算下一个坡度
if (manager.IsStart())
{
ticks++;
ComputePlayer();//计算人物属性
Forward();
ComputeRecord();
ComputeVideo();
}
else
{
power = 0;
OnlineSpeed = 0;
PreDistance = EndDistance;
speed = 0;
distance = 0;
}
ComputeAnimator();//控制动画
timer += 1f;
}
catch (Exception e)
{
power = 0;
speed = 0;
Debug.LogError(e.Message);
}
}
ComputeAnimator();//控制动画
ComputeAnimatorSpeed();//控制动画速度
}
//动画状态机
protected virtual void ComputeAnimator()
public void StartAction()
{
if (animator != null)
try
{
if (manager._viewMode == VideoGameManager.ViewMode.FIRST &&manager.CurrentPlayer.UserId == App.CurrentUser.Id)
if (manager.IsStart())
{
animator.SetFloat("preSpeed", (float)preSpeed);
animator.SetFloat("speed", (float)Math.Min(speed,25));
animator.SetFloat("grade", (float)currentSlope);
animator.SetFloat("power", (float)power);
ComputeTimerTick();
ComputeNextSlope();//计算下一个坡度
ComputePlayer();//计算人物属性
Forward();
ComputeRecord();
}
else
{
animator.SetFloat("preSpeed", (float)preSpeed);
animator.SetFloat("speed", (float)speed);
animator.SetFloat("grade", (float)currentSlope);
animator.SetFloat("power", (float)power);
//播放喝水和回头的动画
//var headBack = ticks % 60 == 0 && speed > 0;
//var drink = ticks % 125 == 0 && speed > 0;
//animator.SetBool("headBack", headBack);
//animator.SetBool("drinking", drink);
power = 0;
OnlineSpeed = 0;
PreDistance = EndDistance;
speed = 0;
distance = 0;
}
}
catch (Exception e)
{
power = 0;
speed = 0;
Debug.LogError(e.Message);
}
}
protected virtual int GetCurrentFrame()
private void ComputeTimerTick()
{
return manager.GetCurrentFrame();
ticks++;
}
public virtual void ComputeAnimator()
{
if (animator != null)
{
animator.SetFloat("preSpeed", (float)preSpeed);
animator.SetFloat("speed", (float)speed);
animator.SetFloat("grade", (float)currentSlope);
animator.SetFloat("power", (float)power);
}
}
protected Vector3 currentRotation;
//计算人物当前属性
protected virtual void ComputePlayer()
public virtual void ComputePlayer()
{
if (power > 0)
{
@@ -153,17 +131,9 @@ namespace Assets.Scripts.Scenes.VideoRide
speed = 0;
distance = 0;
}
////一旦有功率就开始骑行、否则暂停
//if (!manager.IsQuit() && manager.CurrentPlayer.UserId == UserId && !manager.IsQuit() && manager.cyclingModel == CyclingModel.Single)
//{
// if (power > 0)
// {
// manager.StartGame();
// }
//}
}
//计算当前人物的经纬度
protected virtual void Forward()
public virtual void Forward()
{
try
{
@@ -176,44 +146,28 @@ namespace Assets.Scripts.Scenes.VideoRide
Debug.LogError(e);
}
}
protected float ratio = 1;//视频播放速率
protected virtual void ComputeVideo()
public virtual void ComputeAnimatorSpeed()
{
mapData = manager.GetMapData();
if (currentIndex + 1 < mapData.List.Count)
{
ratio = (float)(speed / mapData.List[currentIndex + 1].Distance);
}
else
{
ratio = (float)(speed / mapData.List[currentIndex].Distance);
}
if (manager.CurrentPlayer != null && manager.CurrentPlayer.UserId != UserId)
if (manager == null)
return;
mapData = manager.GetMapData();
int safeIndex = currentIndex + 1 < mapData.List.Count ? currentIndex + 1 : currentIndex;
float num = speed == 0 ? 1f : (float)(speed / mapData.List[safeIndex].Distance);
ratio = Mathf.Clamp(num, 0.3f, 1.2f);
if (ratio > 1)
{
ratio = Math.Min(ratio, 1.2f);
}
if (ratio < 1)
{
ratio = Math.Max(ratio, 0.3f);
}
if (speed == 0)
{
ratio = 1;
}
var info = animator.GetCurrentAnimatorClipInfo(0);
var currentClip = info.FirstOrDefault();
if (currentClip.clip != null)
if (info != null)
{
animator.speed = ratio;
var currentClip = info.FirstOrDefault();
if (currentClip.clip != null)
{
animator.speed = ratio;
}
}
}
protected virtual void ComputeRecord() { }
public virtual void ComputeRecord() { }
//计算当前区段属性下一个区段属性
void ComputeNextSlope()
public void ComputeNextSlope()
{
double sumDistance = 0;
mapData = manager.GetMapData();
@@ -265,13 +219,12 @@ namespace Assets.Scripts.Scenes.VideoRide
{
head?.SetActive(false);
}
private void OnEnable()
{
head?.SetActive(true);
}
//显示人物海拔图的头像
protected virtual void CreateHeadImage()
//显示人物头顶的名字
public virtual void CreateHeadImage()
{
if (head == null)
{
@@ -281,7 +234,6 @@ namespace Assets.Scripts.Scenes.VideoRide
ftpImage = head.transform.Find("ftp").GetComponent<Image>();
headName = head.transform.Find("name").GetComponent<Text>();
headWkg = head.transform.Find("wkg").GetComponent<Text>();
}
}
if (head != null)
@@ -293,8 +245,14 @@ namespace Assets.Scripts.Scenes.VideoRide
ftpImage.fillAmount = (float)(wkg / 6);
headName.text = UserName;
headWkg.text = $"{wkg}W/KG";
head.SetActive(bone_bottle_2.position.z > 0);
}
}
public void SetStartDistance(double distance)
{
this.StartDistance = distance *1000;
}
public void Destroy()
{
if (manager.CurrentPlayer != null && manager.CurrentPlayer.UserId == UserId)
@@ -306,10 +264,5 @@ namespace Assets.Scripts.Scenes.VideoRide
}
//设置当前玩家属性
public abstract void SetPlayer(string name, double speed, double preDistance, double endDistance, double cadance, double heartRate, double wkg, int userId, double power, double currentPlayerDistance, int frame);
public void SetStartDistance(double distance)
{
this.StartDistance = distance *1000;
}
}
}
@@ -61,6 +61,7 @@ namespace Assets.Scripts.Scenes.VideoRide
{
if (cyclingCotroller.CurrentPlayer != null)
{
uitransform = cyclingCotroller.GetCanvasTransform();
var tr = uitransform.Find("MiniMap/MiniMap/arrow");
tr.SetSiblingIndex(9999);
var pos = _map.GeoToWorldPosition(cyclingCotroller.CurrentPlayer.currentlatLon);
@@ -15,25 +15,16 @@ namespace Assets.Scripts.Scenes.VideoRide
base.Update();
}
//计算
protected override void ComputePlayer()
{
}
protected override void ComputeAnimator()
public override void ComputeAnimator()
{
base.ComputeAnimator();
}
protected override int GetCurrentFrame()
public override void ComputeAnimatorSpeed()
{
return this.currentFrame;
base.ComputeAnimatorSpeed();
}
protected override void ComputeVideo()
{
base.ComputeVideo();
}
protected override void CreateHeadImage()
public override void CreateHeadImage()
{
base.CreateHeadImage();
if (Diff < 0)
@@ -46,8 +37,6 @@ namespace Assets.Scripts.Scenes.VideoRide
{
manager = FindObjectOfType<VideoGameManager>();
}
var f = totalDistance;
preTime = f;
this.wkg = wkg;
this.userName = name;
this.cadance = cadance;
@@ -119,6 +119,7 @@ namespace Assets.Scripts.Scenes.VideoRide
videoLoading.Init();//初始化loading页面
}
float timer = 0f;
int ticks = 0;
protected override void Update()
{
timer -= Time.deltaTime;
@@ -131,6 +132,20 @@ namespace Assets.Scripts.Scenes.VideoRide
try
{
CreateOnlineUser(onlineRiders);
if (isStart)
{
var riders = FindObjectsOfType<OnlineVideoPlayer>();
foreach (var rider in riders)
{
rider.StartAction();
}
//解决当前用户setActive(false)时仍能计算数据
if (CurrentPlayer != null && CurrentPlayer is VideoPlayer)
{
CurrentPlayer.StartAction();
}
ticks++;
}
}
catch (Exception e)
{
@@ -159,18 +174,12 @@ namespace Assets.Scripts.Scenes.VideoRide
private void CreateOnlineUser(List<BaseRider> list)
{
list = list.Where(c => ((OnlineRiderModel)c).IsWatcher == false).ToList();
//断网移除所有用户
if (list == null || list.Count == 0)
{
var currentList = FindObjectsOfType<PlayerRenderer>();
foreach (var item in currentList)
{
item.Speed = 0;
item.Paused = true;
}
rideObjs.Clear();
visibleRiders.Clear();
//移除退出的选手
var onlineUserList = FindObjectsOfType<OnlineVideoPlayer>();
var onlineUserList = FindObjectsOfType<AbstractVideoPlayer>();
foreach (var item in onlineUserList)
{
DestroyImmediate(item.gameObject);
@@ -188,7 +197,6 @@ namespace Assets.Scripts.Scenes.VideoRide
{
rideObjs.Remove(item);
visibleRiders.Remove(item);
//trainController.RemoveRider(item.UserId);
}
}
var currentPlayerInfo = list.Where(c => c.IsSelf).FirstOrDefault();
@@ -200,13 +208,14 @@ namespace Assets.Scripts.Scenes.VideoRide
if(currentPlayerInfo == null)
currentPlayerInfo = needUpdateList.FirstOrDefault();
}
//更新和新增用户
foreach (var item in needUpdateList)
{
var onlineRider = item as OnlineRiderModel;
var visibleItem = visibleRiders.Where(c => c.UserId == item.UserId).FirstOrDefault();
//更新
if (visibleItem != null)
{
Debug.Log($"count {visibleRiders.Count()}");
visibleItem.SetPlayer(onlineRider.NickName, onlineRider.Speed, onlineRider.PreDistance, onlineRider.EndDistance, item.Cadence, item.HeartRate, onlineRider.WeightKg, item.UserId, onlineRider.Power, currentPlayerInfo.EndDistance, onlineRider.FrameRate.Value);
if(visibleItem.userId == currentUserId)
CurrentPlayer = visibleItem;
@@ -293,7 +302,7 @@ namespace Assets.Scripts.Scenes.VideoRide
public GameObject videoPlayer;
public GameObject target;
private int currentUserId { get; set; }
//切换人物 TODO:替换写法
//切换人物
public void ChangePlayer(int userId)
{
currentUserId = userId;
@@ -321,7 +330,6 @@ namespace Assets.Scripts.Scenes.VideoRide
//设置骑行模式
public void SetCyclingModel(CyclingModel mode)
{
//startTime = UIManager.Now.GetDateTime();
this.cyclingModel = mode;
switch (cyclingModel)
{
@@ -343,11 +351,6 @@ namespace Assets.Scripts.Scenes.VideoRide
isStart = true;
}
//骑行是否开始
public bool IsReady()
{
return true;
}
//骑行是否开始
public bool IsStart()
{
return isStart;
@@ -390,9 +393,7 @@ namespace Assets.Scripts.Scenes.VideoRide
{
return mediaPlayer.Control.GetCurrentTimeFrames();
}
public float ratio = 111.676f;
public float startOffset = 0f;
//获取AR骑行配置信息
//获取路书gps信息
public MapDataModel GetMapData()
@@ -418,14 +419,16 @@ namespace Assets.Scripts.Scenes.VideoRide
return mapRoute;
}
//保存骑行记录
public void Save(double totalDistance)
public void SaveData(double totalDistance)
{
if (cyclingController.recorderData == null || cyclingController.recorderData.Saved)
return;
Quit();
cyclingController.recorderData.EndTime = UIManager.Now.GetDateTime();
var path = PFConstants.MapWorkoutRecordFolder + "/" + recordId;
Assets.Scenes.Ride.Scripts.Helper.CreateDirectoryIfNotExsit(path);
string imageFileName = path + "/" + Guid.NewGuid().ToString() + ".png";
CaptureCamera(Camera.main, new Rect(Screen.width * 0f, Screen.height * 0f, Screen.width * 0.5f, Screen.height * 0.5f), imageFileName);
Helper.CaptureCamera(Camera.main, new Rect(Screen.width * 0f, Screen.height * 0f, Screen.width * 0.5f, Screen.height * 0.5f), imageFileName);
cyclingController.recorderData.StartTime = startTime;
cyclingController.recorderData.IsCompleted = totalDistance >= mapData.TotalDistance;
cyclingController.recorderData.EndDistance = totalDistance;
@@ -448,27 +451,6 @@ namespace Assets.Scripts.Scenes.VideoRide
{
return UIManager.Instance.loginRegOptions.GetCountryImageByName(name);
}
//截图
protected void CaptureCamera(Camera camera, Rect rect, string fileName)
{
byte[] bytes = CaptureCameraReturnByte(camera, rect);
System.IO.File.WriteAllBytes(fileName, bytes);
}
private byte[] CaptureCameraReturnByte(Camera camera, Rect rect)
{
RenderTexture rt = new RenderTexture((int)rect.width, (int)rect.height, 0);
camera.targetTexture = rt;
camera.Render();
RenderTexture.active = rt;
Texture2D screenShot = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGB24, false);
screenShot.ReadPixels(rect, 0, 0);
screenShot.Apply();
camera.targetTexture = null;
RenderTexture.active = null;
GameObject.Destroy(rt);
return screenShot.EncodeToJPG();
}
//根据距离计算下一个点坐标
public Vector2d Along(double endDistance)
{
if (mapData != null)
+10 -25
View File
@@ -16,21 +16,11 @@ namespace Assets.Scripts.Scenes.VideoRide
userName = App.CurrentUser.Nickname;
}
protected bool GetStart()
{
return manager.IsStart();
}
protected override void Update()
{
base.Update();
}
public void SetPower(double power)
{
this.power = power;
}
public void SetEndDistance(double distance)
{
this.totalDistance = distance;
@@ -48,12 +38,12 @@ namespace Assets.Scripts.Scenes.VideoRide
}
}
protected override void ComputeAnimator()
public override void ComputeAnimator()
{
base.ComputeAnimator();
}
protected override void ComputePlayer()
public override void ComputePlayer()
{
heartRate = manager.UpDateHeart();
power = manager.UpdatePower();
@@ -93,14 +83,12 @@ namespace Assets.Scripts.Scenes.VideoRide
},true);
}
}
//控制视频播放速度
protected override void ComputeVideo()
{
base.ComputeVideo();
//单人骑行到终点自动暂停
}
protected override void ComputeRecord()
public override void ComputeAnimatorSpeed()
{
base.ComputeAnimatorSpeed();
}
public override void ComputeRecord()
{
var mapData = manager.GetMapData();
//记录骑行数据
@@ -134,7 +122,7 @@ namespace Assets.Scripts.Scenes.VideoRide
}
}
protected override void Forward()
public override void Forward()
{
base.Forward();
distance = Math.Round(speed / 3600, 5, MidpointRounding.AwayFromZero);
@@ -143,10 +131,7 @@ namespace Assets.Scripts.Scenes.VideoRide
//保存骑行记录
public void Upload()
{
if (manager.cyclingController.recorderData != null && !manager.cyclingController.recorderData.Saved)//处理重复上传的问题
{
manager.Save(totalDistance);
}
manager.SaveData(totalDistance);
}
public override void SetPlayer(string name, double speed, double preDistance, double endDistance, double cadance, double heartRate, double wkg, int userId, double power, double currentPlayerDistance, int frame)