Added gps and ITask update
This commit is contained in:
@@ -1,10 +1,7 @@
|
||||
using UnityEngine;
|
||||
using GeoSus.Client;
|
||||
using Subsystems;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System;
|
||||
using TMPro;
|
||||
/*
|
||||
|
||||
@@ -1,8 +1,21 @@
|
||||
using UnityEngine;
|
||||
using GeoSus.Client;
|
||||
using System;
|
||||
using System.Collections;
|
||||
|
||||
namespace Subsystems
|
||||
{
|
||||
internal class CoroutineHost : MonoBehaviour
|
||||
{
|
||||
public CoroutineHost() { }
|
||||
}
|
||||
internal enum GPSState
|
||||
{
|
||||
Uninitialized,
|
||||
Initializing,
|
||||
Running,
|
||||
Failed
|
||||
}
|
||||
public static class PositonExtensions
|
||||
{
|
||||
public static Position ToLocal(this Position position, Position center)
|
||||
@@ -37,8 +50,11 @@ namespace Subsystems
|
||||
private Position _lastSentPosition;
|
||||
private GameObject _player;
|
||||
private bool _testMode;
|
||||
|
||||
private GPSState _GPSState = GPSState.Uninitialized;
|
||||
private float _speed = 0.00001f;
|
||||
private Position _mapCenter;
|
||||
private CoroutineHost _coroutineHost = new CoroutineHost();
|
||||
public GameManager_Input(GameClient gameClient, GameObject player, bool testMode)
|
||||
{
|
||||
_gameClient = gameClient;
|
||||
@@ -54,7 +70,7 @@ namespace Subsystems
|
||||
if (_testMode)
|
||||
{
|
||||
|
||||
if (_currentPosition == null || _currentPosition == new Position(0,0))
|
||||
if (_currentPosition == null || _currentPosition == new Position(0, 0))
|
||||
{
|
||||
//Init blok
|
||||
_currentPosition = _gameClient.CurrentLobbyState.MapData.Center;
|
||||
@@ -66,7 +82,37 @@ namespace Subsystems
|
||||
}
|
||||
else
|
||||
{
|
||||
//TODO: Real GPS
|
||||
if (_GPSState == GPSState.Uninitialized)
|
||||
{
|
||||
_coroutineHost.StartCoroutine(InitiallizeGPS());
|
||||
return;
|
||||
}
|
||||
else if (_GPSState == GPSState.Initializing)
|
||||
{
|
||||
return;
|
||||
}
|
||||
else if (_GPSState == GPSState.Running)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (_currentPosition != _lastSentPosition)
|
||||
{
|
||||
_gameClient.UpdatePosition(_currentPosition);
|
||||
_lastSentPosition = _currentPosition;
|
||||
_player.transform.position = _currentPosition.ToLocalVector3(_mapCenter);
|
||||
_player.transform.rotation = Quaternion.Euler(0, (float)CalculateHeading(_lastSentPosition.ToLocalVector3(_mapCenter), _currentPosition.ToLocalVector3(_mapCenter)), 0);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.Log(ex);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.Log("GPS failed, trying again...");)
|
||||
_GPSState = GPSState.Uninitialized;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -150,5 +196,59 @@ namespace Subsystems
|
||||
return heading;
|
||||
}
|
||||
}
|
||||
IEnumerator InitiallizeGPS()
|
||||
{
|
||||
_GPSState = GPSState.Initializing;
|
||||
if (!Input.location.isEnabledByUser)
|
||||
{
|
||||
Debug.LogError("Location not enabled on device or app does not have permission to access location");
|
||||
}
|
||||
// Starts the location service.
|
||||
|
||||
float desiredAccuracyInMeters = 10f;
|
||||
float updateDistanceInMeters = 10f;
|
||||
|
||||
Input.location.Start(desiredAccuracyInMeters, updateDistanceInMeters);
|
||||
|
||||
// Waits until the location service initializes
|
||||
int maxWait = 20;
|
||||
while (Input.location.status == LocationServiceStatus.Initializing && maxWait > 0)
|
||||
{
|
||||
yield return new WaitForSeconds(1);
|
||||
maxWait--;
|
||||
}
|
||||
|
||||
// If the service didn't initialize in 20 seconds this cancels location service use.
|
||||
if (maxWait < 1)
|
||||
{
|
||||
_GPSState = GPSState.Failed;
|
||||
Debug.LogError("Timed out");
|
||||
yield break;
|
||||
}
|
||||
_GPSState = GPSState.Running;
|
||||
yield return _coroutineHost.StartCoroutine(GPSService());
|
||||
}
|
||||
IEnumerator GPSService()
|
||||
{
|
||||
// Check if the user has location service enabled.
|
||||
|
||||
|
||||
// If the connection failed this cancels location service use.
|
||||
if (Input.location.status == LocationServiceStatus.Failed)
|
||||
{
|
||||
Debug.LogError("Unable to determine device location");
|
||||
yield break;
|
||||
}
|
||||
else
|
||||
{
|
||||
// If the connection succeeded, this retrieves the device's current location and displays it in the Console window.
|
||||
_currentPosition = new Position(Input.location.lastData.latitude, Input.location.lastData.longitude);
|
||||
Debug.Log("Location: " + Input.location.lastData.latitude + " " + Input.location.lastData.longitude + " " + Input.location.lastData.altitude + " " + Input.location.lastData.horizontalAccuracy + " " + Input.location.lastData.timestamp);
|
||||
yield return new WaitForSeconds(5f);
|
||||
}
|
||||
|
||||
// Stops the location service if there is no need to query location updates continuously.
|
||||
yield return _coroutineHost.StartCoroutine(GPSService());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,10 +2,10 @@ using GeoSus.Client;
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
public enum TaskType
|
||||
/*public enum TaskType
|
||||
{
|
||||
Task //TODO: Typy úkolù
|
||||
}
|
||||
}*/
|
||||
|
||||
|
||||
|
||||
@@ -14,7 +14,7 @@ public interface ITask
|
||||
public string TaskID { get; } // Unikátní ID úkolu pro server
|
||||
public TaskType TaskType { get; } // Typ úkolu
|
||||
public string TaskName { get; } // Viditelný název úkolu
|
||||
public (double, double) TaskLocation { get; } // Polohy na mapì
|
||||
public Position TaskLocation { get; } // Polohy na mapì
|
||||
public bool IsCompleted { get; } // Stav dokonèení úkolu
|
||||
|
||||
void Initialize(Action<ITask> onCompleted); // Vytvoøení tasku + naètení postupu
|
||||
@@ -27,7 +27,7 @@ public class Wires : ITask{
|
||||
public string TaskID { get; set; } // Unikátní ID úkolu pro server
|
||||
public TaskType TaskType { get; set; } // Typ úkolu
|
||||
public string TaskName { get; set; } // Viditelný název úkolu
|
||||
public (double, double) TaskLocation { get; set; } // Poloha na mapì
|
||||
public Position TaskLocation { get; set; } // Poloha na mapì
|
||||
public bool IsCompleted { get; private set; } // Stav dokonèení úkolu
|
||||
private Action<ITask> _onCompleted;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user