quantum-space-buddies/QSB/TriggerSync/Messages/TriggerInitialStateMessage.cs
2022-01-27 00:32:30 -08:00

32 lines
803 B
C#

using QSB.Messaging;
using QSB.Player;
using QSB.TriggerSync.WorldObjects;
using System.Collections.Generic;
using System.Linq;
namespace QSB.TriggerSync.Messages
{
/// <summary>
/// always sent by host
/// </summary>
public class TriggerInitialStateMessage : QSBWorldObjectMessage<IQSBTrigger, uint[]>
{
public TriggerInitialStateMessage(IEnumerable<PlayerInfo> occupants) =>
Value = occupants.Select(x => x.PlayerId).ToArray();
public override void OnReceiveRemote()
{
var serverOccupants = Value.Select(QSBPlayerManager.GetPlayer).ToList();
foreach (var added in serverOccupants.Except(WorldObject.Occupants))
{
WorldObject.Enter(added);
}
foreach (var removed in WorldObject.Occupants.Except(serverOccupants))
{
WorldObject.Exit(removed);
}
}
}
}