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
|
|
|
|
|
|
|
public class NewBehaviourScript : MonoBehaviour
|
|
|
|
{
|
|
|
|
// Shared
|
|
|
|
public int Wave = 0;
|
2024-02-03 17:33:45 +01:00
|
|
|
public float difficulty = 1;
|
2024-02-03 04:05:06 +01:00
|
|
|
|
|
|
|
// Inspector
|
|
|
|
[SerializeField] private float difficultyIncreasePerWave = 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 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)) });
|
|
|
|
}
|
|
|
|
|
|
|
|
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++;
|
|
|
|
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 17:33:45 +01:00
|
|
|
SpawnStrongestFirst(difficulty);
|
|
|
|
|
|
|
|
//if (Wave != 0 && Wave % 10 == 0)
|
|
|
|
// SpawnStrongestFirst(difficulty);
|
|
|
|
//else
|
|
|
|
// SpawnRandom(difficulty);
|
|
|
|
}
|
|
|
|
|
|
|
|
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 17:33:45 +01:00
|
|
|
|
|
|
|
void SpawnRandom(float difficulty)
|
|
|
|
{
|
|
|
|
while (difficulty > 0.5f) // Spawn until difficulty is less than 0.5f
|
|
|
|
{
|
|
|
|
var validEnemies = enemyList.Where(x => x.Difficulty <= difficulty).ToArray();
|
|
|
|
var enemyToSpawn = validEnemies[UnityEngine.Random.Range(0, validEnemies.Length)];
|
|
|
|
difficulty -= enemyToSpawn.Difficulty;
|
|
|
|
GameObject enemy = Instantiate(enemyToSpawn.prefabs[UnityEngine.Random.Range(0, enemyToSpawn.prefabs.Length)], GetRandomPointOnCircle(mainCam.transform.position, SpawnRadius), Quaternion.identity, SpawnedEnenmyHolder.transform);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|