-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDeployEnemy.cs
94 lines (80 loc) · 2.75 KB
/
DeployEnemy.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
using System.IO;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public class DeployEnemy : MonoBehaviour
{
// retrieves the enemy objects to copy
[SerializeField] GameObject enemy1;
[SerializeField] GameObject enemy2;
[SerializeField] GameObject enemy3;
private GameObject enemyClone;
// retrieves the game difficulty
[SerializeField] Difficulty difText;
// sets the respawn time and the list index of which spawn point
[SerializeField] float respawnTime = 2f;
[SerializeField] int spawnIndex;
// references to the 10 spawn points
[SerializeField] Transform spawn0; // left top
[SerializeField] Transform spawn1; // left middle
[SerializeField] Transform spawn2; // left bottom
[SerializeField] Transform spawn3; // bottom left
[SerializeField] Transform spawn4; // bottom right
[SerializeField] Transform spawn5; // right bottom
[SerializeField] Transform spawn6; // right middle
[SerializeField] Transform spawn7; // right top
[SerializeField] Transform spawn8; // top right
[SerializeField] Transform spawn9; // top left
private List<Transform> spawnArray;
void Start()
{
// creates a list of the spawn points
spawnArray = new List<Transform>(){
spawn0,
spawn1,
spawn2,
spawn3,
spawn4,
spawn5,
spawn6,
spawn7,
spawn8,
spawn9
};
// starts the flow of enemies toward the player
StartCoroutine(EnemySpawner());
}
private void SpawnEnemy(GameObject clone)
{
// creates a new instance of the monster
GameObject x = Instantiate(clone) as GameObject;
// GameObject x = PrefabUtility.InstantiatePrefab(PrefabUtility.GetCorrespondingObjectFromSource(enemy2Prefab)) as GameObject;
// sets position to the spawn point at the current index of the list
x.transform.position = new Vector2(spawnArray[spawnIndex].position.x, spawnArray[spawnIndex].position.y);
// changes the new spawn point to a random one
spawnIndex = Random.Range(0,9);
}
IEnumerator EnemySpawner()
{
// runs indefinitely
while (true)
{
if (difText.difficulty == 1)
{
enemyClone = enemy1;
}
else if (difText.difficulty == 2)
{
enemyClone = enemy2;
}
else if (difText.difficulty == 3)
{
enemyClone = enemy3;
};
// waits before spawning a new enemy
yield return new WaitForSeconds(respawnTime);
SpawnEnemy(enemyClone);
}
}
}