198 lines
5.1 KiB
C#
198 lines
5.1 KiB
C#
using UnityEngine;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using Mapbox.Unity.Map;
|
|
using Mapbox.Utils;
|
|
using System.Collections;
|
|
using Mapbox.Unity.MeshGeneration.Modifiers;
|
|
using Mapbox.Unity.MeshGeneration.Data;
|
|
using Mapbox.Unity.Utilities;
|
|
using System;
|
|
|
|
namespace Assets.Scenes.Ride.Scripts
|
|
{
|
|
public class RouteController : MonoBehaviour
|
|
{
|
|
[SerializeField]
|
|
AbstractMap _map;
|
|
[SerializeField]
|
|
MeshModifier SnapModifier;
|
|
[SerializeField]
|
|
Material _material;
|
|
[SerializeField]
|
|
[Range(1, 10)]
|
|
private float UpdateFrequency = 2;
|
|
|
|
private int _counter;
|
|
|
|
GameObject _directionsGO;
|
|
GameObject _mipMapRoute;
|
|
CyclingController cyclingCotroller;
|
|
private bool initComplete = false;
|
|
|
|
protected virtual void Awake()
|
|
{
|
|
if (_map == null)
|
|
{
|
|
_map = FindObjectOfType<AbstractMap>();
|
|
}
|
|
// _map.OnInitialized += _map_OnInitialized; ;
|
|
//_map.OnUpdated += _map_OnUpdated; ;
|
|
|
|
|
|
|
|
}
|
|
|
|
private void _map_OnUpdated()
|
|
{
|
|
Debug.Log("_map_OnUpdated");
|
|
}
|
|
|
|
private void _map_OnInitialized()
|
|
{
|
|
Debug.Log("_map_OnInitialized");
|
|
CreateRoute();
|
|
}
|
|
|
|
private void _map_OnTileFinished(UnityTile obj)
|
|
{
|
|
|
|
if (!initComplete)
|
|
{
|
|
CreateRoute(obj);
|
|
initComplete = true;
|
|
}
|
|
}
|
|
|
|
|
|
PlayerController playerController;
|
|
public void Start()
|
|
{
|
|
cyclingCotroller = transform.parent.GetComponent<CyclingController>();
|
|
playerController = FindObjectOfType<PlayerController>();
|
|
_map.OnTileFinished += _map_OnTileFinished;
|
|
StartCoroutine(QueryTimer());
|
|
//CreateRoute();
|
|
}
|
|
|
|
protected virtual void OnDestroy()
|
|
{
|
|
_map.OnInitialized -= CreateRoute;
|
|
_map.OnUpdated -= CreateRoute;
|
|
}
|
|
|
|
public IEnumerator QueryTimer()
|
|
{
|
|
while (true)
|
|
{
|
|
CreateRoute(null);
|
|
yield return new WaitForSeconds(UpdateFrequency);
|
|
}
|
|
}
|
|
|
|
void CreateRoute(UnityTile tile)
|
|
{
|
|
{
|
|
var meshData = new MeshData();
|
|
var dat = new List<Vector3>();
|
|
|
|
var mapData = cyclingCotroller.GetMapData();
|
|
if (mapData != null)
|
|
{
|
|
//foreach (var mapDataItem in mapData.List)
|
|
for (int i = 0; i < mapData.List.Count; i++)
|
|
{
|
|
var point = mapData.List[i].Point;
|
|
Vector3 item = _map.GeoToWorldPosition(new Vector2d(point[0], point[1]));
|
|
item.y += 0.3f;
|
|
if (!posInScreen(item) && i > playerController.CurrentIndex)
|
|
{
|
|
break;
|
|
}
|
|
else if (posInScreen(item))
|
|
{
|
|
dat.Add(item);
|
|
}
|
|
}
|
|
var feat = new VectorFeatureUnity();
|
|
feat.Points.Add(dat);
|
|
//处理海拔高度的问题
|
|
//SnapModifier.Run(feat, meshData, tile);
|
|
CreatMapRoute(feat);//创建路线
|
|
//CreateMiniMapRoute(feat);//创建小地图路线
|
|
}
|
|
}
|
|
}
|
|
bool posInScreen(Vector3 position)
|
|
{
|
|
Transform camreatra = Camera.main.transform;
|
|
Vector3 viewPos = Camera.main.WorldToViewportPoint(position);
|
|
Vector3 dir = (position - camreatra.position).normalized;
|
|
float dot = Vector3.Dot(camreatra.forward, dir);
|
|
if (dot > -1 && viewPos.x > -1 && viewPos.x < 2 && viewPos.y > -1 && viewPos.y < 2)
|
|
{
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
void CreateRoute()
|
|
{
|
|
UnityTile tile;
|
|
bool foundTile = _map.MapVisualizer.ActiveTiles.TryGetValue(Conversions.LatitudeLongitudeToTileId(_map.CenterLatitudeLongitude.x, _map.CenterLatitudeLongitude.y, (int)_map.Zoom), out tile);
|
|
if (foundTile)
|
|
{
|
|
var meshData = new MeshData();
|
|
var dat = new List<Vector3>();
|
|
|
|
var mapData = cyclingCotroller.GetMapData();
|
|
if (mapData != null)
|
|
{
|
|
foreach (var mapDataItem in mapData.List)
|
|
{
|
|
var point = mapDataItem.Point;
|
|
Vector3 item = _map.GeoToWorldPosition(new Vector2d(point[0], point[1]));
|
|
item.y += 0.3f;
|
|
dat.Add(item);
|
|
}
|
|
var feat = new VectorFeatureUnity();
|
|
feat.Points.Add(dat);
|
|
//处理海拔高度的问题
|
|
//SnapModifier.Run(feat, meshData, tile);
|
|
//CreatMapRoute(feat);//创建路线
|
|
//CreateMiniMapRoute(feat);//创建小地图路线
|
|
|
|
}
|
|
}
|
|
}
|
|
void CreatMapRoute(VectorFeatureUnity feat)
|
|
{
|
|
if (_directionsGO != null)
|
|
{
|
|
_directionsGO.Destroy();
|
|
}
|
|
_directionsGO = new GameObject("MapRoute");
|
|
_directionsGO.transform.parent = transform;
|
|
var lineRender = _directionsGO.AddComponent<LineRenderer>();
|
|
lineRender.material = new Material(Shader.Find("Sprites/Default"));
|
|
var dat = feat.Points[0];
|
|
ColorUtility.TryParseHtmlString("#FF2742", out Color c);
|
|
lineRender.endColor = c;
|
|
lineRender.startColor = c;
|
|
//设置宽度
|
|
lineRender.SetWidth(0.2f, 0.2f);
|
|
lineRender.SetVertexCount(dat.Count);
|
|
lineRender.SetPositions(feat.Points[0].ToArray());
|
|
//lineRender.numCapVertices = 90;
|
|
float alpha = 1.0f;
|
|
//Gradient gradient = new Gradient();
|
|
//gradient.SetKeys(
|
|
// new GradientColorKey[] { new GradientColorKey(Color.green, 0.0f), new GradientColorKey(Color.red, 1.0f) },
|
|
// new GradientAlphaKey[] { new GradientAlphaKey(alpha, 0.0f), new GradientAlphaKey(alpha, 1.0f) }
|
|
//);
|
|
//lineRender.colorGradient = gradient;
|
|
lineRender.numCornerVertices = 90;
|
|
|
|
}
|
|
}
|
|
}
|