quantum-space-buddies/QSB/Player/Events/PlayerReadyEvent.cs

48 lines
1.7 KiB
C#
Raw Normal View History

2020-08-24 15:51:12 +01:00
using OWML.Common;
2020-11-03 21:42:14 +00:00
using QSB.EventsCore;
2020-08-24 15:51:12 +01:00
using QSB.Messaging;
using QSB.TransformSync;
using QSB.Utility;
using System.Linq;
2020-08-10 11:45:24 +01:00
2020-11-03 21:33:48 +00:00
namespace QSB.Player.Events
2020-08-10 11:45:24 +01:00
{
2020-08-10 18:17:54 +02:00
public class PlayerReadyEvent : QSBEvent<ToggleMessage>
2020-08-10 11:45:24 +01:00
{
2020-08-22 18:08:25 +01:00
public override EventType Type => EventType.PlayerReady;
2020-08-10 11:45:24 +01:00
2020-10-22 16:46:01 +01:00
public override void SetupListener() => GlobalMessenger<bool>.AddListener(EventNames.QSBPlayerReady, Handler);
2020-08-15 21:55:25 +02:00
2020-10-22 16:46:01 +01:00
public override void CloseListener() => GlobalMessenger<bool>.RemoveListener(EventNames.QSBPlayerReady, Handler);
2020-08-15 20:32:58 +01:00
2020-08-15 21:52:43 +02:00
private void Handler(bool ready) => SendEvent(CreateMessage(ready));
2020-08-10 18:17:54 +02:00
private ToggleMessage CreateMessage(bool ready) => new ToggleMessage
{
2020-08-17 16:51:56 +01:00
AboutId = LocalPlayerId,
2020-08-10 18:17:54 +02:00
ToggleValue = ready
};
2020-08-10 11:45:24 +01:00
public override void OnServerReceive(ToggleMessage message)
{
2020-08-24 15:51:12 +01:00
DebugLog.DebugWrite($"[S] Get ready event from {message.FromId}", MessageType.Success);
2020-11-03 21:18:40 +00:00
if (message.FromId == QSBPlayerManager.LocalPlayerId)
2020-08-24 15:51:12 +01:00
{
return;
}
2020-11-03 21:18:40 +00:00
QSBPlayerManager.GetPlayer(message.AboutId).IsReady = message.ToggleValue;
2020-11-03 21:33:48 +00:00
PlayerStateEvent.LocalInstance.Send();
2020-08-10 11:45:24 +01:00
}
2020-08-24 15:51:12 +01:00
public override void OnReceiveRemote(ToggleMessage message)
{
DebugLog.DebugWrite($"Get ready event from {message.FromId}", MessageType.Success);
2020-11-03 21:18:40 +00:00
foreach (var item in QSBPlayerManager.GetSyncObjects<TransformSync.TransformSync>()
2020-09-04 20:52:06 +01:00
.Where(x => x != null && x.IsReady && x.ReferenceSector != null && x.PlayerId == LocalPlayerId))
2020-08-24 15:51:12 +01:00
{
2020-09-04 20:09:25 +01:00
GlobalMessenger<uint, QSBSector>.FireEvent(EventNames.QSBSectorChange, item.netId.Value, item.ReferenceSector);
2020-08-24 15:51:12 +01:00
}
}
2020-08-10 11:45:24 +01:00
}
}