powerfun-unity/Assets/Scenes/Ride/Scripts/CyclingController.cs

311 lines
9.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Assets.Scripts.Apis;
using Assets.Scripts.Apis.Models;
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.Networking;
using Assets.Scenes.Ride.Scripts.Model;
using Assets.Scenes.Ride.Scripts.Model.CyclingModels;
using Assets.Scenes.Ride.Scripts;
using Mapbox.Unity.Map;
using UnityEngine;
using GeoJSON.Net.Geometry;
using TurfCS;
using Mapbox.Utils;
using Assets.Scripts;
using System.Threading.Tasks;
public class CyclingController : DeviceServiceMonoBase
{
public AbstractMap map;
public GameObject player;
public GameObject UIObject;
public RouteResultParam selectParamModel;
public RouteResult routeResult;
public int preticks;
#region
private MapDataModel mapData;//当前路书数据
private Vector2d coordiantes;//当前地图中心
public bool isStart;//当前游戏是否开始
public CyclingModel cyclingModel;//当前骑行模式
public BaseCycling cyclingController { get; set; }
private int RouteId;
private MapApi mapApi;
private Route mapRoute;
public DateTime startTime;
public DateTime endTime;
public bool isQuit;
public bool isPause;
#endregion
GameObject loadingcanvas;
private async Task Login()
{
var result = await new UserApi().Login("13115011550", "laozhong", "");
App.CurrentUser = result.data;
}
void Awake()
{
#if UNITY_EDITOR
Login();
#endif
//获取路书信息
mapApi = ConfigHelper.mapApi;
RouteId = App.RouteIdParam > 0? App.RouteIdParam : 5492;
MapDataModel result = mapApi.GetData(RouteId);
mapRoute = GetNewInstace(RouteId);//获取一个路书所有的数据
MapUDPService.Init();//初始化TCP
if (result != null )
{
Init(result);
}
else
{
Debug.LogError("获取地图数据失败");
}
}
private void Start()
{
UIManager.Instance.MainPanel = this.transform.Find("SingleUI/Panel").GetComponent<PFUIPanel>();
UIManager.Instance.ModalsPanel = this.transform.Find("SingleUI/ModalPanel").GetComponent<PFUIPanel>();
}
//加载人物和地图以及UI界面
private void Init(MapDataModel result)
{
if (App.routeResult != null )
{
routeResult = App.routeResult;
selectParamModel = App.routeResult.ContinueCyclingParam;
preticks = App.routeResult.Ticks;
}
if (selectParamModel == null)
{
selectParamModel = new RouteResultParam
{
CompetitionId = 0,
ContinueIndex = 0,
ContinueMark = "",
GlobalContinue = false,
EndDistance = 0,
RankingsId = new List<string>(),
OnlineUserId = 0,
RouteId = App.RouteIdParam
};
}
App.routeResult = null;
mapData = result;
double endDistance = 0;
endDistance = selectParamModel.EndDistance;
coordiantes = Along(endDistance);
map.gameObject.SetActive(true);
player.SetActive(true);
UIObject.SetActive(true);
}
//设置骑行模式
public void SetCyclingModel(CyclingModel mode)
{
this.cyclingModel = mode;
switch (cyclingModel)
{
case CyclingModel.Single:
cyclingController = new SingleModel(mapRoute, selectParamModel);
break;
case CyclingModel.Review:
cyclingController = new SingleModel(mapRoute, selectParamModel);
break;
}
}
#region
public bool Is3dView;
public void ChangeMapImageStyle(GameObject arrowGameObj)
{
if (!Is3dView)
{
map.Terrain.ExaggerationFactor = 1.5f;
map.Terrain.SetElevationType(ElevationLayerType.TerrainWithElevation);
map.ImageLayer.SetLayerSource(ImagerySourceType.Custom);
//map.SetTileMaterial(new Material());
player.transform.localScale = new Vector3(3f, 3f, 3f);
Is3dView = true;
arrowGameObj.SetActive(false);
}
else
{
map.Terrain.ExaggerationFactor = 0f;
map.Terrain.SetElevationType(ElevationLayerType.FlatTerrain);
map.ImageLayer.SetLayerSource(ImagerySourceType.MapboxDark);
player.transform.localScale = new Vector3(0.1f, 0.1f, 0.1f);
Is3dView = false;
arrowGameObj.SetActive(true);
}
}
#endregion
#region
//开始骑行
public void SetStart()
{
if (!isPause)
{
isStart = true;
startTime = DateTime.Now;
}
else
{
SetContinue();
}
}
//继续骑行
public void SetContinue()
{
isStart = true;
isPause = false;
}
//暂停骑行
public void SetPause()
{
isStart = false;
isPause = true;
}
//退出或者完成骑行
public void SetQuit()
{
isStart = false;
isQuit = true;
}
#endregion
#region
/// <summary>
/// 当前骑行数据的记录器
/// </summary>
private RecorderDataModel recorderData
{
get
{
return cyclingController?.recorderData;
}
}
public Route GetRoute()
{
return mapRoute;
}
public MapDataModel GetMapData()
{
return mapData;
}
public void Save(double totalDistance)
{
isStart = false;
string imageFileName = CaptureCamera(Camera.main, new Rect(Screen.width * 0f, Screen.height * 0f, Screen.width * 0.5f, Screen.height * 0.5f));
cyclingController.recorderData.StartTime = startTime;
cyclingController.recorderData.IsCompleted = totalDistance == mapData.TotalDistance;
cyclingController.recorderData.EndDistance = totalDistance;
cyclingController.recorderData.AntModelId = AntModelId;
cyclingController.recorderData.ManufacturerId = ManufacturerId;
cyclingController.recorderData.DeviceNumber = DeviceNumber;
cyclingController.recorderData.SaveData(cyclingModel, selectParamModel, imageFileName);
}
protected string CaptureCamera(Camera camera, Rect rect)
{
// 创建一个RenderTexture对象
RenderTexture rt = new RenderTexture((int)rect.width, (int)rect.height, 0);
// 临时设置相关相机的targetTexture为rt, 并手动渲染相关相机
camera.targetTexture = rt;
camera.Render();
//ps: --- 如果这样加上第二个相机,可以实现只截图某几个指定的相机一起看到的图像。
//ps: camera2.targetTexture = rt;
//ps: camera2.Render();
//ps: -------------------------------------------------------------------
// 激活这个rt, 并从中中读取像素。
RenderTexture.active = rt;
Texture2D screenShot = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGB24, false);
screenShot.ReadPixels(rect, 0, 0);// 注这个时候它是从RenderTexture.active中读取像素
screenShot.Apply();
// 重置相关参数以使用camera继续在屏幕上显示
camera.targetTexture = null;
//ps: camera2.targetTexture = null;
RenderTexture.active = null; // JC: added to avoid errors
GameObject.Destroy(rt);
// 最后将这些纹理数据,成一个图片文件
byte[] bytes = screenShot.EncodeToPNG();
string filename = Application.dataPath + "/" + Guid.NewGuid().ToString() + ".png";
System.IO.File.WriteAllBytes(filename, bytes);
//Debug.Log(string.Format("截屏了一张照片: {0}", filename));
return filename;
}
public Vector2d GetCenterCoordinate()
{
return coordiantes;
}
public Route GetNewInstace(int id)
{
var routeInstance = mapApi.GetById(id);
if (routeInstance == null)
{
return null;
}
var jsonData = mapApi.GetData(RouteId);
return new Route(jsonData, routeInstance.data);
}
public Vector2d Along(double endDistance)
{
if (mapData != null)
{
var list = mapData.List.Select(p => new GeoJSON.Net.Geometry.GeographicPosition(p.Point[0], p.Point[1]));
LineString lineString = new LineString(list);
var pt1 = Turf.Along(lineString, endDistance);
var ll = ((GeographicPosition)((GeoJSON.Net.Geometry.Point)pt1.Geometry).Coordinates);
return new Vector2d(ll.Latitude, ll.Longitude);
}
else
{
return new Vector2d(0,0);
}
}
/// <summary>
/// 海拔图数据
/// </summary>
public float[] GetRealTimeLineChartData(int index)
{
List<float> dist = new List<float>();
var list = mapData.List.Select(c => (float) c.Elevation ).ToArray();
var count = list.Count();
for (int i = 0; i < count; i++)
{
if (i > index)
{
dist.Add(list[i]);
}
}
return dist.ToArray();
}
public float[] GetLineChartData()
{
var list = mapData.List.Select(c => (float)c.Elevation).ToArray();
return list;
}
#endregion
}