fgm24/Assets/Scripts/Controller/EnemySpawner/EnemySpawner.cs

146 lines
4.7 KiB
C#
Raw Normal View History

2024-02-03 04:05:06 +01:00
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
2024-02-03 17:33:45 +01:00
using System.Threading;
using System.Threading.Tasks;
2024-02-03 04:05:06 +01:00
using Unity.VisualScripting;
using UnityEngine;
2024-02-03 17:33:45 +01:00
using UnityEngine.UIElements;
2024-02-03 04:05:06 +01:00
2024-02-04 00:08:51 +01:00
public class EnemySpawner : MonoBehaviour
2024-02-03 04:05:06 +01:00
{
// Shared
public int Wave = 0;
2024-02-04 00:16:27 +01:00
public float difficulty = 1f;
2024-02-03 04:05:06 +01:00
// Inspector
[SerializeField] private float difficultyIncreasePerWave = 0.1f;
2024-02-04 00:16:27 +01:00
[SerializeField] private float difficultyMultiplyDecreasePerWave = 0.1f;
2024-02-03 17:33:45 +01:00
[SerializeField] private float WaveTime = 20f;
[SerializeField] private List<float> enemyDifficulties;
[SerializeField] private float SpawnRadius = 10;
[SerializeField] private int NumEnemies = 6;
2024-02-03 22:31:54 +01:00
[SerializeField] private GameObject[] players;
2024-02-04 00:08:51 +01:00
[SerializeField] private float initialSpawnDelay = 5;
2024-02-03 04:05:06 +01:00
// Private
private bool nextWaveRequested = false;
private float timer = 0f;
2024-02-03 17:33:45 +01:00
private Camera mainCam;
private GameObject SpawnedEnenmyHolder;
[SerializeField] private List<EnemyPrefabInfo> enemyList;
2024-02-03 04:05:06 +01:00
2024-02-03 17:33:45 +01:00
private bool SpawnerStarted = false;
private void Start()
{
mainCam = Camera.main;
SpawnedEnenmyHolder = new GameObject("SpawnedEnenmyHolder");
for (int i = 0; i < 6; i++)
{
enemyList.Add(new EnemyPrefabInfo() { Difficulty = i+1, prefabs = Resources.LoadAll<GameObject>("Enemies/" + (i+1)) });
}
2024-02-04 00:08:51 +01:00
timer = WaveTime - initialSpawnDelay;
2024-02-03 17:33:45 +01:00
StartSpawning();
}
2024-02-03 04:05:06 +01:00
2024-02-03 17:33:45 +01:00
public void Update()
2024-02-03 04:05:06 +01:00
{
2024-02-03 17:33:45 +01:00
if (SpawnerStarted)
timer += Time.deltaTime;
if (timer > WaveTime || nextWaveRequested)
2024-02-03 04:05:06 +01:00
{
2024-02-03 17:33:45 +01:00
Task.Factory.StartNew(() => { Task.Delay(100); timer = 0; });
2024-02-03 04:05:06 +01:00
SpawnWave(difficulty);
Wave++;
2024-02-04 00:16:27 +01:00
if (difficultyIncreasePerWave > 1f)
{
difficultyIncreasePerWave -= difficultyMultiplyDecreasePerWave;
difficulty *= difficultyIncreasePerWave + 1;
}
2024-02-03 17:33:45 +01:00
nextWaveRequested = false;
timer = 0;
2024-02-03 04:05:06 +01:00
}
2024-02-03 17:33:45 +01:00
2024-02-03 04:05:06 +01:00
}
2024-02-03 17:33:45 +01:00
public void StartSpawning() => SpawnerStarted = true;
public void StartNextWave() => nextWaveRequested = true;
2024-02-03 04:05:06 +01:00
void SpawnWave(float difficulty)
{
2024-02-03 22:36:28 +01:00
//SpawnStrongestFirst(difficulty);
2024-02-03 17:33:45 +01:00
2024-02-03 22:36:28 +01:00
if (Wave != 0 && Wave % 10 == 0)
SpawnStrongestFirst(difficulty);
else
SpawnRandom(difficulty);
2024-02-03 17:33:45 +01:00
}
void SpawnStrongestFirst(float difficulty)
{
var decendingList = enemyList.Where(x => x.Difficulty < difficulty).OrderByDescending(x => x.Difficulty).ToArray();
2024-02-03 04:05:06 +01:00
for (int i = 0; i < decendingList.Length; i++)
{
while (difficulty > decendingList[i].Difficulty)
{
2024-02-03 17:33:45 +01:00
GameObject enemy = Instantiate(decendingList[i].prefabs[UnityEngine.Random.Range(0, decendingList[i].prefabs.Length)], GetRandomPointOnCircle(mainCam.transform.position, SpawnRadius), Quaternion.identity, SpawnedEnenmyHolder.transform);
2024-02-03 04:05:06 +01:00
difficulty -= decendingList[i].Difficulty;
2024-02-03 22:31:54 +01:00
enemy.GetComponent<EnemyPathFinding>().targets = players.Select(x=>x.transform).ToArray();
2024-02-03 04:05:06 +01:00
}
}
}
2024-02-03 17:33:45 +01:00
void SpawnRandom(float difficulty)
{
2024-02-04 00:26:32 +01:00
difficulty *= 1.1f;
2024-02-03 23:22:55 +01:00
while (difficulty > 1f) // Spawn until difficulty is less than 0.5f
2024-02-03 17:33:45 +01:00
{
var validEnemies = enemyList.Where(x => x.Difficulty <= difficulty).ToArray();
2024-02-03 23:22:55 +01:00
int enemyIndex = UnityEngine.Random.Range(0, validEnemies.Length);
var enemyToSpawn = validEnemies[enemyIndex];
2024-02-03 17:33:45 +01:00
difficulty -= enemyToSpawn.Difficulty;
2024-02-03 23:22:55 +01:00
int variant = UnityEngine.Random.Range(0, enemyToSpawn.prefabs.Length);
GameObject variantToSpawn = enemyToSpawn.prefabs[variant];
GameObject enemy = Instantiate(variantToSpawn, GetRandomPointOnCircle(mainCam.transform.position, SpawnRadius), Quaternion.identity, SpawnedEnenmyHolder.transform);
2024-02-03 22:31:54 +01:00
enemy.GetComponent<EnemyPathFinding>().targets = players.Select(x => x.transform).ToArray();
2024-02-03 17:33:45 +01:00
}
}
public Vector3 GetRandomPointOnCircle(Vector3 location, float radius)
{
float angle = UnityEngine.Random.Range(0f, 360f);
float radians = Mathf.Deg2Rad * angle;
Vector3 position = transform.position;
float x = position.x + radius * Mathf.Cos(radians);
float y = position.y + radius * Mathf.Sin(radians);
return new Vector3(x, y, position.z);
}
#if UNITY_EDITOR
private void OnDrawGizmosSelected()
{
Gizmos.color = Color.green;
Gizmos.DrawWireSphere(transform.position, SpawnRadius);
for (int i = 0; i < 10; i++)
{
Gizmos.DrawWireSphere(GetRandomPointOnCircle(transform.position, SpawnRadius), 0.25f);
}
}
#endif
2024-02-03 04:05:06 +01:00
}