supports multiple elevators (if they have unique names) (#160)

This commit is contained in:
AmazingAlek 2020-08-12 22:22:52 +02:00 committed by GitHub
parent 904b40504b
commit a44e563001
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 52 additions and 27 deletions

View File

@ -5,7 +5,7 @@ namespace QSB.ElevatorSync
{ {
public class ElevatorController : MonoBehaviour public class ElevatorController : MonoBehaviour
{ {
public static ElevatorController Instance { get; private set; } public string ElevatorName => _elevator.name;
private Elevator _elevator; private Elevator _elevator;
private Vector3 _startLocalPos; private Vector3 _startLocalPos;
@ -15,20 +15,9 @@ namespace QSB.ElevatorSync
private OWAudioSource _owAudioSourceOneShot; private OWAudioSource _owAudioSourceOneShot;
private OWAudioSource _owAudioSourceLP; private OWAudioSource _owAudioSourceLP;
private void Awake() public void Init(Elevator elevator)
{
Instance = this;
QSB.Helper.Events.Subscribe<Elevator>(OWML.Common.Events.AfterAwake);
QSB.Helper.Events.Event += OnEvent;
QSB.Helper.HarmonyHelper.AddPostfix<Elevator>("StartLift", typeof(ElevatorPatches), nameof(ElevatorPatches.StartLift));
}
private void OnEvent(MonoBehaviour behaviour, OWML.Common.Events ev)
{
if (behaviour is Elevator elevator && ev == OWML.Common.Events.AfterAwake)
{ {
PlayerRegistry.ElevatorControllers.Add(this);
_elevator = elevator; _elevator = elevator;
_startLocalPos = _elevator.GetValue<Vector3>("_startLocalPos"); _startLocalPos = _elevator.GetValue<Vector3>("_startLocalPos");
_endLocalPos = _elevator.GetValue<Vector3>("_endLocalPos"); _endLocalPos = _elevator.GetValue<Vector3>("_endLocalPos");
@ -36,7 +25,6 @@ namespace QSB.ElevatorSync
_owAudioSourceOneShot = _elevator.GetValue<OWAudioSource>("_owAudioSourceOneShot"); _owAudioSourceOneShot = _elevator.GetValue<OWAudioSource>("_owAudioSourceOneShot");
_owAudioSourceLP = _elevator.GetValue<OWAudioSource>("_owAudioSourceLP"); _owAudioSourceLP = _elevator.GetValue<OWAudioSource>("_owAudioSourceLP");
} }
}
public void RemoteCall(ElevatorDirection direction) public void RemoteCall(ElevatorDirection direction)
{ {

View File

@ -9,13 +9,14 @@ namespace QSB.ElevatorSync
public override void SetupListener() public override void SetupListener()
{ {
GlobalMessenger<ElevatorDirection>.AddListener(EventNames.QSBStartLift, direction => SendEvent(CreateMessage(direction))); GlobalMessenger<ElevatorDirection, string>.AddListener(EventNames.QSBStartLift, (direction, elevatorName) => SendEvent(CreateMessage(direction, elevatorName)));
} }
private ElevatorMessage CreateMessage(ElevatorDirection direction) => new ElevatorMessage private ElevatorMessage CreateMessage(ElevatorDirection direction, string elevatorName) => new ElevatorMessage
{ {
SenderId = PlayerRegistry.LocalPlayer.NetId, SenderId = PlayerRegistry.LocalPlayer.NetId,
Direction = direction Direction = direction,
ElevatorName = elevatorName
}; };
public override void OnReceiveRemote(ElevatorMessage message) public override void OnReceiveRemote(ElevatorMessage message)
@ -24,7 +25,7 @@ namespace QSB.ElevatorSync
{ {
return; return;
} }
ElevatorController.Instance.RemoteCall(message.Direction); PlayerRegistry.GetElevatorController(message.ElevatorName).RemoteCall(message.Direction);
} }
} }
} }

View File

@ -0,0 +1,24 @@
using UnityEngine;
namespace QSB.ElevatorSync
{
public class ElevatorManager : MonoBehaviour
{
private void Awake()
{
QSB.Helper.Events.Subscribe<Elevator>(OWML.Common.Events.AfterAwake);
QSB.Helper.Events.Event += OnEvent;
QSB.Helper.HarmonyHelper.AddPostfix<Elevator>("StartLift", typeof(ElevatorPatches), nameof(ElevatorPatches.StartLift));
}
private void OnEvent(MonoBehaviour behaviour, OWML.Common.Events ev)
{
if (behaviour is Elevator elevator && ev == OWML.Common.Events.AfterAwake)
{
var elevatorController = gameObject.AddComponent<ElevatorController>();
elevatorController.Init(elevator);
}
}
}
}

View File

@ -6,17 +6,20 @@ namespace QSB.ElevatorSync
public class ElevatorMessage : PlayerMessage public class ElevatorMessage : PlayerMessage
{ {
public ElevatorDirection Direction { get; set; } public ElevatorDirection Direction { get; set; }
public string ElevatorName { get; set; }
public override void Deserialize(NetworkReader reader) public override void Deserialize(NetworkReader reader)
{ {
base.Deserialize(reader); base.Deserialize(reader);
Direction = (ElevatorDirection)reader.ReadInt32(); Direction = (ElevatorDirection)reader.ReadInt32();
ElevatorName = reader.ReadString();
} }
public override void Serialize(NetworkWriter writer) public override void Serialize(NetworkWriter writer)
{ {
base.Serialize(writer); base.Serialize(writer);
writer.Write((int)Direction); writer.Write((int)Direction);
writer.Write(ElevatorName);
} }
} }
} }

View File

@ -9,7 +9,7 @@ namespace QSB.ElevatorSync
{ {
var isGoingUp = __instance.GetValue<bool>("_goingToTheEnd"); var isGoingUp = __instance.GetValue<bool>("_goingToTheEnd");
var direction = isGoingUp ? ElevatorDirection.Up : ElevatorDirection.Down; var direction = isGoingUp ? ElevatorDirection.Up : ElevatorDirection.Down;
GlobalMessenger<ElevatorDirection>.FireEvent(EventNames.QSBStartLift, direction); GlobalMessenger<ElevatorDirection, string>.FireEvent(EventNames.QSBStartLift, direction, __instance.name);
} }
} }
} }

View File

@ -2,6 +2,7 @@
using System.Linq; using System.Linq;
using QSB.TransformSync; using QSB.TransformSync;
using QSB.Animation; using QSB.Animation;
using QSB.ElevatorSync;
using QSB.Messaging; using QSB.Messaging;
namespace QSB namespace QSB
@ -15,6 +16,8 @@ namespace QSB
public static List<TransformSync.TransformSync> LocalTransformSyncs => TransformSyncs.Where(t => t != null && t.hasAuthority).ToList(); public static List<TransformSync.TransformSync> LocalTransformSyncs => TransformSyncs.Where(t => t != null && t.hasAuthority).ToList();
public static List<AnimationSync> AnimationSyncs { get; } = new List<AnimationSync>(); public static List<AnimationSync> AnimationSyncs { get; } = new List<AnimationSync>();
public static List<ElevatorController> ElevatorControllers { get; } = new List<ElevatorController>();
public static PlayerInfo CreatePlayer(uint id) public static PlayerInfo CreatePlayer(uint id)
{ {
if (PlayerExists(id)) if (PlayerExists(id))
@ -64,5 +67,10 @@ namespace QSB
return AnimationSyncs.FirstOrDefault(x => x != null && x.netId.Value == id); return AnimationSyncs.FirstOrDefault(x => x != null && x.netId.Value == id);
} }
public static ElevatorController GetElevatorController(string name)
{
return ElevatorControllers.FirstOrDefault(x => x != null && x.ElevatorName == name);
}
} }
} }

View File

@ -29,7 +29,7 @@ namespace QSB
gameObject.AddComponent<NetworkManagerHUD>(); gameObject.AddComponent<NetworkManagerHUD>();
gameObject.AddComponent<DebugActions>(); gameObject.AddComponent<DebugActions>();
gameObject.AddComponent<UnityHelper>(); gameObject.AddComponent<UnityHelper>();
gameObject.AddComponent<ElevatorController>(); gameObject.AddComponent<ElevatorManager>();
GlobalMessenger.AddListener(EventNames.RestartTimeLoop, OnLoopStart); GlobalMessenger.AddListener(EventNames.RestartTimeLoop, OnLoopStart);
GlobalMessenger.AddListener(EventNames.WakeUp, OnWakeUp); GlobalMessenger.AddListener(EventNames.WakeUp, OnWakeUp);

View File

@ -120,6 +120,7 @@
<Compile Include="ElevatorSync\ElevatorDirection.cs" /> <Compile Include="ElevatorSync\ElevatorDirection.cs" />
<Compile Include="ElevatorSync\ElevatorController.cs" /> <Compile Include="ElevatorSync\ElevatorController.cs" />
<Compile Include="ElevatorSync\ElevatorEvent.cs" /> <Compile Include="ElevatorSync\ElevatorEvent.cs" />
<Compile Include="ElevatorSync\ElevatorManager.cs" />
<Compile Include="ElevatorSync\ElevatorMessage.cs" /> <Compile Include="ElevatorSync\ElevatorMessage.cs" />
<Compile Include="ElevatorSync\ElevatorPatches.cs" /> <Compile Include="ElevatorSync\ElevatorPatches.cs" />
<Compile Include="Events\EventNames.cs" /> <Compile Include="Events\EventNames.cs" />