feat: backend state machine, Unity URL prod, simplify GameSetup

This commit is contained in:
2026-05-15 09:16:01 +02:00
parent c322793b0d
commit 1e37e44143
6 changed files with 400 additions and 215 deletions

View File

@@ -1,154 +1,13 @@
using UnityEngine;
/// <summary>
/// Global game setup applied at startup:
/// - Application.runInBackground (physics continues on ALT-TAB)
/// - Tall invisible arena barriers (prevent ball escape)
/// - Visual enhancements: obstacle colors, floor tint, lighting contrast
/// Attach to a persistent GameObject (e.g. NetworkManager).
/// Global startup settings. Scene geometry and materials are set directly in the Editor.
/// </summary>
public class GameSetup : MonoBehaviour
{
[Header("Arena Boundaries")]
public float arenaHalfSize = 45f;
public float barrierHeight = 50f;
public float barrierThickness = 1f;
[Header("Visuals")]
public bool enhanceVisuals = true;
void Awake()
{
// --- Keep physics and network running on focus loss ---
Application.runInBackground = true;
Application.targetFrameRate = 60;
// Barriers removed — respawn system handles falls (Y < -10)
}
void Start()
{
if (enhanceVisuals)
EnhanceVisuals();
}
// --- Barrier creation ---
private void CreateBarrier(string name, Vector3 position, Vector3 size)
{
var go = new GameObject(name);
go.transform.position = position;
var col = go.AddComponent<BoxCollider>();
col.size = size;
// No Renderer = invisible. Static collider = immovable wall.
}
// --- Visual enhancements ---
private void EnhanceVisuals()
{
TintFloor();
ColorObstacles();
ColorWallsAndGrids();
EnhanceLighting();
}
private void TintFloor()
{
var plane = GameObject.Find("Plane");
if (plane == null) return;
var rend = plane.GetComponent<Renderer>();
if (rend == null) return;
var mat = new Material(rend.sharedMaterial);
// Soft blue-gray instead of flat white
Color floorColor = new Color(0.70f, 0.74f, 0.82f, 1f);
SetMatColor(mat, floorColor);
rend.material = mat;
}
private void ColorObstacles()
{
Color[] palette =
{
new Color(0.42f, 0.55f, 0.75f), // Steel blue
new Color(0.60f, 0.45f, 0.68f), // Muted purple
new Color(0.48f, 0.68f, 0.55f), // Sage green
new Color(0.74f, 0.52f, 0.42f), // Warm terracotta
new Color(0.68f, 0.65f, 0.44f), // Sandy gold
new Color(0.44f, 0.62f, 0.72f), // Slate teal
};
for (int i = 1; i <= 18; i++)
{
var obs = GameObject.Find($"Obs_{i}");
if (obs == null) continue;
var rend = obs.GetComponent<Renderer>();
if (rend == null) continue;
var mat = new Material(rend.sharedMaterial);
SetMatColor(mat, palette[i % palette.Length]);
rend.material = mat;
}
}
private void ColorWallsAndGrids()
{
Color wallColor = new Color(0.50f, 0.54f, 0.62f);
foreach (string name in new[] { "Wall_North", "Wall_South", "Wall_East", "Wall_West" })
{
var wall = GameObject.Find(name);
if (wall == null) continue;
var rend = wall.GetComponent<Renderer>();
if (rend == null) continue;
var mat = new Material(rend.sharedMaterial);
SetMatColor(mat, wallColor);
rend.material = mat;
}
Color gridColor = new Color(0.58f, 0.61f, 0.68f);
for (int i = 1; i <= 4; i++)
{
foreach (string dir in new[] { "NS", "EW" })
{
var grid = GameObject.Find($"Grid_{dir}_{i}");
if (grid == null) continue;
var rend = grid.GetComponent<Renderer>();
if (rend == null) continue;
var mat = new Material(rend.sharedMaterial);
SetMatColor(mat, gridColor);
rend.material = mat;
}
}
}
private void EnhanceLighting()
{
// Directional light: warm white, stronger, soft shadows
var lights = FindObjectsByType<Light>(FindObjectsSortMode.None);
foreach (var light in lights)
{
if (light.type == LightType.Directional)
{
light.color = new Color(1f, 0.96f, 0.90f); // Warm white
light.intensity = 1.6f;
light.shadows = LightShadows.Soft;
light.shadowStrength = 0.75f;
}
}
// Ambient: cool tint for contrast with warm direct light
RenderSettings.ambientMode = UnityEngine.Rendering.AmbientMode.Flat;
RenderSettings.ambientLight = new Color(0.32f, 0.36f, 0.48f);
}
// --- Utility ---
private static void SetMatColor(Material mat, Color color)
{
if (mat.HasProperty("_BaseColor"))
mat.SetColor("_BaseColor", color);
if (mat.HasProperty("_Color"))
mat.color = color;
}
}

View File

@@ -67,11 +67,11 @@ public class DebugNetworkUI : MonoBehaviour
string name = !string.IsNullOrEmpty(nm.LocalPlayerName) ? nm.LocalPlayerName : "\u2014";
string room = !string.IsNullOrEmpty(nm.RoomId) ? nm.RoomId[..Mathf.Min(8, nm.RoomId.Length)] : "\u2014";
string sess = !string.IsNullOrEmpty(nm.LocalSessionId) ? nm.LocalSessionId[..Mathf.Min(6, nm.LocalSessionId.Length)] : "\u2014";
info = $" {dot} <b>{name}</b> | Room {room} | Sess {sess} | {nm.PlayerCount}P | {nm.serverURL} | {_currentFps:F0} FPS";
info = $" {dot} <b>{name}</b> | Room {room} | Sess {sess} | {nm.PlayerCount}P | {"wss://rolld.io:2567"} | {_currentFps:F0} FPS";
}
else
{
info = $" {dot} {nm.ConnectionStatus} | {nm.serverURL} | {_currentFps:F0} FPS";
info = $" {dot} {nm.ConnectionStatus} | {"wss://rolld.io:2567"} | {_currentFps:F0} FPS";
}
GUI.Label(new Rect(0, 0, Screen.width, h), info, ImGuiSkin.HudLabel);
@@ -93,7 +93,7 @@ public class DebugNetworkUI : MonoBehaviour
GUIStyle statusStyle = nm.IsConnected ? ImGuiSkin.StatusGreen : ImGuiSkin.StatusRed;
GUILayout.Label($"\u25CF {nm.ConnectionStatus}", statusStyle);
ImGuiSkin.DrawField("Server", nm.serverURL);
ImGuiSkin.DrawField("Server", "wss://rolld.io:2567");
ImGuiSkin.DrawField("Room ID", string.IsNullOrEmpty(nm.RoomId) ? "\u2014" : nm.RoomId);
ImGuiSkin.DrawField("Session", string.IsNullOrEmpty(nm.LocalSessionId) ? "\u2014" : nm.LocalSessionId);
ImGuiSkin.DrawField("Players", nm.PlayerCount.ToString());

View File

@@ -327,7 +327,7 @@ public class LobbyUI : MonoBehaviour
if (_isConnecting && !NetworkManager.Instance.IsConnected)
{
_isConnecting = false;
_statusMessage = "Erreur : Timeout de connexion. Vérifiez que le serveur est lancé.";
_statusMessage = "Erreur : Impossible de joindre rolld.io. Réessayez dans quelques instants.";
if (!string.IsNullOrEmpty(NetworkManager.Instance.LastError))
{
_statusMessage += $"\n{NetworkManager.Instance.LastError}";

View File

@@ -12,9 +12,7 @@ public class NetworkManager : MonoBehaviour
{
public static NetworkManager Instance { get; private set; }
[Header("Connection")]
[Tooltip("Colyseus server endpoint (overridden by frontend via SetServerURL)")]
public string serverURL = "ws://localhost:2567";
private const string serverURL = "wss://rolld.io:2567";
[Header("Prefab")]
[Tooltip("Prefab for remote players (must have RemotePlayerController)")]
@@ -94,13 +92,6 @@ public class NetworkManager : MonoBehaviour
}
}
/// <summary>Called from frontend JS via SendMessage to override the server URL.</summary>
public void SetServerURL(string url)
{
serverURL = url;
Debug.Log($"[Network] Server URL set to: {url}");
}
public NetworkPlayer GetLocalPlayerState()
{
if (_room == null || _room.State.players == null || string.IsNullOrEmpty(LocalSessionId)) return null;