quantum-space-buddies/QSB/AuthoritySync/AuthWorldObject.cs

40 lines
1009 B
C#
Raw 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;
namespace QSB.AuthoritySync;
/// <summary>
/// helper implementation of the interface
/// </summary>
public abstract class AuthWorldObject<T> : WorldObject<T>, IAuthWorldObject
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) =>
2022-08-16 06:38:51 +00:00
((IAuthWorldObject)this).SendMessage(new AuthWorldObjectMessage(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)
{
2022-08-16 17:53:11 +00:00
((IAuthWorldObject)this).SendMessage(new AuthWorldObjectMessage(CanOwn ? QSBPlayerManager.LocalPlayerId : 0));
2022-08-16 06:20:46 +00:00
}
2022-08-16 06:14:29 +00:00
}
}