quantum-space-buddies/QSB/TriggerSync/WorldObjects/QSBTrigger.cs

106 lines
2.3 KiB
C#
Raw Normal View History

2022-01-29 04:49:07 +00:00
using Cysharp.Threading.Tasks;
using QSB.Messaging;
2022-01-13 02:43:18 +00:00
using QSB.Player;
2022-01-13 03:18:59 +00:00
using QSB.TriggerSync.Messages;
2022-01-29 09:24:15 +00:00
using QSB.Utility;
2022-01-13 02:43:18 +00:00
using QSB.WorldSync;
using System.Collections.Generic;
2022-01-13 05:28:35 +00:00
using System.Linq;
2022-01-29 04:49:07 +00:00
using System.Threading;
2022-01-13 02:43:18 +00:00
using UnityEngine;
2022-03-03 03:46:33 +00:00
namespace QSB.TriggerSync.WorldObjects;
public interface IQSBTrigger : IWorldObject
2022-01-13 02:43:18 +00:00
{
2022-03-03 03:46:33 +00:00
List<PlayerInfo> Occupants { get; }
2022-01-13 03:12:37 +00:00
2022-03-03 03:46:33 +00:00
void Enter(PlayerInfo player);
2022-01-13 03:12:37 +00:00
2022-03-03 03:46:33 +00:00
void Exit(PlayerInfo player);
}
2022-01-13 03:12:37 +00:00
2022-03-03 03:46:33 +00:00
public abstract class QSBTrigger<TO> : WorldObject<OWTriggerVolume>, IQSBTrigger
{
public TO TriggerOwner { get; init; }
2022-01-13 03:12:37 +00:00
2022-03-03 03:46:33 +00:00
public List<PlayerInfo> Occupants { get; } = new();
2022-01-13 02:43:18 +00:00
2022-03-03 03:46:33 +00:00
protected virtual string CompareTag => "PlayerDetector";
2022-01-13 05:28:35 +00:00
2022-03-03 03:46:33 +00:00
public override async UniTask Init(CancellationToken ct)
{
AttachedObject.OnEntry += OnEnterEvent;
AttachedObject.OnExit += OnExitEvent;
2022-01-13 02:43:18 +00:00
2022-03-03 03:46:33 +00:00
QSBPlayerManager.OnRemovePlayer += OnPlayerLeave;
2022-01-13 02:43:18 +00:00
2022-03-03 03:46:33 +00:00
Delay.RunWhen(() => QSBWorldSync.AllObjectsReady, () =>
{
if (AttachedObject._trackedObjects != null && AttachedObject._trackedObjects.Any(x => x.CompareTag(CompareTag)))
2022-01-13 02:43:18 +00:00
{
2022-03-03 03:46:33 +00:00
((IQSBTrigger)this).SendMessage(new TriggerMessage(true));
}
});
}
2022-01-13 02:43:18 +00:00
2022-03-03 03:46:33 +00:00
public override void OnRemoval()
{
AttachedObject.OnEntry -= OnEnterEvent;
AttachedObject.OnExit -= OnExitEvent;
2022-01-13 02:43:18 +00:00
2022-03-03 03:46:33 +00:00
QSBPlayerManager.OnRemovePlayer -= OnPlayerLeave;
}
2022-01-13 02:43:18 +00:00
private void OnPlayerLeave(PlayerInfo player) => Exit(player);
2022-03-03 03:46:33 +00:00
public override void SendInitialState(uint to) =>
((IQSBTrigger)this).SendMessage(new TriggerInitialStateMessage(Occupants) { To = to });
2022-01-21 23:13:16 +00:00
2022-03-03 03:46:33 +00:00
protected void OnEnterEvent(GameObject hitObj)
{
if (hitObj.CompareTag(CompareTag))
2022-01-13 02:43:18 +00:00
{
2022-03-03 03:46:33 +00:00
((IQSBTrigger)this).SendMessage(new TriggerMessage(true));
2022-01-13 02:43:18 +00:00
}
2022-03-03 03:46:33 +00:00
}
2022-01-13 02:43:18 +00:00
2022-03-03 03:46:33 +00:00
protected void OnExitEvent(GameObject hitObj)
{
if (hitObj.CompareTag(CompareTag))
2022-01-13 02:43:18 +00:00
{
2022-03-03 03:46:33 +00:00
((IQSBTrigger)this).SendMessage(new TriggerMessage(false));
2022-01-13 02:43:18 +00:00
}
2022-03-03 03:46:33 +00:00
}
2022-01-13 02:43:18 +00:00
2022-03-03 03:46:33 +00:00
public void Enter(PlayerInfo player)
{
if (!Occupants.SafeAdd(player))
2022-01-13 02:43:18 +00:00
{
2022-03-03 03:46:33 +00:00
return;
}
2022-01-13 02:43:18 +00:00
2022-03-03 03:46:33 +00:00
OnEnter(player);
}
2022-01-13 03:43:48 +00:00
2022-03-03 03:46:33 +00:00
public void Exit(PlayerInfo player)
{
if (!Occupants.QuickRemove(player))
{
return;
}
2022-03-03 03:46:33 +00:00
OnExit(player);
}
2022-03-03 03:46:33 +00:00
/// <summary>
/// called when a player enters this trigger
/// </summary>
protected virtual void OnEnter(PlayerInfo player) { }
/// <summary>
/// called when a player exits this trigger or leaves the game
/// </summary>
protected virtual void OnExit(PlayerInfo player) { }
}