mirror of
https://github.com/misternebula/quantum-space-buddies.git
synced 2025-01-16 16:15:24 +00:00
world object auth
This commit is contained in:
parent
eebd36d11d
commit
fcfac90a2a
15
QSB/AuthoritySync/IAuthWorldObject.cs
Normal file
15
QSB/AuthoritySync/IAuthWorldObject.cs
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
using QSB.WorldSync;
|
||||||
|
|
||||||
|
namespace QSB.AuthoritySync;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// a world object that has an owner
|
||||||
|
/// </summary>
|
||||||
|
public interface IAuthWorldObject : IWorldObject
|
||||||
|
{
|
||||||
|
public uint Owner { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// can the world object have authority
|
||||||
|
/// </summary>
|
||||||
|
public bool CanOwn { get; }
|
||||||
|
}
|
45
QSB/AuthoritySync/WorldObjectAuthMessage.cs
Normal file
45
QSB/AuthoritySync/WorldObjectAuthMessage.cs
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
using QSB.Messaging;
|
||||||
|
using QSB.Player;
|
||||||
|
|
||||||
|
namespace QSB.AuthoritySync;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// request ownership of a world object
|
||||||
|
/// </summary>
|
||||||
|
public class WorldObjectAuthMessage : QSBWorldObjectMessage<IAuthWorldObject, uint>
|
||||||
|
{
|
||||||
|
public WorldObjectAuthMessage(uint owner) : base(owner) { }
|
||||||
|
|
||||||
|
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.Owner == 0 || Data == 0) && WorldObject.Owner != Data;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void OnReceiveLocal() => WorldObject.Owner = Data;
|
||||||
|
|
||||||
|
public override void OnReceiveRemote()
|
||||||
|
{
|
||||||
|
WorldObject.Owner = Data;
|
||||||
|
if (WorldObject.Owner == 0 && WorldObject.CanOwn)
|
||||||
|
{
|
||||||
|
// object has no owner, but is still active for this player. request ownership
|
||||||
|
WorldObject.SendMessage(new WorldObjectAuthMessage(QSBPlayerManager.LocalPlayerId));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user