111 lines
2.3 KiB
C#
Raw Normal View History

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