BloodAndGore/Assets/Scripts/Enemies/EnemySpawner.cs

36 lines
1.4 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Saltosion.OneWeapon.Enemies {
public class EnemySpawner : MonoBehaviour {
public GameObject[] EnemyPrefabs;
public float SpawnCooldown = 5.0f;
public float SpawnChance = 0.5f;
public int MaxEnemiesWhenSpawning = 5;
public float Radius = 4;
private float SpawnTime = 0.0f;
private void Update() {
if (SpawnTime > 0) {
SpawnTime -= Time.deltaTime;
} else {
GameObject[] Enemies = GameObject.FindGameObjectsWithTag("Enemy");
if (Enemies.Length < 5 && Random.value < SpawnChance) {
float DirectionRadians = Random.value * Mathf.PI * 2.0f;
Vector2 Direction = new Vector2(Mathf.Cos(DirectionRadians), Mathf.Sin(DirectionRadians));
float Distance = Random.value * Radius;
Instantiate(EnemyPrefabs[(int)(Mathf.Min(0.99f, Random.value) * EnemyPrefabs.Length)], (Vector2)transform.position + Direction * Distance, new Quaternion(), null);
}
SpawnTime = SpawnCooldown;
}
}
private void OnDrawGizmosSelected() {
Gizmos.color = Color.green;
Gizmos.DrawWireSphere(transform.position, Radius);
}
}
}