PlayerRespawnMessage

This commit is contained in:
JohnCorby 2021-12-22 22:09:00 -08:00
parent 54810251cd
commit a7c7544314
4 changed files with 44 additions and 41 deletions

View File

@ -65,7 +65,6 @@
public const string QSBComponentDamaged = nameof(QSBComponentDamaged);
public const string QSBComponentRepaired = nameof(QSBComponentRepaired);
public const string QSBComponentRepairTick = nameof(QSBComponentRepairTick);
public const string QSBPlayerRespawn = nameof(QSBPlayerRespawn);
public const string QSBProbeEvent = nameof(QSBProbeEvent);
public const string QSBProbeStartRetrieve = nameof(QSBProbeStartRetrieve);
public const string QSBRetrieveProbe = nameof(QSBRetrieveProbe);

View File

@ -1,39 +0,0 @@
using QSB.ClientServerStateSync;
using QSB.Events;
using QSB.Messaging;
using QSB.Player;
namespace QSB.RespawnSync.Events
{
internal class PlayerRespawnEvent : QSBEvent<PlayerMessage>
{
public override bool RequireWorldObjectsReady => false;
public override void SetupListener()
=> GlobalMessenger<uint>.AddListener(EventNames.QSBPlayerRespawn, Handler);
public override void CloseListener()
=> GlobalMessenger<uint>.RemoveListener(EventNames.QSBPlayerRespawn, Handler);
private void Handler(uint playerId) => SendEvent(CreateMessage(playerId));
private PlayerMessage CreateMessage(uint playerId) => new()
{
AboutId = playerId
};
public override void OnReceiveLocal(bool server, PlayerMessage message)
=> OnReceiveRemote(server, message);
public override void OnReceiveRemote(bool server, PlayerMessage message)
{
if (message.AboutId == LocalPlayerId)
{
RespawnManager.Instance.Respawn();
ClientStateManager.Instance.OnRespawn();
}
RespawnManager.Instance.OnPlayerRespawn(QSBPlayerManager.GetPlayer(message.AboutId));
}
}
}

View File

@ -0,0 +1,41 @@
using QSB.ClientServerStateSync;
using QSB.Messaging;
using QSB.Player;
using QuantumUNET.Transport;
namespace QSB.RespawnSync.Events
{
internal class PlayerRespawnMessage : QSBMessage
{
private uint PlayerId;
public PlayerRespawnMessage(uint playerId) => PlayerId = playerId;
public PlayerRespawnMessage() { }
public override void Serialize(QNetworkWriter writer)
{
base.Serialize(writer);
writer.Write(PlayerId);
}
public override void Deserialize(QNetworkReader reader)
{
base.Deserialize(reader);
PlayerId = reader.ReadUInt32();
}
public override void OnReceiveLocal() => OnReceiveRemote();
public override void OnReceiveRemote()
{
if (PlayerId == QSBPlayerManager.LocalPlayerId)
{
RespawnManager.Instance.Respawn();
ClientStateManager.Instance.OnRespawn();
}
RespawnManager.Instance.OnPlayerRespawn(QSBPlayerManager.GetPlayer(PlayerId));
}
}
}

View File

@ -6,6 +6,8 @@ using QSB.Player.TransformSync;
using QSB.Utility;
using System.Collections.Generic;
using System.Linq;
using QSB.Messaging;
using QSB.RespawnSync.Events;
using UnityEngine;
using UnityEngine.Networking;
@ -174,7 +176,7 @@ namespace QSB.RespawnSync
public void RespawnSomePlayer()
{
var playerToRespawn = _playersPendingRespawn.First();
QSBEventManager.FireEvent(EventNames.QSBPlayerRespawn, playerToRespawn.PlayerId);
new PlayerRespawnMessage(playerToRespawn.PlayerId).Send();
}
private void UpdateRespawnNotification()