105 lines
3.2 KiB
C#
105 lines
3.2 KiB
C#
#nullable enable
|
|
using System;
|
|
using System.Collections;
|
|
using NaughtyAttributes;
|
|
using UnityEngine;
|
|
using Random = UnityEngine.Random;
|
|
|
|
public class Arena : MonoBehaviour {
|
|
[Serializable]
|
|
struct GladiatorEntrance {
|
|
public Vector2 position;
|
|
public Vector2 direction;
|
|
}
|
|
|
|
[SerializeField] [Required]
|
|
GameFlowManager gameFlowManager = null!;
|
|
|
|
//TODO Add some kind of "MinLength(1)" attribute
|
|
[SerializeField]
|
|
GladiatorEntrance[] spawners = null!;
|
|
[SerializeField] [Required]
|
|
ArenaStats stats = null!;
|
|
[SerializeField] [Required]
|
|
public AIEntity lightGladiator = null!;
|
|
[SerializeField] [Required]
|
|
public AIEntity regularGladiator = null!;
|
|
[SerializeField] [Required]
|
|
public AIEntity heavyGladiator = null!;
|
|
|
|
[field: SerializeField] [field: Required]
|
|
public Transform gladiatorParent { get; private set; } = null!;
|
|
|
|
[field: SerializeField] [field: Required]
|
|
public Transform minionParent { get; private set; } = null!;
|
|
|
|
[field: SerializeField] [field: Required]
|
|
public Transform graveyard { get; private set; } = null!;
|
|
|
|
SafeZone safeZone = null!;
|
|
[field: SerializeField]int currWaveSize = 0;
|
|
|
|
void Awake() => safeZone = GetComponentInChildren<SafeZone>();
|
|
|
|
void Start() => StartCoroutine(SpawnEnemies());
|
|
|
|
void SpawnEnemy(int spawnerIndex, AIEntity entityPrefab) {
|
|
if (!gameFlowManager.CanDoAction)
|
|
return;
|
|
|
|
var gladiator = Instantiate(entityPrefab, gladiatorParent).GetComponent<Gladiator>();
|
|
gladiator.arena = this;
|
|
float randFloat = Random.Range(0.1f, 0.5f);
|
|
Vector2 offset = new Vector2(randFloat, randFloat);
|
|
gladiator.transform.position = spawners[spawnerIndex].position + offset;
|
|
gladiator.direction = spawners[spawnerIndex].direction;
|
|
gladiator.gameFlowManager = gameFlowManager;
|
|
}
|
|
|
|
IEnumerator SpawnEnemies() {
|
|
yield return new WaitForSeconds(stats.initWaitToSpawn);
|
|
currWaveSize = stats.initWaveSize;
|
|
int currentSpawner = 0;
|
|
int lightAmountSpawned = 0;
|
|
int heavyAmountSpawned = 0;
|
|
int amountSpawned = 0;
|
|
int wave = 1;
|
|
while(true){
|
|
while (amountSpawned < currWaveSize) {
|
|
currentSpawner = Random.Range(0, spawners.Length);
|
|
if(currWaveSize >= .5*stats.maxWaveSize && lightAmountSpawned <= stats.maxWaveSize * stats.lightRatio){
|
|
SpawnEnemy(currentSpawner, lightGladiator);
|
|
lightAmountSpawned++;
|
|
}else if(currWaveSize >= .8*stats.maxWaveSize && heavyAmountSpawned <= stats.maxWaveSize * stats.heavyRatio){
|
|
SpawnEnemy(currentSpawner, heavyGladiator);
|
|
heavyAmountSpawned++;
|
|
}else{
|
|
SpawnEnemy(currentSpawner, regularGladiator);
|
|
}
|
|
amountSpawned++;
|
|
}
|
|
if(wave++ >= stats.increaseWaveStep){
|
|
if((currWaveSize += stats.waveIncrease) > stats.maxWaveSize) currWaveSize=stats.maxWaveSize;
|
|
}
|
|
amountSpawned = 0;
|
|
lightAmountSpawned = 0;
|
|
heavyAmountSpawned = 0;
|
|
yield return new WaitForSeconds(stats.secondsBetweenSpawners);
|
|
}
|
|
|
|
}
|
|
|
|
public Vector3 GetMoatExtents(){
|
|
return safeZone.GetMoatExtents();
|
|
}
|
|
|
|
#if UNITY_EDITOR
|
|
void OnDrawGizmosSelected() {
|
|
Gizmos.color = Color.blue;
|
|
foreach (GladiatorEntrance entrance in spawners) {
|
|
Gizmos.DrawWireCube(entrance.position, Vector3.one);
|
|
Gizmos.DrawLine(entrance.position, entrance.position + entrance.direction);
|
|
}
|
|
}
|
|
#endif
|
|
} |