89 lines
2.4 KiB
C#
89 lines
2.4 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]
|
|
GameObject entityPrefab = 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!;
|
|
|
|
void Awake() => safeZone = GetComponentInChildren<SafeZone>();
|
|
|
|
void Start() => gameFlowManager.stateChanged += OnGameFlowStateChanged;
|
|
|
|
void OnDestroy() => gameFlowManager.stateChanged -= OnGameFlowStateChanged;
|
|
|
|
void SpawnEnemy(int spawnerIndex) {
|
|
if (!gameFlowManager.CanDoAction)
|
|
return;
|
|
|
|
var gladiator = Instantiate(entityPrefab, gladiatorParent).GetComponent<Gladiator>();
|
|
gladiator.arena = this;
|
|
gladiator.transform.position = spawners[spawnerIndex].position;
|
|
gladiator.direction = spawners[spawnerIndex].direction;
|
|
gladiator.gameFlowManager = gameFlowManager;
|
|
}
|
|
|
|
IEnumerator SpawnEnemies() {
|
|
yield return new WaitForSeconds(stats.initWaitToSpawn);
|
|
|
|
int currentSpawner = 0;
|
|
int amountSpawned = 0;
|
|
while(true){
|
|
while (amountSpawned < stats.waveSize) {
|
|
currentSpawner = Random.Range(0, spawners.Length);
|
|
SpawnEnemy(currentSpawner);
|
|
amountSpawned++;
|
|
}
|
|
yield return new WaitForSeconds(stats.secondsBetweenSpawners);
|
|
amountSpawned = 0;
|
|
}
|
|
|
|
}
|
|
|
|
public Vector3 GetMoatExtents(){
|
|
return safeZone.GetMoatExtents();
|
|
}
|
|
|
|
void OnGameFlowStateChanged(BaseState oldState, BaseState newState) {
|
|
if (oldState is GameFlowManager.StartFlowState && newState is GameFlowManager.GameplayFlowState)
|
|
StartCoroutine(SpawnEnemies());
|
|
}
|
|
|
|
#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
|
|
} |