mirror of
https://github.com/misternebula/quantum-space-buddies.git
synced 2025-01-25 06:35:45 +00:00
53 lines
2.0 KiB
C#
53 lines
2.0 KiB
C#
using OWML.Common;
|
|
using QSB.EventsCore;
|
|
using QSB.Messaging;
|
|
using QSB.SectorSync;
|
|
using QSB.Utility;
|
|
using System.Linq;
|
|
|
|
namespace QSB.Player.Events
|
|
{
|
|
public class PlayerReadyEvent : QSBEvent<ToggleMessage>
|
|
{
|
|
public override EventType Type => EventType.PlayerReady;
|
|
|
|
public override void SetupListener() => GlobalMessenger<bool>.AddListener(EventNames.QSBPlayerReady, Handler);
|
|
|
|
public override void CloseListener() => GlobalMessenger<bool>.RemoveListener(EventNames.QSBPlayerReady, Handler);
|
|
|
|
private void Handler(bool ready) => SendEvent(CreateMessage(ready));
|
|
|
|
private ToggleMessage CreateMessage(bool ready) => new ToggleMessage
|
|
{
|
|
AboutId = LocalPlayerId,
|
|
ToggleValue = ready
|
|
};
|
|
|
|
public override void OnServerReceive(ToggleMessage message)
|
|
{
|
|
DebugLog.DebugWrite($"[S] Get ready event from {message.FromId}", MessageType.Success);
|
|
if (message.FromId == QSBPlayerManager.LocalPlayerId)
|
|
{
|
|
return;
|
|
}
|
|
QSBPlayerManager.GetPlayer(message.AboutId).IsReady = message.ToggleValue;
|
|
GlobalMessenger.FireEvent(EventNames.QSBServerSendPlayerStates);
|
|
}
|
|
|
|
public override void OnReceiveRemote(ToggleMessage message)
|
|
{
|
|
DebugLog.DebugWrite($"Get ready event from {message.FromId}", MessageType.Success);
|
|
if (!QSBPlayerManager.PlayerExists(message.FromId))
|
|
{
|
|
DebugLog.ToConsole("Error - Got ready event for non-existent player! Did we not send a PlayerStatesRequestEvent? Or was it not handled?", MessageType.Error);
|
|
return;
|
|
}
|
|
foreach (var item in QSBPlayerManager.GetSyncObjects<TransformSync.TransformSync>()
|
|
.Where(x => x != null && x.IsReady && x.ReferenceSector != null && x.PlayerId == LocalPlayerId))
|
|
{
|
|
GlobalMessenger<uint, QSBSector>.FireEvent(EventNames.QSBSectorChange, item.netId.Value, item.ReferenceSector);
|
|
}
|
|
}
|
|
}
|
|
}
|