Files
beyond/Assets/Scripts/Optimization/StreamingAsset.cs
2024-11-20 15:21:28 +01:00

115 lines
3.2 KiB
C#

using PixelCrushers;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Beyond
{
public abstract class StreamingAsset : MonoBehaviour
{
public static readonly string LAYER_NAME = "Triggers";
public Action OnVolume;
[Header("Detection")]
public LayerMask collisionLayer = 1 << 8;
public TagMask tagMask = new TagMask();
protected new Collider collider;
protected bool playerIn;
[SerializeField]
protected bool startVolumeState;
[SerializeField]
protected float deactivateDelay;
protected Coroutine deactivation = null;
public bool IsVolumeActive { get; private set; }
public bool PlayerIn { get => playerIn; set => playerIn = value; }
[Header("Debug")]
[SerializeField]
protected bool draw;
[SerializeField]
protected bool drawWire;
[SerializeField]
protected Color drawColor = new Color(1f, 1f, 1f, 0.4f);
[SerializeField]
protected Color drawWireColor = Color.white;
[SerializeField]
protected Color activeVolumeColor = new Color(1f, 0f, 1f, 0.4f);
[SerializeField]
protected Color activeVolumeWireColor = Color.magenta;
protected virtual void Awake()
{
collider = GetComponent<Collider>();
collider.isTrigger = true;
gameObject.layer = LayerMask.NameToLayer(LAYER_NAME);
}
protected virtual void Start()
{
SetActive(startVolumeState);
}
protected void SetActive(bool isActive)
{
if (!isActive && deactivateDelay != 0f)
{
Debug.Log("Start deactivateDelay Coroutine");
deactivation = StartCoroutine(SetActiveDelay(isActive));
}
else
{
SetActiveVolume(isActive);
}
}
private IEnumerator SetActiveDelay(bool isActive)
{
yield return new WaitForSeconds(deactivateDelay);
SetActiveVolume(isActive);
deactivation = null;
}
private void SetActiveVolume(bool isActive)
{
if (IsVolumeActive != isActive)
{
playerIn = isActive;
IsVolumeActive = isActive;
OnVolume?.Invoke();
}
}
protected void OnDrawGizmos()
{
if (draw)
{
Gizmos.color = drawColor;
if (playerIn)
{
Gizmos.color = activeVolumeColor;
}
Draw();
}
if (drawWire)
{
Gizmos.color = drawWireColor;
if (playerIn)
{
Gizmos.color = activeVolumeColor;
}
DrawWire();
}
}
protected abstract void DrawWire();
protected abstract void Draw();
protected abstract void OnTriggerEnter(Collider other);
protected abstract void OnTriggerExit(Collider other);
}
}