78 lines
2.4 KiB
C#
78 lines
2.4 KiB
C#
using FishNet.Object;
|
|
using UnityEngine;
|
|
using Ashwild.Player;
|
|
|
|
namespace Ashwild.Network
|
|
{
|
|
/// <summary>
|
|
/// Lives on the networked player root and decides which parts of the player are "local".
|
|
/// Only the owning client keeps input, camera, audio listener, stats, etc. active; on every
|
|
/// other machine this player is a passive puppet driven by its NetworkTransform.
|
|
/// Wire the owner-only components/objects in the inspector.
|
|
/// </summary>
|
|
[DisallowMultipleComponent]
|
|
[RequireComponent(typeof(NetworkObject))]
|
|
public class PlayerNetworkController : NetworkBehaviour
|
|
{
|
|
#region Serialized Fields
|
|
|
|
[Header("Owner-only behaviours")]
|
|
[Tooltip("Components active only on the local player (input, locomotion, camera, stats, inventory, audio…).")]
|
|
[SerializeField] private Behaviour[] ownerOnlyBehaviours;
|
|
|
|
[Header("Owner-only objects")]
|
|
[SerializeField] private GameObject[] ownerOnlyObjects;
|
|
|
|
[Header("Debug")]
|
|
[Tooltip("Logs ownership resolution to the console.")]
|
|
[SerializeField] private bool verboseLogging = true;
|
|
|
|
#endregion
|
|
|
|
#region Network Lifecycle
|
|
|
|
/// <summary>
|
|
/// Gates owner-only pieces as soon as this player is initialized on a client.
|
|
/// </summary>
|
|
public override void OnStartClient()
|
|
{
|
|
base.OnStartClient();
|
|
|
|
bool owner = base.IsOwner;
|
|
Log($"OnStartClient — owner={owner}, ownerId={base.OwnerId}");
|
|
|
|
foreach (Behaviour behaviour in ownerOnlyBehaviours)
|
|
if (behaviour != null) behaviour.enabled = owner;
|
|
|
|
foreach (GameObject go in ownerOnlyObjects)
|
|
if (go != null) go.SetActive(owner);
|
|
|
|
if (owner)
|
|
{
|
|
gameObject.name = "Player (Local)";
|
|
PlayerEvents.RaiseLocalPlayerSpawned();
|
|
Log("✅ Joueur local prêt et possédé.");
|
|
}
|
|
else
|
|
{
|
|
gameObject.name = $"Player (Remote {base.OwnerId})";
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Internal Helpers
|
|
|
|
/// <summary>
|
|
/// Prefixed console log gated by the verbose toggle.
|
|
/// </summary>
|
|
private void Log(string message)
|
|
{
|
|
if (verboseLogging)
|
|
Debug.Log($"[PlayerNetwork] {message}", this);
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|