make it have owner

This commit is contained in:
JohnCorby 2022-08-15 23:04:14 -07:00
parent fcfac90a2a
commit 6b33dc6296
3 changed files with 11 additions and 53 deletions

View File

@ -7,6 +7,9 @@ namespace QSB.AuthoritySync;
/// </summary>
public interface IAuthWorldObject : IWorldObject
{
/// <summary>
/// 0 = owned by no one
/// </summary>
public uint Owner { get; set; }
/// <summary>
/// can the world object have authority

View File

@ -1,26 +0,0 @@
using QSB.EchoesOfTheEye.LightSensorSync.WorldObjects;
using QSB.Messaging;
using System.Linq;
namespace QSB.EchoesOfTheEye.LightSensorSync.Messages;
/// <summary>
/// always sent by host
/// </summary>
internal class IlluminatedByMessage : QSBWorldObjectMessage<QSBLightSensor, uint[]>
{
public IlluminatedByMessage(uint[] illuminatedBy) : base(illuminatedBy) { }
public override void OnReceiveRemote()
{
foreach (var added in Data.Except(WorldObject._illuminatedBy).ToList())
{
WorldObject.SetIlluminated(added, true);
}
foreach (var removed in WorldObject._illuminatedBy.Except(Data).ToList())
{
WorldObject.SetIlluminated(removed, false);
}
}
}

View File

@ -1,4 +1,5 @@
using Cysharp.Threading.Tasks;
using QSB.AuthoritySync;
using QSB.EchoesOfTheEye.LightSensorSync.Messages;
using QSB.Messaging;
using QSB.Player;
@ -24,21 +25,20 @@ namespace QSB.EchoesOfTheEye.LightSensorSync.WorldObjects;
/// the sector it's enabled in is bigger than the sector the zone2 walls are enabled in :(
/// maybe this can be fixed by making the collision group use the same sector.
/// </summary>
internal class QSBLightSensor : WorldObject<SingleLightSensor>
internal class QSBLightSensor : WorldObject<SingleLightSensor>, IAuthWorldObject
{
internal bool _locallyIlluminated;
public Action OnDetectLocalLight;
public Action OnDetectLocalDarkness;
internal readonly List<uint> _illuminatedBy = new();
public uint Owner { get; set; }
public bool CanOwn => AttachedObject.enabled;
public override void SendInitialState(uint to)
{
this.SendMessage(new IlluminatedByMessage(_illuminatedBy.ToArray()) { To = to });
if (AttachedObject._illuminatingDreamLanternList != null)
{
this.SendMessage(new IlluminatingLanternsMessage(AttachedObject._illuminatingDreamLanternList) { To = to });
}
// todo initial state
}
public override async UniTask Init(CancellationToken ct)
@ -64,25 +64,6 @@ internal class QSBLightSensor : WorldObject<SingleLightSensor>
public void SetIlluminated(uint playerId, bool locallyIlluminated)
{
var illuminated = _illuminatedBy.Count > 0;
if (locallyIlluminated)
{
_illuminatedBy.SafeAdd(playerId);
}
else
{
_illuminatedBy.QuickRemove(playerId);
}
if (!illuminated && _illuminatedBy.Count > 0)
{
AttachedObject._illuminated = true;
AttachedObject.OnDetectLight.Invoke();
}
else if (illuminated && _illuminatedBy.Count == 0)
{
AttachedObject._illuminated = false;
AttachedObject.OnDetectDarkness.Invoke();
}
// todo remove
}
}