diff --git a/Assets/Scenes/scene.unity b/Assets/Scenes/scene.unity index 0adf398..723c557 100644 --- a/Assets/Scenes/scene.unity +++ b/Assets/Scenes/scene.unity @@ -314,13 +314,64 @@ MonoBehaviour: m_Name: m_EditorClassIdentifier: Assembly-CSharp::MapRenderer queryRadiusMeters: 1000 + gpsManager: {fileID: 1539417978} buildingMaterial: {fileID: 2100000, guid: 283cf727b4c3ac64c94d59598e221b10, type: 2} defaultFloorHeight: 3 defaultBuildingHeight: 6 roadMaterial: {fileID: 2100000, guid: 283cf727b4c3ac64c94d59598e221b10, type: 2} defaultRoadWidth: 4 + motorwayWidth: 10 + primaryWidth: 8 + secondaryWidth: 6 + tertiaryWidth: 5 metersPerUnit: 1 - autoStart: 1 +--- !u!1 &1539417977 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 1539417979} + - component: {fileID: 1539417978} + m_Layer: 0 + m_Name: currentGPS + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!114 &1539417978 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1539417977} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 6b892bed07e5c4d45ad23b4a9b108e08, type: 3} + m_Name: + m_EditorClassIdentifier: Assembly-CSharp::GPSManager + Accuracy: 10 + UpdateDistance: 5 + MaxWait: 20 +--- !u!4 &1539417979 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1539417977} + serializedVersion: 2 + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: -18.75082, y: 0, z: 46.79584} + m_LocalScale: {x: 1, y: 1, z: 1} + m_ConstrainProportionsScale: 0 + m_Children: [] + m_Father: {fileID: 0} + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!1 &1865882987 GameObject: m_ObjectHideFlags: 0 @@ -494,3 +545,4 @@ SceneRoots: - {fileID: 1896274048} - {fileID: 1865882990} - {fileID: 1274200471} + - {fileID: 1539417979} diff --git a/Assets/Scripts/GPSManager.cs b/Assets/Scripts/GPSManager.cs new file mode 100644 index 0000000..d12dfb3 --- /dev/null +++ b/Assets/Scripts/GPSManager.cs @@ -0,0 +1,72 @@ +using System.Collections; +using UnityEngine; + +public class GPSManager : MonoBehaviour +{ + [Header("GPS settings")] + public float Accuracy = 10f; + public float UpdateDistance = 5f; + public int MaxWait = 20; + + [Header("GPS coordinates")] + private double[] LastCoords = new double[2]; + private double[] FailsafeCoords = new double[] { 50.7727878, 15.0718625 }; + private double? LastTime; + + void Start() + { + + } + + void Update() + { + StartCoroutine(UpdateGPS()); + } + + public double[] GetLastCoords() + { + if (LastCoords[0] == 0 && LastCoords[1] == 0) { return FailsafeCoords; } + return LastCoords; + } + IEnumerator UpdateGPS() + { + if (!Input.location.isEnabledByUser) + { + Debug.Log("GPS not enabled by user"); + LastCoords = FailsafeCoords; + LastTime = null; + yield break; + } + + Input.location.Start(Accuracy, UpdateDistance); + + while (Input.location.status == LocationServiceStatus.Initializing && MaxWait > 0) + { + yield return new WaitForSeconds(1); + MaxWait--; + } + + if (MaxWait < 1) + { + Debug.Log("GPS timed out"); + LastCoords = FailsafeCoords; + LastTime = null; + yield break; + } + if (Input.location.status == LocationServiceStatus.Failed) + { + Debug.Log("GPS failed to determine device location"); + LastCoords = FailsafeCoords; + LastTime = null; + yield break; + } + else + { + LastCoords[0] = Input.location.lastData.latitude; + LastCoords[1] = Input.location.lastData.longitude; + LastTime = Input.location.lastData.timestamp; + + Debug.Log("GPS location: " + LastCoords[0] + ", " + LastCoords[1] + " (time: " + LastTime + ")"); + } + } +} diff --git a/Assets/Scripts/MapRenderer.cs b/Assets/Scripts/MapRenderer.cs index e94101f..92d7941 100644 --- a/Assets/Scripts/MapRenderer.cs +++ b/Assets/Scripts/MapRenderer.cs @@ -15,6 +15,7 @@ public class MapRenderer : MonoBehaviour public float queryRadiusMeters = 200f; // radius around lat/lon to query [Header("Location (lat, lon)")] + public GPSManager gpsManager; private double latitude = 50.7727878; private double longitude = 15.0718625; @@ -26,30 +27,31 @@ public class MapRenderer : MonoBehaviour [Header("Road settings")] public Material roadMaterial; public float defaultRoadWidth = 4.0f; // meters + public float motorwayWidth = 10.0f; + public float primaryWidth = 8.0f; + public float secondaryWidth = 6.0f; + public float tertiaryWidth = 5.0f; + [Header("Misc")] public float metersPerUnit = 1f; // scale: 1 unit = 1 meter - public bool autoStart = true; + [Header("Storage")] Dictionary nodes = new Dictionary(); // id -> latlon List parsedWays = new List(); void Start() - { - if (autoStart) { StartCoroutine(RenderMap()); } - } - - public void StartGenerating() { StartCoroutine(RenderMap()); } - IEnumerator RenderMap() { ClearChildren(); - //TODO: GPS update + double[] GPS = gpsManager.GetLastCoords(); + latitude = GPS[0]; + longitude = GPS[1]; string q = $"[out:xml][timeout:90];(way[\"building\"](around:{queryRadiusMeters.ToString().Replace(",", ".")},{latitude.ToString().Replace(",", ".")},{longitude.ToString().Replace(",", ".")});way[\"highway\"](around:{queryRadiusMeters.ToString().Replace(",", ".")},{latitude.ToString().Replace(",", ".")},{longitude.ToString().Replace(",", ".")}););(._;>;);out body;"; @@ -337,10 +339,10 @@ public class MapRenderer : MonoBehaviour { // simple heuristic string h = w.tags["highway"]; - if (h == "motorway") width = 10f; - else if (h == "primary") width = 8f; - else if (h == "secondary") width = 6f; - else if (h == "tertiary") width = 5f; + if (h == "motorway") width = motorwayWidth; + else if (h == "primary") width = primaryWidth; + else if (h == "secondary") width = secondaryWidth; + else if (h == "tertiary") width = tertiaryWidth; else width = defaultRoadWidth; }