using System.Collections.Generic; using Drawing; using Pathfinding; using Sirenix.OdinInspector; using UnityEngine; namespace Reset.Units{ public class EnemySpawn : MonoBehaviour{ public float radius = 30f; public int minimumEnemies = 1; public int maximumEnemies = 5; public Vector2 enemyCount; // TODO: Replace this with an Enemy selector based on difficulty, random chance, etc? public GameObject enemy; public List enemies; public GridGraph relatedGraph; void Start(){ CreateAstarGraph(); SpawnEnemies(); } void CreateAstarGraph(){ relatedGraph = AstarPath.active.data.AddGraph(typeof(GridGraph)) as GridGraph; relatedGraph.SetDimensions(Mathf.FloorToInt(radius) * 2, Mathf.FloorToInt(radius) * 2, 1f); relatedGraph.collision.diameter = 3f; AstarPath.active.Scan(relatedGraph); GetComponent().graph = relatedGraph; } void SpawnEnemies(){ int count = Random.Range(minimumEnemies, maximumEnemies + 1); for (int i = 0; i < count; i++) { Vector3 newPosition = transform.position; float randomX = Random.Range(-(radius / 2f), radius / 2f); float randomZ = Random.Range(-(radius / 2f), radius / 2f); newPosition += new Vector3(randomX, transform.position.y, randomZ); float randomRot = Random.Range(0f, 360f); GameObject newEnemy = Instantiate(enemy, newPosition, Quaternion.AngleAxis(randomRot, Vector3.up)); newEnemy.GetComponent().relatedSpawner = this; enemies.Add(newEnemy); } } // Update is called once per frame void Update(){ Draw.WireCylinder(transform.position, transform.position + Vector3.up * 7f, radius); if (PlayerIsInRange()) { SetPlayerAsTarget(); } } GameObject PlayerIsInRange(){ // TODO: Make compatible with all players Vector3 playerPos = PlayerManager.Player.transform.position; // Skip checking and return null/false if the player is nowhere near the spawn if (Vector3.Distance(playerPos, transform.position) < radius * 1.5f) { return null; } // If they are in range, check if the player is close enough to either an enemy or the spawn center if (Vector3.Distance(playerPos, transform.position) < radius * .33f) { return PlayerManager.Player; } foreach (GameObject thisEnemy in enemies) { if (Vector3.Distance(playerPos, thisEnemy.transform.position) < radius / 2f) { return PlayerManager.Player; } } return null; } void SetPlayerAsTarget(){ foreach (GameObject thisEnemy in enemies) { thisEnemy.GetComponent().SetNewTarget(PlayerManager.Player); } } } }