-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObjectSpawner.cs
More file actions
67 lines (65 loc) · 1.98 KB
/
ObjectSpawner.cs
File metadata and controls
67 lines (65 loc) · 1.98 KB
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
using UnityEngine;
using System.Collections;
public class ObjectSpawner : MonoBehaviour
{
//create coroutine that handles enemy wave spawning
//do this by starting with an integer below 10
//spawn that many enemies with a x seconds pause between each (public variable)
//where t is the iterator increased upon by 1 each time the loop
//satisfies all conditional gameplay checks
//USE EXPONENTIAL GROWTH FORMULA
//spawns a new wave of enemies t time after last wave is destroyed
//do this by passing the variable above that determines the number of enemies to be spawned
//to a helper function that does gameobject child counts and compares the result
// Use this for initialization
public GameObject prefab;
public float delay;
public int maxObjects;
private bool isSafeToPlay;
private bool isSpawning = false;
int currentObjects;
void Start ()
{
NotificationCenter.DefaultCenter.AddObserver (this, "SafeToPlay");
/*MyGUIWindow newWindow = gameObject.AddComponent<MyGUIWindow> ();
newWindow.SetDimensions (new Vector2 (220, 10), 100);
newWindow.SetWindowName ("spawner");
newWindow.AddButton ("start", this, "SpawnObjects");
newWindow.AddButton ("stop", this, "StopSpawning");*/
}
private void SpawnObjects ()
{
if (isSafeToPlay) {
isSpawning = true;
StartCoroutine ("CoSpawnObjects");
}
}
private void StopSpawning ()
{
if (isSafeToPlay) {
isSpawning = false;
StopCoroutine ("CoSpawnObjects");
}
}
void ToggleSpawning ()
{
if (isSpawning == false) {
SpawnObjects ();
} else {
StopSpawning ();
}
}
private IEnumerator CoSpawnObjects ()
{
while (currentObjects < maxObjects) {
GameObject newObject = (GameObject)Instantiate (prefab, transform.position, Quaternion.identity);
newObject.transform.SetParent (transform);
yield return new WaitForSeconds (delay);
}
yield return null;
}
private void SafeToPlay ()
{
isSafeToPlay = true;
}
}