rabidus-test/Assets/BNG Framework/Scripts/Helpers/GrabberArea.cs

62 lines
1.5 KiB
C#
Raw Permalink Normal View History

2023-07-24 16:38:13 +03:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace BNG {
/// <summary>
/// Keeps a List of all Grabbers within this Trigger
/// </summary>
public class GrabberArea : MonoBehaviour {
public Grabber InArea;
public List<Grabber> grabbersInArea;
private void Update() {
InArea = GetOpenGrabber();
}
public Grabber GetOpenGrabber() {
if(grabbersInArea != null && grabbersInArea.Count > 0) {
foreach (var g in grabbersInArea) {
if(!g.HoldingItem) {
return g;
}
}
}
return null;
}
void OnTriggerEnter(Collider other) {
Grabber grab = other.GetComponent<Grabber>();
if (grab != null) {
if(grabbersInArea == null) {
grabbersInArea = new List<Grabber>();
}
if(!grabbersInArea.Contains(grab)) {
grabbersInArea.Add(grab);
}
}
}
void OnTriggerExit(Collider other) {
Grabber grab = other.GetComponent<Grabber>();
if (grab != null) {
if (grabbersInArea == null) {
return;
}
if (grabbersInArea.Contains(grab)) {
grabbersInArea.Remove(grab);
}
}
}
}
}