Hello,
I just wanted to ask if there is any way to write this code easier or better.
I want to achieve a simple Enemy Manager which spawns a Wave when they First Phase is over and when all enemies died
the first Phase start and then after the second wave spawn.
PhaseManager:
using UnityEngine;
using System.Collections;
public class Basic_PhaseManager : MonoBehaviour {
public int durationFP = 120;
float timer;
public bool onFP = true;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(onFP){
Basic_WaveManager wm = GetComponent<Basic_WaveManager>();
wm.enabled = false;
timer += Time.deltaTime;
if(timer >= durationFP){
onFP = false;
timer -= durationFP;
}
}else{
Basic_WaveManager wm = GetComponent<Basic_WaveManager>();
wm.enabled = true;
}
}
}
WaveManager:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Basic_WaveManager : MonoBehaviour {
#region Waves
public List<GameObject> Wave01 = new List<GameObject>();
public List<GameObject> Wave02 = new List<GameObject>();
public List<GameObject> Wave03 = new List<GameObject>();
public List<GameObject> Wave04 = new List<GameObject>();
public List<GameObject> Wave05 = new List<GameObject>();
public List<GameObject> Wave06 = new List<GameObject>();
public List<GameObject> Wave07 = new List<GameObject>();
public List<GameObject> Wave08 = new List<GameObject>();
public List<GameObject> Wave09 = new List<GameObject>();
public List<GameObject> Wave10 = new List<GameObject>();
#endregion
private int curWave = 1;
public List<GameObject> Spawns = new List<GameObject>();
void Start(){
Spawn();
}
void OnEnable(){
Spawn ();
}
void Update () {
if(Wave01 == null){
curWave = 2;
}if(Wave02 == null){
curWave = 3;
}if(Wave03 == null){
curWave = 4;
}if(Wave04 == null){
curWave = 5;
}if(Wave05 == null){
curWave = 6;
}if(Wave06 == null){
curWave = 7;
}if(Wave07 == null){
curWave = 8;
}if(Wave08 == null){
curWave = 9;
}if(Wave09 == null){
curWave = 10;
}if(Wave10 == null){
curWave = 11;
}
}
void Spawn(){
switch(curWave)
{
case 1:
for(int i = 0;i < Wave01.Count;i++){
int rand = Random.Range(1,Spawns.Count);
Instantiate(Wave01[i],Spawns[rand].transform.position,Wave01[i].transform.rotation);
}
break;
case 2:
for(int i = 0;i < Wave02.Count;i++){
int rand = Random.Range(1,Spawns.Count);
Instantiate(Wave02[i],Spawns[rand].transform.position,Wave02[i].transform.rotation);
}
break;
case 3:
for(int i = 0;i < Wave03.Count;i++){
int rand = Random.Range(1,Spawns.Count);
Instantiate(Wave03[i],Spawns[rand].transform.position,Wave03[i].transform.rotation);
}
break;
case 4:
for(int i = 0;i < Wave04.Count;i++){
int rand = Random.Range(1,Spawns.Count);
Instantiate(Wave04[i],Spawns[rand].transform.position,Wave04[i].transform.rotation);
}
break;
case 5:
for(int i = 0;i < Wave05.Count;i++){
int rand = Random.Range(1,Spawns.Count);
Instantiate(Wave05[i],Spawns[rand].transform.position,Wave05[i].transform.rotation);
}
break;
case 6:
for(int i = 0;i < Wave06.Count;i++){
int rand = Random.Range(1,Spawns.Count);
Instantiate(Wave06[i],Spawns[rand].transform.position,Wave06[i].transform.rotation);
}
break;
case 7:
for(int i = 0;i < Wave07.Count;i++){
int rand = Random.Range(1,Spawns.Count);
Instantiate(Wave07[i],Spawns[rand].transform.position,Wave07[i].transform.rotation);
}
break;
case 8:
for(int i = 0;i < Wave08.Count;i++){
int rand = Random.Range(1,Spawns.Count);
Instantiate(Wave08[i],Spawns[rand].transform.position,Wave08[i].transform.rotation);
}
break;
case 9:
for(int i = 0;i < Wave09.Count;i++){
int rand = Random.Range(1,Spawns.Count);
Instantiate(Wave09[i],Spawns[rand].transform.position,Wave09[i].transform.rotation);
}
break;
case 10:
for(int i = 0;i < Wave10.Count;i++){
int rand = Random.Range(1,Spawns.Count);
Instantiate(Wave10[i],Spawns[rand].transform.position,Wave10[i].transform.rotation);
}
break;
default:
WavesDone();
break;
}
}
void WavesDone(){
Basic_PhaseManager pm = GetComponent<Basic_PhaseManager>();
pm.onFP = true;
}
}