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

44 lines
1.1 KiB
C#
Raw Normal View History

2022-01-27 05:00:12 +00:00
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
2022-03-03 03:46:33 +00:00
namespace QSB.QuantumSync.Messages;
public class QuantumAuthorityMessage : QSBWorldObjectMessage<IQSBQuantumObject, uint>
2021-01-26 14:07:17 +00:00
{
2022-03-11 01:57:50 +00:00
public QuantumAuthorityMessage(uint authorityOwner) : base(authorityOwner) { }
2022-03-03 03:46:33 +00:00
public override bool ShouldReceive
{
get
2021-12-22 22:46:37 +00:00
{
2022-03-03 03:46:33 +00:00
if (!base.ShouldReceive)
2021-12-22 22:46:37 +00:00
{
2022-03-03 03:46:33 +00:00
return false;
}
2021-12-22 22:46:37 +00:00
2022-03-03 03:46:33 +00:00
// Deciding if to change the object's owner
// Message
// | = 0 | > 0 |
// = 0 | No | Yes |
// > 0 | Yes | No |
// if Obj==Message then No
// Obj
2021-12-22 22:46:37 +00:00
2022-03-03 03:46:33 +00:00
return (WorldObject.ControllingPlayer == 0 || Data == 0)
&& WorldObject.ControllingPlayer != Data;
2021-12-22 22:46:37 +00:00
}
2022-03-03 03:46:33 +00:00
}
2021-12-22 22:46:37 +00:00
2022-03-03 03:46:33 +00:00
public override void OnReceiveLocal() => WorldObject.ControllingPlayer = Data;
2021-12-22 22:46:37 +00:00
2022-03-03 03:46:33 +00:00
public override void OnReceiveRemote()
{
WorldObject.ControllingPlayer = Data;
if (WorldObject.ControllingPlayer == 00 && WorldObject.IsEnabled)
2021-12-22 22:46:37 +00:00
{
2022-03-03 03:46:33 +00:00
// object has no owner, but is still active for this player. request ownership
WorldObject.SendMessage(new QuantumAuthorityMessage(QSBPlayerManager.LocalPlayerId));
2021-12-22 22:46:37 +00:00
}
2021-01-26 14:07:17 +00:00
}
}