AmazingAlek 485698e93b
Alek/simplify name messages (#88)
* Showing player markers

* Marker above head

* Use player name on marker

* Sync full state on init

* Sync game state on start

* removed name from death and leave message

Co-authored-by: Ricardo Lopes <raicuparta@gmail.com>
2020-05-19 20:05:15 +02:00

57 lines
1.6 KiB
C#

using System.Linq;
using QSB.Messaging;
using UnityEngine;
using UnityEngine.Networking;
namespace QSB.Events
{
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)
{
var playerName = PlayerJoin.PlayerNames[message.SenderId];
DebugLog.All(playerName, "left");
PlayerJoin.PlayerNames.Remove(message.SenderId);
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);
}
}
}