2020-03-07 16:42:43 +01:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using QSB.Messaging;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
using UnityEngine.Networking;
|
|
|
|
|
|
|
|
|
|
namespace QSB.Events
|
|
|
|
|
{
|
2020-07-28 00:13:43 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Client-only-side component for managing player leaves.
|
|
|
|
|
/// </summary>
|
2020-03-07 16:42:43 +01:00
|
|
|
|
public class PlayerLeave : NetworkBehaviour
|
|
|
|
|
{
|
|
|
|
|
private MessageHandler<LeaveMessage> _leaveHandler;
|
|
|
|
|
|
|
|
|
|
private void Awake()
|
|
|
|
|
{
|
|
|
|
|
_leaveHandler = new MessageHandler<LeaveMessage>();
|
|
|
|
|
_leaveHandler.OnClientReceiveMessage += OnClientReceiveMessage;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Leave(uint playerId, uint[] objectIds)
|
|
|
|
|
{
|
|
|
|
|
var message = new LeaveMessage
|
|
|
|
|
{
|
|
|
|
|
SenderId = playerId,
|
|
|
|
|
ObjectIds = objectIds
|
|
|
|
|
};
|
|
|
|
|
_leaveHandler.SendToAll(message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnClientReceiveMessage(LeaveMessage message)
|
|
|
|
|
{
|
2020-07-28 00:13:43 +01:00
|
|
|
|
var playerName = PlayerRegistry.GetPlayerName(message.SenderId);
|
|
|
|
|
DebugLog.ToAll(playerName, "disconnected.");
|
|
|
|
|
PlayerRegistry.RemovePlayer(message.SenderId);
|
2020-03-07 16:42:43 +01:00
|
|
|
|
foreach (var objectId in message.ObjectIds)
|
|
|
|
|
{
|
|
|
|
|
DestroyObject(objectId);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void DestroyObject(uint objectId)
|
|
|
|
|
{
|
|
|
|
|
var component = GameObject.FindObjectsOfType<NetworkBehaviour>()
|
|
|
|
|
.FirstOrDefault(x => x.netId.Value == objectId);
|
|
|
|
|
if (component == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
var transformSync = component.GetComponent<TransformSync.TransformSync>();
|
|
|
|
|
if (transformSync != null)
|
|
|
|
|
{
|
|
|
|
|
Destroy(transformSync.SyncedTransform.gameObject);
|
|
|
|
|
}
|
|
|
|
|
Destroy(component.gameObject);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|