2026-04-30 15:08:19 +07:00
|
|
|
using System;
|
2026-04-11 18:13:40 +07:00
|
|
|
using Fusion;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
// struct quản lý thông tin
|
|
|
|
|
public struct _PlayerMetaData : INetworkStruct
|
|
|
|
|
{
|
|
|
|
|
public NetworkString<_16> Name;
|
2026-04-23 08:56:35 +07:00
|
|
|
public _Role Role;
|
2026-04-29 13:10:00 +07:00
|
|
|
public NetworkBool IsReady;
|
2026-04-11 18:13:40 +07:00
|
|
|
}
|
|
|
|
|
|
2026-04-30 00:55:16 +07:00
|
|
|
public class PlayerDataManager : NetworkBehaviour
|
2026-04-11 18:13:40 +07:00
|
|
|
{
|
2026-04-30 15:08:19 +07:00
|
|
|
public static PlayerDataManager Instance { get; private set; }
|
|
|
|
|
|
2026-04-11 18:13:40 +07:00
|
|
|
[Networked]
|
|
|
|
|
public NetworkDictionary<PlayerRef, _PlayerMetaData> Players => default;
|
2026-05-02 00:00:31 +07:00
|
|
|
|
|
|
|
|
[Networked]
|
|
|
|
|
public PlayerRef Leader { get; set; }
|
2026-04-11 18:13:40 +07:00
|
|
|
|
2026-04-30 15:08:19 +07:00
|
|
|
public event Action<PlayerRef, string> OnChatMessageReceived;
|
|
|
|
|
|
|
|
|
|
public override void Spawned()
|
|
|
|
|
{
|
|
|
|
|
Instance = this;
|
2026-05-02 00:00:31 +07:00
|
|
|
if (Object.HasStateAuthority)
|
|
|
|
|
{
|
|
|
|
|
Leader = Runner.LocalPlayer;
|
|
|
|
|
}
|
2026-04-30 15:08:19 +07:00
|
|
|
}
|
|
|
|
|
|
2026-05-01 21:58:20 +07:00
|
|
|
public override void Despawned(NetworkRunner runner, bool hasState)
|
|
|
|
|
{
|
|
|
|
|
if (Instance == this) Instance = null;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-02 00:00:31 +07:00
|
|
|
[Rpc(RpcSources.All, RpcTargets.StateAuthority)]
|
|
|
|
|
public void RPC_TransferLeader(PlayerRef newLeader)
|
|
|
|
|
{
|
|
|
|
|
if (Players.ContainsKey(newLeader))
|
|
|
|
|
{
|
|
|
|
|
Leader = newLeader;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-11 18:13:40 +07:00
|
|
|
[Rpc(RpcSources.All, RpcTargets.StateAuthority)]
|
|
|
|
|
public void RPC_UpdatePlayerMetaData(PlayerRef playerRef, _PlayerMetaData metaData)
|
|
|
|
|
{
|
2026-05-01 21:58:20 +07:00
|
|
|
if (Object == null || !Object.IsValid) return;
|
2026-04-11 18:13:40 +07:00
|
|
|
Players.Set(playerRef, metaData);
|
|
|
|
|
}
|
2026-04-29 13:10:00 +07:00
|
|
|
|
|
|
|
|
[Rpc(RpcSources.All, RpcTargets.StateAuthority)]
|
|
|
|
|
public void RPC_SetReady(PlayerRef playerRef, bool ready)
|
|
|
|
|
{
|
2026-05-01 21:58:20 +07:00
|
|
|
if (Object == null || !Object.IsValid) return;
|
2026-04-29 13:10:00 +07:00
|
|
|
if (Players.TryGet(playerRef, out var data))
|
|
|
|
|
{
|
|
|
|
|
data.IsReady = ready;
|
|
|
|
|
Players.Set(playerRef, data);
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-04-30 15:08:19 +07:00
|
|
|
|
|
|
|
|
[Rpc(RpcSources.All, RpcTargets.All)]
|
|
|
|
|
public void RPC_SendChatMessage(PlayerRef sender, string message)
|
|
|
|
|
{
|
|
|
|
|
OnChatMessageReceived?.Invoke(sender, message);
|
|
|
|
|
}
|
2026-04-11 18:13:40 +07:00
|
|
|
|
|
|
|
|
public bool TryGetPlayerMetaData(PlayerRef playerRef, out _PlayerMetaData metaData)
|
|
|
|
|
{
|
2026-05-01 21:58:20 +07:00
|
|
|
metaData = default;
|
|
|
|
|
// Kiểm tra xem object đã được Spawned chưa trước khi truy cập networked property
|
|
|
|
|
if (Object == null || !Object.IsValid) return false;
|
|
|
|
|
|
2026-04-11 18:13:40 +07:00
|
|
|
return Players.TryGet(playerRef, out metaData);
|
|
|
|
|
}
|
|
|
|
|
}
|