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

61 lines
1.4 KiB
C#
Raw Normal View History

using Mirror;
using QSB.Messaging;
2021-12-22 22:46:37 +00:00
using QSB.Player;
using QSB.QuantumSync.WorldObjects;
2021-01-26 14:07:17 +00:00
namespace QSB.QuantumSync.Messages
2021-01-26 14:07:17 +00:00
{
2021-12-22 22:46:37 +00:00
public class QuantumAuthorityMessage : QSBWorldObjectMessage<IQSBQuantumObject>
2021-01-26 14:07:17 +00:00
{
private uint AuthorityOwner;
2021-01-26 14:07:17 +00:00
public QuantumAuthorityMessage(uint authorityOwner) => AuthorityOwner = authorityOwner;
public override void Serialize(NetworkWriter writer)
2021-01-26 14:07:17 +00:00
{
base.Serialize(writer);
writer.Write(AuthorityOwner);
}
2021-12-22 22:46:37 +00:00
public override void Deserialize(NetworkReader reader)
{
base.Deserialize(reader);
AuthorityOwner = reader.Read<uint>();
}
2021-12-22 22:46:37 +00:00
public override bool ShouldReceive
{
get
{
if (!base.ShouldReceive)
{
return false;
}
// Deciding if to change the object's owner
// Message
// | = 0 | > 0 |
// = 0 | No | Yes |
// > 0 | Yes | No |
// if Obj==Message then No
// Obj
return (WorldObject.ControllingPlayer == 0 || AuthorityOwner == 0)
&& WorldObject.ControllingPlayer != AuthorityOwner;
}
}
public override void OnReceiveLocal() => WorldObject.ControllingPlayer = AuthorityOwner;
public override void OnReceiveRemote()
{
WorldObject.ControllingPlayer = AuthorityOwner;
if (WorldObject.ControllingPlayer == 00 && WorldObject.IsEnabled)
{
// object has no owner, but is still active for this player. request ownership
2021-12-23 23:44:04 +00:00
WorldObject.SendMessage(new QuantumAuthorityMessage(QSBPlayerManager.LocalPlayerId));
2021-12-22 22:46:37 +00:00
}
}
2021-01-26 14:07:17 +00:00
}
2021-12-22 22:46:37 +00:00
}