quantum-space-buddies/QSB/QuantumSync/Messages/QuantumAuthorityMessage.cs

79 lines
1.8 KiB
C#
Raw Normal View History

2021-12-22 14:46:37 -08:00
using QSB.Messaging;
using QSB.Player;
using QSB.QuantumSync.WorldObjects;
2021-01-26 14:07:17 +00:00
using QuantumUNET.Transport;
namespace QSB.QuantumSync.Messages
2021-01-26 14:07:17 +00:00
{
2021-12-22 14:46:37 -08:00
public class QuantumAuthorityMessage : QSBWorldObjectMessage<IQSBQuantumObject>
2021-01-26 14:07:17 +00:00
{
2022-01-02 21:10:12 -08:00
private uint ControllingPlayer;
/// <summary>
/// if true, force sets controlling player,
/// without checking current controlling player
/// or checking for other potential controllers
/// </summary>
private bool Force;
2021-01-26 14:07:17 +00:00
2022-01-02 21:10:12 -08:00
public QuantumAuthorityMessage(uint controllingPlayer, bool force)
{
ControllingPlayer = controllingPlayer;
Force = force;
}
2021-01-26 14:07:17 +00:00
public override void Serialize(QNetworkWriter writer)
{
base.Serialize(writer);
2022-01-02 21:10:12 -08:00
writer.Write(ControllingPlayer);
writer.Write(Force);
2021-01-26 14:07:17 +00:00
}
2021-12-22 14:46:37 -08:00
public override void Deserialize(QNetworkReader reader)
{
base.Deserialize(reader);
2022-01-02 21:10:12 -08:00
ControllingPlayer = reader.ReadUInt32();
Force = reader.ReadBoolean();
}
2021-12-22 14:46:37 -08:00
public override bool ShouldReceive
{
get
{
if (!base.ShouldReceive)
{
return false;
}
2022-01-02 21:10:12 -08:00
if (WorldObject.ControllingPlayer == ControllingPlayer)
{
return false;
}
if (Force)
{
return true;
}
if (ControllingPlayer == uint.MaxValue)
{
return true;
}
2021-12-22 14:46:37 -08:00
2022-01-02 21:10:12 -08:00
return WorldObject.ControllingPlayer == uint.MaxValue;
2021-12-22 14:46:37 -08:00
}
}
2022-01-02 21:10:12 -08:00
public override void OnReceiveLocal() => WorldObject.ControllingPlayer = ControllingPlayer;
2021-12-22 14:46:37 -08:00
public override void OnReceiveRemote()
{
2022-01-02 21:10:12 -08:00
WorldObject.ControllingPlayer = ControllingPlayer;
if (!Force && ControllingPlayer == uint.MaxValue && WorldObject.IsEnabled)
2021-12-22 14:46:37 -08:00
{
// object has no owner, but is still active for this player. request ownership
2022-01-02 21:10:12 -08:00
WorldObject.SendMessage(new QuantumAuthorityMessage(QSBPlayerManager.LocalPlayerId, false));
2021-12-22 14:46:37 -08:00
}
}
2021-01-26 14:07:17 +00:00
}
2021-12-22 14:46:37 -08:00
}