quantum-space-buddies/QSB/TriggerSync/Messages/TriggerResyncMessage.cs

47 lines
1.1 KiB
C#
Raw Normal View History

2022-01-14 22:30:54 +00:00
using Mirror;
using QSB.Messaging;
2022-01-13 02:43:18 +00:00
using QSB.Player;
2022-01-13 03:18:59 +00:00
using QSB.TriggerSync.WorldObjects;
2022-01-13 02:43:18 +00:00
using System.Collections.Generic;
using System.Linq;
2022-01-13 03:18:59 +00:00
namespace QSB.TriggerSync.Messages
2022-01-13 02:43:18 +00:00
{
/// <summary>
/// always sent by host
/// </summary>
2022-01-13 03:12:37 +00:00
public class TriggerResyncMessage : QSBWorldObjectMessage<IQSBTrigger>
2022-01-13 02:43:18 +00:00
{
private uint[] _playerIds;
2022-01-13 04:58:38 +00:00
public TriggerResyncMessage(IEnumerable<PlayerInfo> occupants) =>
_playerIds = occupants.Select(x => x.PlayerId).ToArray();
2022-01-13 02:43:18 +00:00
2022-01-14 22:30:54 +00:00
public override void Serialize(NetworkWriter writer)
2022-01-13 02:43:18 +00:00
{
base.Serialize(writer);
writer.WriteArray(_playerIds);
2022-01-13 02:43:18 +00:00
}
2022-01-14 22:30:54 +00:00
public override void Deserialize(NetworkReader reader)
2022-01-13 02:43:18 +00:00
{
base.Deserialize(reader);
_playerIds = reader.ReadArray<uint>();
2022-01-13 02:43:18 +00:00
}
public override void OnReceiveRemote()
{
2022-01-13 04:58:38 +00:00
var serverOccupants = _playerIds.Select(QSBPlayerManager.GetPlayer).ToList();
foreach (var added in serverOccupants.Except(WorldObject.Occupants))
2022-01-13 02:43:18 +00:00
{
WorldObject.Enter(added);
}
2022-01-13 04:58:38 +00:00
foreach (var removed in WorldObject.Occupants.Except(serverOccupants))
2022-01-13 02:43:18 +00:00
{
WorldObject.Exit(removed);
}
}
}
}