You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
HighGroundRoyaleNetcode/Assets/Scripts/Gameplay/ChaosRuneSystem.cs

93 lines
3.0 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using System.Collections;
using Unity.Netcode;
using UnityEngine;
public class ChaosRuneSystem : NetworkBehaviour
{
[Header("Drop Timing (seconds)")]
[SerializeField] private float powerUpDropInterval = 10f;
[Header("Chaos Rune Prefabs")]
[Tooltip("Drag your ChaosRune prefabs here (must have NetworkObject + ChaosRune).")]
[SerializeField] private GameObject[] chaosRunePrefabs;
[Header("Spawn Area (local to this GameObject)")]
[Tooltip("Local offset from this GameObjects position to the center of the spawn area.")]
[SerializeField] private Vector3 spawnAreaCenter = Vector3.zero;
[Tooltip("Size of the rectangle: X = width, Y = depth.")]
[SerializeField] private Vector2 spawnAreaSize = new Vector2(20f, 20f);
[Tooltip("Height above this GameObjects Y-position from which runes spawn.")]
[SerializeField] private float spawnHeight = 15f;
public override void OnNetworkSpawn()
{
if (IsServer)
StartCoroutine(DropRunesLoop());
}
private IEnumerator DropRunesLoop()
{
while (true)
{
yield return new WaitForSeconds(powerUpDropInterval);
SpawnRandomRune();
}
}
private void SpawnRandomRune()
{
if (chaosRunePrefabs == null || chaosRunePrefabs.Length == 0)
{
Debug.LogWarning("ChaosRuneSystem: No prefabs assigned.");
return;
}
int idx = Random.Range(0, chaosRunePrefabs.Length);
GameObject prefab = chaosRunePrefabs[idx];
Vector3 pos = GetRandomDropPosition();
var go = Instantiate(prefab, pos, Quaternion.identity);
var netObj = go.GetComponent<NetworkObject>();
if (netObj != null)
netObj.Spawn();
else
Debug.LogError($"ChaosRuneSystem: Prefab '{prefab.name}' missing NetworkObject.");
}
private Vector3 GetRandomDropPosition()
{
// center in world space
Vector3 worldCenter = transform.position + spawnAreaCenter;
float halfW = spawnAreaSize.x * 0.5f;
float halfD = spawnAreaSize.y * 0.5f;
float x = worldCenter.x + Random.Range(-halfW, halfW);
float z = worldCenter.z + Random.Range(-halfD, halfD);
float y = transform.position.y + spawnHeight;
return new Vector3(x, y, z);
}
private void OnDrawGizmosSelected()
{
// draw spawn area rectangle on XZ plane
Vector3 worldCenter = transform.position + spawnAreaCenter;
Vector3 size = new Vector3(spawnAreaSize.x, 0.1f, spawnAreaSize.y);
Gizmos.color = Color.cyan;
Gizmos.DrawWireCube(worldCenter, size);
// draw vertical line & marker at spawn height
Gizmos.color = Color.gray;
Vector3 bottom = worldCenter;
Vector3 top = worldCenter + Vector3.up * spawnHeight;
Gizmos.DrawLine(bottom, top);
Gizmos.DrawWireSphere(top, 0.2f);
}
}
public enum ChaosRuneType
{
MeteorStrike,
BoobyTrap,
Barricade
}