78 lines
2.4 KiB
C#
78 lines
2.4 KiB
C#
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<Transform> spawnPoints;
|
|
public float spawnRate;
|
|
[SerializeField] private float currentSpawnTime;
|
|
[Header("Boss")]
|
|
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("Cache")]
|
|
[SerializeField] private Transform enemyFolder;
|
|
[SerializeField] private 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);
|
|
}
|
|
}
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
private void UpdateBossHealthBar()
|
|
{
|
|
bossHealthBar.localScale = new Vector3(Math.Clamp(bossEnemyInstance.health / bossEnemyInstance.maxHealth, 0, bossEnemyInstance.maxHealth), 1, 1);
|
|
bossHealthText.text = $"{bossEnemyInstance.health}/{bossEnemyInstance.maxHealth}";
|
|
}
|
|
}
|