LightSensorAuthorityMessage

This commit is contained in:
JohnCorby 2022-05-27 13:42:39 -07:00
parent c29f94f122
commit 7ed6fed50c
2 changed files with 43 additions and 2 deletions

View File

@ -0,0 +1,36 @@
using QSB.EchoesOfTheEye.LightSensorSync.WorldObjects;
using QSB.Messaging;
using QSB.Player;
namespace QSB.EchoesOfTheEye.LightSensorSync.Messages;
internal class LightSensorAuthorityMessage : QSBWorldObjectMessage<QSBLightSensor, uint>
{
public LightSensorAuthorityMessage(uint authorityOwner) : base(authorityOwner) { }
public override bool ShouldReceive
{
get
{
if (!base.ShouldReceive)
{
return false;
}
return (WorldObject.AuthorityOwner == 0 || Data == 0)
&& WorldObject.AuthorityOwner != Data;
}
}
public override void OnReceiveLocal() => WorldObject.AuthorityOwner = Data;
public override void OnReceiveRemote()
{
WorldObject.AuthorityOwner = Data;
if (WorldObject.AuthorityOwner == 0 && WorldObject.AttachedObject.enabled)
{
// object has no owner, but is still active for this player. request ownership
WorldObject.SendMessage(new LightSensorAuthorityMessage(QSBPlayerManager.LocalPlayerId));
}
}
}

View File

@ -1,14 +1,19 @@
using QSB.WorldSync;
using QSB.EchoesOfTheEye.LightSensorSync.Messages;
using QSB.Messaging;
using QSB.WorldSync;
using System;
namespace QSB.EchoesOfTheEye.LightSensorSync.WorldObjects;
internal class QSBLightSensor : WorldObject<SingleLightSensor>
{
public uint AuthorityOwner;
public bool LocallyIlluminated;
public Action OnDetectLocalLight;
public Action OnDetectLocalDarkness;
public override void SendInitialState(uint to) { }
public override void SendInitialState(uint to) =>
this.SendMessage(new LightSensorAuthorityMessage(AuthorityOwner) { To = to });
}