-
Notifications
You must be signed in to change notification settings - Fork 0
/
SpawnEnemies.cs
103 lines (88 loc) · 3.35 KB
/
SpawnEnemies.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpawnEnemies : MonoBehaviour{
[Tooltip("Locations where enemies can be dropped in")]
public Transform[] spawnLocations;
[Tooltip("Types of units that can spawn, in ascending order of difficulty")]
public GameObject[] spawnTypes;
[Tooltip("For each unit in spawnTypes, assign a power rating, which will determine spawn rate")]
public int[] spawnValues;
[Tooltip("Time, in seconds between wave spawns")]
public float spawnTime;
[Tooltip("Max enemies spawned per wave. Must be smaller or equal to spawn points available")]
public int maxSpawns;
[Tooltip("Higher seed = more, stronger spawns")]
public int initialSeed;
[Tooltip("Higher max = more time to expand difficulty")]
public int maxSeed;
[Tooltip("Faster increase = more, stronger spawns, sooner")]
public int seedIncreasePerSecond;
[Tooltip("Current spawn seed, will be overriden by script")]
public int currentSeed;
[Tooltip("Completely stop the spawner until false")]
public bool isPaused = true;
void Start() {
currentSeed = initialSeed;
StartCoroutine(nameof(IncreaseDifficulty));
StartCoroutine(nameof(SpawnWave));
}
void Update() {
}
/// <summary>
/// Resets to initial seed value
/// </summary>
public void ResetInitial() {
currentSeed = initialSeed;
}
/// <summary>
/// Picks how many enemies will spawn this wave
/// </summary>
/// <returns>How many enemies will spawn this wave</returns>
int pickQuantity() {
float random = Random.Range(currentSeed, maxSeed + 1);
int spawns = (int) (random / maxSeed * maxSpawns);
return spawns;
}
/// <summary>
/// Picks an index from the spawnTypes array
/// </summary>
/// <returns>An index from the spawnTypes array</returns>
int pickSpawn() {
float random = Random.Range(0, currentSeed);
for(int i=spawnValues.Length-1; i>=0; i--) {
if (spawnValues[i] < random) {
return i;
}
}
return 0;
}
IEnumerator IncreaseDifficulty() {
while (true) {
yield return new WaitForSeconds(1f);
if (!isPaused) {
if (currentSeed + seedIncreasePerSecond < maxSeed) {
currentSeed += seedIncreasePerSecond;
} else {
currentSeed = maxSeed;
}
}
}
}
IEnumerator SpawnWave() {
while (true) {
yield return new WaitForSeconds(spawnTime);
if (!isPaused) {
int spawns = pickQuantity();
List<Transform> locationsUnpicked = new List<Transform>(spawnLocations);
for (int i = 0; i < spawns; i++) {
int index = Random.Range(0, locationsUnpicked.Count);
Transform location = locationsUnpicked[index];
locationsUnpicked.RemoveAt(index);
int spawnType = pickSpawn();
Instantiate(spawnTypes[spawnType], location.position + new Vector3(0f, 100f, 0f), Quaternion.identity);
}
}
}
}
}