MiniGames/Assets/Scripts/CubeClash/CubeClash_HazardManager.cs

96 lines
2.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using UnityEngine;
public class CubeClash_HazardManager : MonoBehaviour
{
[Header("Hazards")]
public GameObject windGustPrefab;
public GameObject birdPrefab;
public GameObject butterflyPrefab;
public GameObject cubeLidPrefab;
public GameObject shockZonePrefab;
[Header("Items")]
public GameObject blueberryPrefab;
public GameObject timeBombPrefab;
[Header("Timing")]
public float hazardInterval = 5f;
public float itemInterval = 8f;
private float hazardTimer;
private float itemTimer;
void Update()
{
hazardTimer += Time.deltaTime;
itemTimer += Time.deltaTime;
// If ANY occupant (hazard or item) exists, do not spawn anything.
if (CubeClash_HazardMarker.ActiveCount > 0) return;
// Try hazard first (your original order). If both timers are ready the same frame,
// the first spawn will add a marker immediately, so the item block wont run.
if (hazardTimer >= hazardInterval)
{
SpawnHazard();
hazardTimer = 0f;
return; // ensure only one spawn per frame
}
if (itemTimer >= itemInterval)
{
SpawnItem();
itemTimer = 0f;
return; // ensure only one spawn per frame
}
}
void SpawnHazard()
{
int roll = Random.Range(0, 4);
GameObject prefab = null;
switch (roll)
{
case 0: prefab = windGustPrefab; break;
case 1: prefab = birdPrefab; break;
case 2: prefab = butterflyPrefab; break;
case 3: prefab = shockZonePrefab; break;
//case 4: prefab = cubeLidPrefab; break;
}
if (!prefab) return;
GameObject go;
if (prefab == shockZonePrefab || prefab == cubeLidPrefab)
go = Instantiate(prefab, GetRandomPos(), Quaternion.identity);
else
go = Instantiate(prefab);
// Ensure its marked so ActiveCount blocks further spawns
if (!go.GetComponent<CubeClash_HazardMarker>())
go.AddComponent<CubeClash_HazardMarker>();
}
void SpawnItem()
{
int roll = Random.Range(0, 2);
GameObject prefab = null;
switch (roll)
{
case 0: prefab = blueberryPrefab; break;
case 1: prefab = timeBombPrefab; break;
}
if (!prefab) return;
GameObject go = Instantiate(prefab);
// Reuse the SAME marker for items too
if (!go.GetComponent<CubeClash_HazardMarker>())
go.AddComponent<CubeClash_HazardMarker>();
}
Vector3 GetRandomPos()
{
return new Vector3(Random.Range(-3f, 3f), 1f, Random.Range(-3f, 3f));
}
}