Upload project

This commit is contained in:
Lauchstange committed 2025-01-30 17:20:27 +01:00
1 parent 6bbeab16d1
commit bf545f3baf
371 files changed
+91095

No files matched your search

@@ -0,0 +1,141 @@
using Client;
using Common;
using TMPro;
using Unity.Entities;
using Unity.NetCode;
using Unity.Networking.Transport;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
namespace UI
{
public class ClientConnectionManager : MonoBehaviour
{
[SerializeField] private TMP_InputField _addressField;
[SerializeField] private TMP_InputField _portField;
[SerializeField] private TMP_Dropdown _connectionModeDropdown;
[SerializeField] private Button _connectButton;
[SerializeField] private ColorPicker _colorPicker;
private ushort Port => ushort.Parse(_portField.text);
private string Address => _addressField.text;
private void OnEnable()
{
_addressField.text = "127.0.0.1";
_portField.text = "7979";
_connectionModeDropdown.onValueChanged.AddListener(OnConnectionModeChanged);
_connectButton.onClick.AddListener(OnButtonConnect);
OnConnectionModeChanged(_connectionModeDropdown.value);
}
private void OnDisable()
{
_connectionModeDropdown.onValueChanged.RemoveAllListeners();
_connectButton.onClick.RemoveAllListeners();
}
private void OnConnectionModeChanged(int connectionMode)
{
string buttonLabel;
_connectButton.enabled = true;
switch (connectionMode)
{
case 0:
buttonLabel = "Start Host";
break;
case 1 :
buttonLabel = "Start Server";
break;
case 2:
buttonLabel = "Start Client";
break;
default:
buttonLabel = "<ERROR>";
_connectButton.enabled = false;
break;
}
var buttonText = _connectButton.GetComponentInChildren<TextMeshProUGUI>();
buttonText.text = buttonLabel;
}
private void OnButtonConnect()
{
DestroyLocalSimulationWorld();
SceneManager.LoadScene(1);
switch (_connectionModeDropdown.value)
{
case 0:
StartServer();
StartClient();
break;
case 1:
StartServer();
break;
case 2:
StartClient();
break;
default:
Debug.LogError("Error: Unknown connection mode", gameObject);
break;
}
}
private static void DestroyLocalSimulationWorld()
{
//Destroy default world, we will create a new one
foreach (var world in World.All)
{
if (world.Flags == WorldFlags.Game)
{
world.Dispose();
break;
}
}
}
private void StartServer()
{
//Create server world
var serverWorld = ClientServerBootstrap.CreateServerWorld("TD Server World");
//Define endpoint for world to listen to, and set network driver to listen to specified port
var serverEndPoint = NetworkEndpoint.AnyIpv4.WithPort(Port);
{
using var networkDriverQuery =
serverWorld.EntityManager.CreateEntityQuery(ComponentType.ReadWrite<NetworkStreamDriver>());
networkDriverQuery.GetSingletonRW<NetworkStreamDriver>().ValueRW.Listen(serverEndPoint);
}
}
private void StartClient()
{
//Create server world
var clientWorld = ClientServerBootstrap.CreateClientWorld("TD Client World");
//Set IP adress and port for server to host on
var connectionEndpoint = NetworkEndpoint.Parse(Address, Port);
{
using var networkDriverQuery = clientWorld.EntityManager.CreateEntityQuery(ComponentType.ReadWrite<NetworkStreamDriver>());
networkDriverQuery.GetSingletonRW<NetworkStreamDriver>().ValueRW.Connect(clientWorld.EntityManager, connectionEndpoint);
}
World.DefaultGameObjectInjectionWorld = clientWorld;
//create entity with join request component, so that the ClientRequestGameEntrySystem can take over
var teamRequestEntity = clientWorld.EntityManager.CreateEntity();
clientWorld.EntityManager.AddComponentData(teamRequestEntity, new ClientTeamRequest
{
Value = TeamType.Player,
Color = _colorPicker.Color
});
}
}
}
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: dd0b0c7ce7b8fc643af547eca631b1d4
+63
View File
@@ -0,0 +1,63 @@
using UnityEngine;
using UnityEngine.UI;
namespace UI
{
public class ColorPicker : MonoBehaviour
{
[SerializeField] private Renderer previewRenderer;
[SerializeField] private Button buttonNext;
[SerializeField] private Button buttonPrev;
[SerializeField] private Image previewImage;
private int _currentIndex = 0;
private Color _currentColor;
private Color[] _colors = new Color[] { Color.green, Color.magenta, Color.yellow, Color.cyan };
// Start is called once before the first execution of Update after the MonoBehaviour is created
public Color Color => _currentColor;
void Start()
{
_currentColor = _colors[_currentIndex];
buttonNext.onClick.AddListener(NextColor);
buttonPrev.onClick.AddListener(PrevColor);
var newMaterial = new Material(previewRenderer.sharedMaterial);
newMaterial.color = _currentColor;
previewRenderer.sharedMaterial = newMaterial;
UpdateMaterialColor();
}
private void UpdateMaterialColor()
{
_currentColor = _colors[_currentIndex];
previewImage.color = _currentColor;
previewRenderer.sharedMaterial.color = _currentColor;
}
private void PrevColor()
{
_currentIndex--;
if (_currentIndex < 0)
{
_currentIndex = _colors.Length - 1;
}
UpdateMaterialColor();
}
private void NextColor()
{
_currentIndex++;
if (_currentIndex == _colors.Length)
{
_currentIndex = 0;
}
UpdateMaterialColor();
}
}
}
+2
View File
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: fb59c4864abd9254aa540ee9f2230239