slide projector: make user stuff work

This commit is contained in:
JohnCorby 2022-03-02 20:47:32 -08:00
parent e4da50a656
commit e54892367f
4 changed files with 37 additions and 80 deletions

View File

@ -1,54 +0,0 @@
using Mirror;
using QSB.EchoesOfTheEye.SlideProjectors.WorldObjects;
using QSB.Messaging;
namespace QSB.EchoesOfTheEye.SlideProjectors.Messages;
public class ProjectorAuthorityMessage : QSBWorldObjectMessage<QSBSlideProjector>
{
private uint AuthorityOwner;
public ProjectorAuthorityMessage(uint authorityOwner) => AuthorityOwner = authorityOwner;
public override void Serialize(NetworkWriter writer)
{
base.Serialize(writer);
writer.Write(AuthorityOwner);
}
public override void Deserialize(NetworkReader reader)
{
base.Deserialize(reader);
AuthorityOwner = reader.Read<uint>();
}
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.ControllingPlayer == 0 || AuthorityOwner == 0)
&& WorldObject.ControllingPlayer != AuthorityOwner;
}
}
public override void OnReceiveLocal() => OnReceiveRemote();
public override void OnReceiveRemote()
{
WorldObject.ControllingPlayer = AuthorityOwner;
WorldObject.OnChangeAuthority(AuthorityOwner);
}
}

View File

@ -0,0 +1,13 @@
using QSB.EchoesOfTheEye.SlideProjectors.WorldObjects;
using QSB.Messaging;
using QSB.Player;
namespace QSB.EchoesOfTheEye.SlideProjectors.Messages;
public class UseSlideProjectorMessage : QSBWorldObjectMessage<QSBSlideProjector, uint>
{
public UseSlideProjectorMessage(bool @using) => Data = @using ? QSBPlayerManager.LocalPlayerId : 0;
public UseSlideProjectorMessage(uint user) => Data = user;
public override void OnReceiveLocal() => OnReceiveRemote();
public override void OnReceiveRemote() => WorldObject.SetUser(Data);
}

View File

@ -3,36 +3,36 @@ using QSB.EchoesOfTheEye.SlideProjectors.Messages;
using QSB.EchoesOfTheEye.SlideProjectors.WorldObjects;
using QSB.Messaging;
using QSB.Patches;
using QSB.Player;
using QSB.WorldSync;
namespace QSB.EchoesOfTheEye.SlideProjectors.Patches;
internal class ProjectorPatches : QSBPatch
[HarmonyPatch(typeof(SlideProjector))]
internal class SlideProjectorPatches : QSBPatch
{
public override QSBPatchTypes Type => QSBPatchTypes.OnClientConnect;
[HarmonyPostfix]
[HarmonyPatch(typeof(SlideProjector), nameof(SlideProjector.OnPressInteract))]
public static void Interact(SlideProjector __instance) =>
[HarmonyPatch(nameof(SlideProjector.OnPressInteract))]
public static void OnPressInteract(SlideProjector __instance) =>
__instance.GetWorldObject<QSBSlideProjector>()
.SendMessage(new ProjectorAuthorityMessage(QSBPlayerManager.LocalPlayerId));
.SendMessage(new UseSlideProjectorMessage(true));
[HarmonyPostfix]
[HarmonyPatch(typeof(SlideProjector), nameof(SlideProjector.CancelInteraction))]
public static void CancelInteract(SlideProjector __instance) =>
[HarmonyPatch(nameof(SlideProjector.CancelInteraction))]
public static void CancelInteraction(SlideProjector __instance) =>
__instance.GetWorldObject<QSBSlideProjector>()
.SendMessage(new ProjectorAuthorityMessage(0));
.SendMessage(new UseSlideProjectorMessage(false));
[HarmonyPostfix]
[HarmonyPatch(typeof(SlideProjector), nameof(SlideProjector.NextSlide))]
[HarmonyPatch(nameof(SlideProjector.NextSlide))]
public static void NextSlide(SlideProjector __instance) =>
__instance.GetWorldObject<QSBSlideProjector>()
.SendMessage(new NextSlideMessage());
[HarmonyPostfix]
[HarmonyPatch(typeof(SlideProjector), nameof(SlideProjector.PreviousSlide))]
[HarmonyPatch(nameof(SlideProjector.PreviousSlide))]
public static void PreviousSlide(SlideProjector __instance) =>
__instance.GetWorldObject<QSBSlideProjector>()
.SendMessage(new PreviousSlideMessage());
}
}

View File

@ -1,27 +1,25 @@
using Cysharp.Threading.Tasks;
using QSB.EchoesOfTheEye.SlideProjectors.Messages;
using QSB.Messaging;
using QSB.Utility;
using QSB.WorldSync;
using System.Threading;
namespace QSB.EchoesOfTheEye.SlideProjectors.WorldObjects;
public class QSBSlideProjector : WorldObject<SlideProjector>
{
public uint ControllingPlayer;
private uint _user;
public override async UniTask Init(CancellationToken ct)
{
DebugLog.DebugWrite($"Init {this}");
}
public override void SendInitialState(uint to) =>
new UseSlideProjectorMessage(_user) { To = to }.Send();
public override void SendInitialState(uint to)
/// <summary>
/// called both locally and remotely
/// </summary>
public void SetUser(uint user)
{
// todo SendInitialState
}
public void OnChangeAuthority(uint newOwner)
{
DebugLog.DebugWrite($"{this} change ControllingPlayer to {newOwner}");
DebugLog.DebugWrite($"{this} - user = {user}");
AttachedObject._interactReceiver.SetInteractionEnabled(user == 0 || user == _user);
_user = user;
}
public void NextSlide()
@ -79,4 +77,4 @@ public class QSBSlideProjector : WorldObject<SlideProjector>
AttachedObject._gearInterface.AddRotation(-45f, audioVolume);
}
}
}
}