using System; using System.Collections.Generic; using TMPro; using UnityEngine; using Random = UnityEngine.Random; public class EnemySpawner : MonoBehaviour { #region Statication public static EnemySpawner instance; private void Awake() { if (instance != null && instance != this) { Destroy(gameObject); return; } instance = this; } #endregion [Header("Enemies")] public bool canSpawn = true; public Enemy[] enemiesToSpawn; public List spawnPoints; public float spawnRate; [SerializeField] private float currentSpawnTime; [Header("Boss")] private bool bossSpawned = false; public Enemy bossEnemy; private Enemy bossEnemyInstance; public Transform bossSpawnPoint; [SerializeField] private GameObject bossUI; [SerializeField] private Transform bossHealthBar; [SerializeField] private TextMeshProUGUI bossHealthText; [SerializeField] private TextMeshProUGUI bossNameText; [Header("Difficulty Scaling")] public float levelUpIntervals; public int enemyLevel; [Header("Cache")] [SerializeField] private Transform enemyFolder; public Marisa player; private void Update() { if (canSpawn) { currentSpawnTime -= Time.deltaTime; if (currentSpawnTime < 0) { currentSpawnTime = spawnRate; SpawnEnemy(enemiesToSpawn[Random.Range(0, enemiesToSpawn.Length)], spawnPoints[Random.Range(0, spawnPoints.Count)].position); } } else if (bossSpawned && !bossEnemyInstance) { if (Input.GetKey(KeyCode.E)) { LevelSwitcher.instance.LoadShop(); } } enemyLevel = (int)(Time.time / levelUpIntervals) + 1; //figure out the time.time thing because that's not gonna work here } public Enemy SpawnEnemy(Enemy enemy, Vector3 location) { Enemy newEnemy = Instantiate(enemy, location, Quaternion.identity); newEnemy.transform.SetParent(enemyFolder); newEnemy.closestTarget = player; //idk if there's actually gonna be any other target lol return newEnemy; } public void StartBoss() { bossEnemyInstance = SpawnEnemy(bossEnemy, bossSpawnPoint.position); bossUI.SetActive(true); canSpawn = false; Enemy.OnDamaged += UpdateBossHealthBar; //this kinda sucks but it technically works?? bossNameText.text = bossEnemyInstance.entityName; bossSpawned = true; } private void UpdateBossHealthBar() { bossHealthBar.localScale = new Vector3(Math.Clamp(bossEnemyInstance.health / bossEnemyInstance.maxHealth, 0, bossEnemyInstance.maxHealth), 1, 1); bossHealthText.text = $"{bossEnemyInstance.health}/{bossEnemyInstance.maxHealth}"; } }