Added test mode and manual positioning

This commit is contained in:
2026-03-21 22:40:20 +01:00
parent a1b40ad102
commit be595da357
7 changed files with 481 additions and 73 deletions

View File

@@ -22,6 +22,7 @@ public class GameManager : MonoBehaviour
protected GameManager_Network networkSubsystem;
protected GameManager_UI uiSubsystem;
protected GameManager_Map mapSubsystem;
protected GameManager_Input inputSubsystem;
protected GameClient gameClient;
@@ -41,18 +42,39 @@ public class GameManager : MonoBehaviour
public PathwaySettings pathwaySettings;
public AreaSettings areaSettings;
[Header("GPS")]
public GameObject Player;
[Header("Debug")]
public bool testMode = false;
private GameClient _secondClient;
private GameClient _thirdClient;
private GameManager_Network _secondNetwork;
private GameManager_Network _thirdNetwork;
void Start()
{
DontDestroyOnLoad(this);
if (displayName == null || displayName == "")
{
displayName = "Player_" + UnityEngine.Random.Range(1000, 9999).ToString();
displayName = GenerateUsername();
}
gameClient = new GameClient(GenerateUUID(), /*displayName*/ GenerateUsername());
if (testMode)
{
_secondClient = new GameClient(GenerateUUID(), GenerateUsername());
_secondNetwork = new GameManager_Network(_secondClient);
_thirdClient = new GameClient(GenerateUUID(), GenerateUsername());
_thirdNetwork = new GameManager_Network(_thirdClient);
_secondNetwork.OpenConection();
_thirdNetwork.OpenConection();
}
gameClient = new GameClient(GenerateUUID(), displayName);
uiSubsystem = new GameManager_UI(gameClient, JoinCreateLobby, InLobby, LoadingScreen, GameScreen);
networkSubsystem = new GameManager_Network(gameClient);
mapSubsystem = new GameManager_Map(gameClient, MapCenterPoint, buildingSettings, pathwaySettings, areaSettings);
inputSubsystem = new GameManager_Input(gameClient, Player, testMode);
networkSubsystem.OpenConection();
}
private void Update()
@@ -70,7 +92,7 @@ public class GameManager : MonoBehaviour
}
}
catch (NullReferenceException ex) { }
inputSubsystem.positionCheck();
}
@@ -89,6 +111,10 @@ public class GameManager : MonoBehaviour
public void CreateLobbyButton()
{
networkSubsystem.CrateLobby(50.7727264, 15.0719876);
if (testMode)
{
StartCoroutine(ConnectTestClients());
}
}
public void JoinLobbyButton()
{
@@ -113,5 +139,13 @@ public class GameManager : MonoBehaviour
void OnApplicationQuit()
{
gameClient.Disconnect();
_secondClient?.Disconnect();
_thirdClient?.Disconnect();
}
IEnumerator ConnectTestClients()
{
yield return new WaitForSeconds(2f);
_secondNetwork.JoinLobby(gameClient.CurrentLobbyState.JoinCode);
_thirdNetwork.JoinLobby(gameClient.CurrentLobbyState.JoinCode);
}
}

View File

@@ -0,0 +1,154 @@
using UnityEngine;
using GeoSus.Client;
using System;
namespace Subsystems
{
public static class PositonExtensions
{
public static Position ToLocal(this Position position, Position center)
{
double latDiff = position.Lat - center.Lat;
double lonDiff = position.Lon - center.Lon;
double metersPerDegreeLat = 111320.0;
double metersPerDegreeLon = 111320.0 * Math.Cos(center.Lat * Math.PI / 180.0);
float x = (float)(lonDiff * metersPerDegreeLon);
float z = (float)(latDiff * metersPerDegreeLat);
return new Position(z, x);
}
public static Vector3 ToLocalVector3(this Position position, Position center)
{
return position.ToLocal(center).ToVector3(); //TODO: Implementace v subsystemech
}
public static Vector3 ToVector3(this Position position)
{
return new Vector3((float)position.Lon, 0, (float)position.Lat); //TODO: Implementace v subsystemech
}
public static double DistanceTo(this Vector3 pos, Vector3 other)
{
return Math.Sqrt((other.x - pos.x) * (other.x - pos.x) + (other.z - pos.z) * (other.z - pos.z));
}
}
public class GameManager_Input
{
private GameClient _gameClient;
private Position _currentPosition;
private Position _lastSentPosition;
private GameObject _player;
private bool _testMode;
private float _speed = 0.00001f;
private Position _mapCenter;
public GameManager_Input(GameClient gameClient, GameObject player, bool testMode)
{
_gameClient = gameClient;
_player = player;
_testMode = testMode;
}
public void positionCheck()
{
try
{
if (_gameClient.CurrentLobbyState.Phase == GamePhase.Playing)
{
if (_testMode)
{
if (_currentPosition == null || _currentPosition == new Position(0,0))
{
//Init blok
_currentPosition = _gameClient.CurrentLobbyState.MapData.Center;
_mapCenter = _gameClient.CurrentLobbyState.MapData.Center;
_lastSentPosition = _currentPosition;
}
TestPlayerPosition();
}
else
{
//TODO: Real GPS
}
}
}
catch (NullReferenceException ex) { Debug.Log(ex); }
}
private void TestPlayerPosition()
{
double x = Input.GetAxis("Horizontal");
double y = Input.GetAxis("Vertical");
Debug.Log($"Input: {x}, {y}");
_currentPosition = new Position( _lastSentPosition.Lat + y * _speed, _lastSentPosition.Lon + x * _speed);
Debug.Log($"Current Position: {_currentPosition.Lat}, {_currentPosition.Lon}");
var localCurrent = _currentPosition.ToLocalVector3(_mapCenter);
Debug.Log($"Local Current Position: {localCurrent}");
var heading = CalculateHeading(_lastSentPosition.ToLocalVector3(_mapCenter), localCurrent);
if (heading != null)
{
Debug.Log($"Heading: {heading}");
_player.transform.rotation = Quaternion.Euler(0, (float)heading, 0);
}
_player.transform.position = localCurrent;
try
{
if (_currentPosition != _lastSentPosition)
{
_gameClient.UpdatePosition(_currentPosition);
_lastSentPosition = _currentPosition;
}
}
catch
{
_gameClient.UpdatePosition(_currentPosition);
_lastSentPosition = _currentPosition;
}
}
private double? CalculateHeading(Vector3 first, Vector3 second)
{
double? heading = null;
if ((first - second).magnitude == 0)
{
return null;
}
else if (first.x == second.x && first.z < second.z)
{
return 0;
}
else if (first.x == second.x && first.z > second.z)
{
return 180;
}
else if (first.x > second.x && first.z == second.z)
{
return 270;
}
else if (first.x < second.x && first.z == second.z)
{
return 90;
}
else if (first.x < second.x && first.z < second.z)
{
heading = Math.Asin((second.z - first.z) / first.DistanceTo(second));
return (heading * 180) / Math.PI;
}
else if (first.x < second.x && first.z > second.z)
{
heading = Math.Asin((second.z - first.z) / first.DistanceTo(second));
return (heading * 180) / Math.PI + 180;
}
else if (first.x > second.x && first.z < second.z)
{
heading = Math.Asin((second.z - first.z) / first.DistanceTo(second));
return (heading * 180) / Math.PI - 90;
}
else if (first.x > second.x && first.z > second.z)
{
heading = Math.Asin((second.z - first.z) / first.DistanceTo(second));
return (heading * 180) / Math.PI - 90;
}
else
{
return heading;
}
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 2ef1abfb1e85a7943925f9dc3cfea742

View File

@@ -89,7 +89,12 @@ namespace Subsystems{
foreach (var building in _gameClient.CurrentLobbyState.MapData.GetBuildings())
{
var buildingType = _gameClient.CurrentLobbyState.MapData.BuildingTypes[_gameClient.CurrentLobbyState.MapData.GetBuildings().IndexOf(building)];
string buildingType = "Unknown";
try
{
buildingType = _gameClient.CurrentLobbyState.MapData.BuildingTypes[_gameClient.CurrentLobbyState.MapData.GetBuildings().IndexOf(building)];
}
catch (Exception ex) { Debug.Log($"Error: {ex.Message}"); }
building.Name = buildingType;
GameObject b = BuildBuildingMesh(building);
b.transform.parent = buildingsRoot.transform;
@@ -106,6 +111,17 @@ namespace Subsystems{
}
//TODO: POIs
}
void ClearChildren()
{
List<GameObject> toDestroy = new List<GameObject>();
foreach (Transform t in _mapCenterPoint.transform)
toDestroy.Add(t.gameObject);
foreach (var g in toDestroy)
{
UnityEngine.Object.DestroyImmediate(g);
}
}
#region Mesh Building
GameObject BuildBuildingMesh(MapBuilding b)
{
var building = new GameObject($"Building_{b.Name ?? "Unknown"}");
@@ -210,7 +226,7 @@ namespace Subsystems{
line.positionCount = w.Points.Count;
for (int i = 0; i < w.Points.Count; i++)
{
Vector3 pos = LatLonToLocal(w.Points[i]);
Vector3 pos = w.Points[i].ToLocalVector3(_gameClient.CurrentLobbyState.MapData.Center);
pos.y = 0.1f; // Mírně nad zemí
line.SetPosition(i, pos);
}
@@ -261,44 +277,14 @@ namespace Subsystems{
return area;
}
//TODO: POIs
void ClearChildren()
{
List<GameObject> toDestroy = new List<GameObject>();
foreach (Transform t in _mapCenterPoint.transform)
toDestroy.Add(t.gameObject);
foreach (var g in toDestroy)
{
UnityEngine.Object.DestroyImmediate(g);
}
}
Vector3 LatLonToLocal(Position position)
{
if (_gameClient.CurrentLobbyState.MapData == null) return Vector3.zero;
// Výpočet vzdálenosti a směru od středu
// Zjednodušená verze - pro malé vzdálenosti je dost přesná
double latDiff = position.Lat - _gameClient.CurrentLobbyState.MapData.Center.Lat;
double lonDiff = position.Lon - _gameClient.CurrentLobbyState.MapData.Center.Lon;
// Převod stupňů na metry
// 1 stupeň latitude ≈ 111320 metrů
// 1 stupeň longitude závisí na latitude: 111320 * cos(latitude)
double metersPerDegreeLat = 111320.0;
double metersPerDegreeLon = 111320.0 * Math.Cos(_gameClient.CurrentLobbyState.MapData.Center.Lat * Math.PI / 180.0);
float x = (float)(lonDiff * metersPerDegreeLon);
float z = (float)(latDiff * metersPerDegreeLat);
return new Vector3(x, 0, z);
}
#endregion
#region Polygon Utils
private Vector3 CalculatePolygonCenter(List<Position> points)
{
Vector3 center = Vector3.zero;
foreach (var point in points)
{
center += LatLonToLocal(point);
center += point.ToLocalVector3(_gameClient.CurrentLobbyState.MapData.Center);
}
return center / points.Count;
}
@@ -314,7 +300,7 @@ namespace Subsystems{
for (int i = 0; i < vertexCount; i++)
{
Vector3 pos = LatLonToLocal(outline[i]) - center;
Vector3 pos = outline[i].ToLocalVector3(_gameClient.CurrentLobbyState.MapData.Center) - center;
vertices[i] = pos; // Spodní
vertices[i + vertexCount] = pos + Vector3.up * height; // Horní
}
@@ -364,7 +350,7 @@ namespace Subsystems{
for (int i = 0; i < vertexCount; i++)
{
vertices[i] = LatLonToLocal(outline[i]) - center;
vertices[i] = outline[i].ToLocalVector3(_gameClient.CurrentLobbyState.MapData.Center) - center;
}
// Triangulace - fan pattern

View File

@@ -2,6 +2,9 @@ using GeoSus.Client;
using System.Collections;
using System.Threading.Tasks;
using UnityEngine;
using System.Collections.Generic;
using Subsystems;
using System.Linq;
namespace Subsystems
{
@@ -129,8 +132,6 @@ namespace Subsystems
Debug.LogError("Failed to create lobby: " + message.Error);
}
}
public void CrateLobby(double lat, double lon)
{
_gameClient.CreateLobby(new Position(lat, lon));