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

107 lines
2.2 KiB
C#
Raw Normal View History

2022-01-13 12:13:21 +00:00
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-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-13 02:43:18 +00:00
using UnityEngine;
2022-01-13 03:18:59 +00:00
namespace QSB.TriggerSync.WorldObjects
2022-01-13 02:43:18 +00:00
{
2022-01-13 03:12:37 +00:00
public interface IQSBTrigger : IWorldObject
2022-01-13 02:43:18 +00:00
{
2022-01-13 04:58:38 +00:00
List<PlayerInfo> Occupants { get; }
2022-01-13 03:12:37 +00:00
void Enter(PlayerInfo player);
void Exit(PlayerInfo player);
}
public abstract class QSBTrigger<TO> : WorldObject<OWTriggerVolume>, IQSBTrigger
{
public TO TriggerOwner { get; init; }
2022-01-13 04:58:38 +00:00
public List<PlayerInfo> Occupants { get; } = new();
2022-01-13 02:43:18 +00:00
2022-01-13 05:28:35 +00:00
protected virtual string CompareTag => "PlayerDetector";
2022-01-13 02:43:18 +00:00
public override void Init()
{
2022-01-13 05:28:35 +00:00
AttachedObject.OnEntry += OnEnterEvent;
AttachedObject.OnExit += OnExitEvent;
2022-01-13 02:43:18 +00:00
QSBPlayerManager.OnRemovePlayer += OnPlayerLeave;
QSBCore.UnityEvents.RunWhen(() => QSBWorldSync.AllObjectsReady, () =>
2022-01-13 02:43:18 +00:00
{
2022-01-13 12:13:21 +00:00
if (AttachedObject._trackedObjects != null && AttachedObject._trackedObjects.Any(x => x.CompareTag(CompareTag)))
2022-01-13 02:43:18 +00:00
{
2022-01-13 05:28:35 +00:00
((IQSBTrigger)this).SendMessage(new TriggerMessage(true));
2022-01-13 02:43:18 +00:00
}
});
}
public override void OnRemoval()
{
2022-01-13 05:28:35 +00:00
AttachedObject.OnEntry -= OnEnterEvent;
AttachedObject.OnExit -= OnExitEvent;
2022-01-13 02:43:18 +00:00
QSBPlayerManager.OnRemovePlayer -= OnPlayerLeave;
}
2022-01-13 05:28:35 +00:00
protected void OnEnterEvent(GameObject hitObj)
2022-01-13 02:43:18 +00:00
{
2022-01-13 05:28:35 +00:00
if (hitObj.CompareTag(CompareTag))
2022-01-13 02:43:18 +00:00
{
2022-01-13 04:10:04 +00:00
((IQSBTrigger)this).SendMessage(new TriggerMessage(true));
2022-01-13 02:43:18 +00:00
}
}
2022-01-13 05:28:35 +00:00
protected void OnExitEvent(GameObject hitObj)
2022-01-13 02:43:18 +00:00
{
2022-01-13 06:04:50 +00:00
if (hitObj.CompareTag(CompareTag))
2022-01-13 02:43:18 +00:00
{
2022-01-13 04:10:04 +00:00
((IQSBTrigger)this).SendMessage(new TriggerMessage(false));
2022-01-13 02:43:18 +00:00
}
}
2022-01-13 04:10:04 +00:00
private void OnPlayerLeave(PlayerInfo player)
2022-01-13 02:43:18 +00:00
{
2022-01-13 04:58:38 +00:00
if (Occupants.Contains(player))
2022-01-13 02:43:18 +00:00
{
2022-01-13 04:10:04 +00:00
Exit(player);
2022-01-13 02:43:18 +00:00
}
}
public void Enter(PlayerInfo player)
{
2022-01-13 04:58:38 +00:00
if (!Occupants.SafeAdd(player))
2022-01-13 02:43:18 +00:00
{
return;
}
2022-01-13 03:43:48 +00:00
OnEnter(player);
2022-01-13 02:43:18 +00:00
}
public void Exit(PlayerInfo player)
{
2022-01-13 04:58:38 +00:00
if (!Occupants.QuickRemove(player))
2022-01-13 02:43:18 +00:00
{
return;
}
2022-01-13 03:43:48 +00:00
OnExit(player);
2022-01-13 02:43:18 +00:00
}
2022-01-13 03:43:48 +00:00
2022-01-13 05:28:35 +00:00
/// <summary>
/// called when a player enters this trigger
/// </summary>
2022-01-13 04:58:38 +00:00
protected virtual void OnEnter(PlayerInfo player) { }
2022-01-13 03:43:48 +00:00
2022-01-13 05:28:35 +00:00
/// <summary>
/// called when a player exits this trigger or leaves the game
/// </summary>
2022-01-13 04:58:38 +00:00
protected virtual void OnExit(PlayerInfo player) { }
2022-01-13 02:43:18 +00:00
}
}