mirror of
https://github.com/misternebula/quantum-space-buddies.git
synced 2024-12-28 00:19:27 +00:00
trigger
This commit is contained in:
parent
6514fb85ed
commit
066df2ff3f
83
QSB/TriggerSync/TriggerLink.cs
Normal file
83
QSB/TriggerSync/TriggerLink.cs
Normal 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}");
|
||||
}
|
||||
}
|
||||
}
|
32
QSB/TriggerSync/TriggerManager.cs
Normal file
32
QSB/TriggerSync/TriggerManager.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
48
QSB/TriggerSync/TriggerMessage.cs
Normal file
48
QSB/TriggerSync/TriggerMessage.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user