quantum-space-buddies/QSB/OwnershipSync/OwnedWorldObject.cs

41 lines
1.1 KiB
C#
Raw Permalink Normal View History

2022-08-16 06:20:46 +00:00
using Cysharp.Threading.Tasks;
using QSB.Messaging;
using QSB.Player;
2022-08-16 06:14:29 +00:00
using QSB.WorldSync;
2022-08-16 06:20:46 +00:00
using System.Threading;
2022-08-16 06:14:29 +00:00
using UnityEngine;
2023-05-08 18:30:59 +00:00
namespace QSB.OwnershipSync;
2022-08-16 06:14:29 +00:00
/// <summary>
/// helper implementation of the interface
/// </summary>
2023-05-08 18:30:59 +00:00
public abstract class OwnedWorldObject<T> : WorldObject<T>, IOwnedWorldObject
2022-08-16 06:14:29 +00:00
where T : MonoBehaviour
{
public uint Owner { get; set; }
public abstract bool CanOwn { get; }
2022-08-16 06:20:46 +00:00
public override void SendInitialState(uint to) =>
2023-05-08 18:30:59 +00:00
((IOwnedWorldObject)this).SendMessage(new OwnedWorldObjectMessage(Owner) { To = to });
2022-08-16 06:20:46 +00:00
public override async UniTask Init(CancellationToken ct) =>
QSBPlayerManager.OnRemovePlayer += OnPlayerLeave;
public override void OnRemoval() =>
QSBPlayerManager.OnRemovePlayer -= OnPlayerLeave;
private void OnPlayerLeave(PlayerInfo player)
{
2022-08-16 17:42:26 +00:00
if (!QSBCore.IsHost)
{
return;
}
2022-08-16 06:20:46 +00:00
if (Owner == player.PlayerId)
{
// put CanOwn check here cuz it only does the thingy in OnReceiveRemote and we want to be able to own this
2023-05-08 18:30:59 +00:00
((IOwnedWorldObject)this).SendMessage(new OwnedWorldObjectMessage(CanOwn ? QSBPlayerManager.LocalPlayerId : 0));
2022-08-16 06:20:46 +00:00
}
2022-08-16 06:14:29 +00:00
}
}