Files
beyond/Assets/Scripts/Optimization/BoxVolume.cs
2025-12-05 06:16:25 +01:00

87 lines
2.9 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Beyond
{
[RequireComponent(typeof(BoxCollider))]
public class BoxVolume : StreamingAsset
{
// DODANE: Licznik koliderów wewn¹trz woluminu
private int enteredCollidersCount = 0;
protected override void Draw()
{
if (collider == null)
{
collider = GetComponent<Collider>();
}
BoxCollider boxCollider = collider.GetComponent<BoxCollider>();
if (boxCollider)
{
Vector3 size = new Vector3(boxCollider.size.x * transform.localScale.x, boxCollider.size.y * transform.localScale.y, boxCollider.size.z * transform.localScale.z);
GizmosExtensions.DrawCube(transform.position + boxCollider.center, size, transform.rotation);
}
}
protected override void DrawWire()
{
if (collider == null)
{
collider = GetComponent<Collider>();
}
BoxCollider boxCollider = collider.GetComponent<BoxCollider>();
if (boxCollider)
{
Vector3 size = new Vector3(boxCollider.size.x * transform.localScale.x, boxCollider.size.y * transform.localScale.y, boxCollider.size.z * transform.localScale.z);
GizmosExtensions.DrawWireCube(transform.position + boxCollider.center, size, transform.rotation);
}
}
private void CancelDeactivationCoroutine()
{
if (deactivation != null)
{
Debug.Log("StopCoroutine deactivation");
StopCoroutine(deactivation);
deactivation = null;
}
}
protected override void OnTriggerEnter(Collider other)
{
// Sprawdzenie warunków kolizji
if (collisionLayer == 1 << other.gameObject.layer && tagMask.IsInTagMask(other.tag))
{
enteredCollidersCount++;
// Aktywacja nastêpuje tylko przy pierwszym wejœciu (licznik = 1)
if (enteredCollidersCount == 1)
{
CancelDeactivationCoroutine();
SetActive(true);
}
}
}
protected override void OnTriggerExit(Collider other)
{
// Sprawdzenie warunków kolizji
if (collisionLayer == 1 << other.gameObject.layer && tagMask.IsInTagMask(other.tag))
{
enteredCollidersCount--;
// Zabezpieczenie na wypadek b³êdu fizyki
if (enteredCollidersCount < 0) enteredCollidersCount = 0;
// Dezaktywacja nastêpuje tylko, gdy ostatni obiekt opuœci wolumin (licznik = 0)
if (enteredCollidersCount == 0)
{
SetActive(false);
}
}
}
}
}