This commit is contained in:
JohnCorby 2022-01-12 17:02:21 -08:00
parent 6514fb85ed
commit 066df2ff3f
3 changed files with 163 additions and 0 deletions

View File

@ -0,0 +1,83 @@
using OWML.Common;
using QSB.Messaging;
using QSB.Player;
using QSB.Utility;
using System;
using System.Collections.Generic;
using UnityEngine;
namespace QSB.TriggerSync
{
public class TriggerLink : IDisposable
{
public string LogName =>
$"trigger {QSBPlayerManager.LocalPlayerId}.{_id}.{(_trigger ? _trigger.name : "null")}";
public readonly List<PlayerInfo> Players = new();
private readonly int _id;
private readonly OWTriggerVolume _trigger;
public TriggerLink(int id, OWTriggerVolume trigger)
{
_id = id;
_trigger = trigger;
_trigger.OnEntry += OnEntry;
_trigger.OnExit += OnExit;
if (_trigger._trackedObjects != null && _trigger.IsTrackingObject(Locator.GetPlayerDetector()))
{
OnEntry(Locator.GetPlayerDetector());
}
DebugLog.DebugWrite($"{LogName} created");
}
public void Dispose()
{
_trigger.OnEntry -= OnEntry;
_trigger.OnExit -= OnExit;
DebugLog.DebugWrite($"{LogName} disposed");
}
private void OnEntry(GameObject hitObj)
{
if (hitObj.CompareTag("PlayerDetector"))
{
new TriggerMessage(_id, true).Send();
}
}
private void OnExit(GameObject hitObj)
{
if (hitObj.CompareTag("PlayerDetector"))
{
new TriggerMessage(_id, false).Send();
}
}
public void Enter(PlayerInfo player)
{
if (!Players.SafeAdd(player))
{
DebugLog.DebugWrite($"{LogName} already added {player.PlayerId}", MessageType.Warning);
return;
}
DebugLog.DebugWrite($"{LogName} + {player.PlayerId}");
}
public void Exit(PlayerInfo player)
{
if (!Players.QuickRemove(player))
{
DebugLog.DebugWrite($"{LogName} already removed {player.PlayerId}", MessageType.Warning);
return;
}
DebugLog.DebugWrite($"{LogName} - {player.PlayerId}");
}
}
}

View File

@ -0,0 +1,32 @@
using OWML.Common;
using QSB.Utility;
using QSB.WorldSync;
using System.Linq;
namespace QSB.TriggerSync
{
public class TriggerManager : WorldObjectManager
{
public override WorldObjectType WorldObjectType => WorldObjectType.Both;
private static TriggerLink[] _triggerLinks;
protected override void RebuildWorldObjects(OWScene scene)
{
_triggerLinks?.ForEach(x => x.Dispose());
_triggerLinks = QSBWorldSync.GetUnityObjects<OWTriggerVolume>()
.Select((x, i) => new TriggerLink(i, x))
.ToArray();
}
public static TriggerLink GetTriggerLink(int id)
{
if (!_triggerLinks.TryGet(id, out var triggerLink))
{
DebugLog.ToConsole($"no trigger link with id {id}", MessageType.Error);
}
return triggerLink;
}
}
}

View File

@ -0,0 +1,48 @@
using QSB.Messaging;
using QSB.Player;
using QSB.WorldSync;
using QuantumUNET.Transport;
namespace QSB.TriggerSync
{
public class TriggerMessage : QSBBoolMessage
{
private int _id;
public TriggerMessage(int id, bool entered)
{
_id = id;
Value = entered;
}
public override void Serialize(QNetworkWriter writer)
{
base.Serialize(writer);
writer.Write(_id);
}
public override void Deserialize(QNetworkReader reader)
{
base.Deserialize(reader);
_id = reader.ReadInt32();
}
public override bool ShouldReceive => WorldObjectManager.AllObjectsReady;
public override void OnReceiveLocal() => OnReceiveRemote();
public override void OnReceiveRemote()
{
var player = QSBPlayerManager.GetPlayer(From);
var triggerLink = TriggerManager.GetTriggerLink(_id);
if (Value)
{
triggerLink.Enter(player);
}
else
{
triggerLink.Exit(player);
}
}
}
}