-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpawner.cs
More file actions
152 lines (124 loc) · 3.48 KB
/
Spawner.cs
File metadata and controls
152 lines (124 loc) · 3.48 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
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
using UnityEngine;
using System.Collections;
public class Spawner : MonoBehaviour
{
public bool devMode;
public Enemy enemy;
public Wave[] waves;
LivingEntity playerEntity;
Transform playerT;
Wave currentWave;
int currentWaveNumber;
int enemiesRemainingToSpawn;
int enemiesRemainingAlive;
float nextSpawnTime;
MapGenerator map;
float timeBetweenCampingChecks = 2f;
float campThresholdDistance = 1.5f;
float nextCampCheckTime;
Vector3 campPositionOld;
bool isCamping;
bool isDisabled;
public event System.Action<int> OnNewWave;
void Start()
{
playerEntity = FindObjectOfType<Player>();
playerT = playerEntity.transform;
nextCampCheckTime = timeBetweenCampingChecks + Time.time;
campPositionOld = playerT.position;
playerEntity.OnDeath += OnPlayerDeath;
map = FindObjectOfType<MapGenerator>();
NextWave();
}
void Update()
{
if (!isDisabled)
{
if (Time.time > nextCampCheckTime)
{
nextCampCheckTime = timeBetweenCampingChecks + Time.time;
isCamping = Vector3.Distance(playerT.position, campPositionOld) < campThresholdDistance;
campPositionOld = playerT.position;
}
if ((enemiesRemainingToSpawn > 0 || currentWave.infinite) && Time.time > nextSpawnTime)
{
enemiesRemainingToSpawn--;
nextSpawnTime = Time.time + currentWave.timeBetweenSpawns;
StartCoroutine("SpawnEnemy");
}
}
if (devMode)
{
if (Input.GetKeyDown(KeyCode.Return))
{
StopCoroutine("SpawnEnemy");
foreach (var enemy in FindObjectsOfType<Enemy>())
{
GameObject.Destroy(enemy.gameObject);
}
NextWave();
}
}
}
IEnumerator SpawnEnemy()
{
var spawnDelay = 1f;
var tileFlashSpeed = 4f;
Transform spawnTile = map.GetRandomOpenTile();
if (isCamping) spawnTile = map.GetTileFromPosition(playerT.position);
var tileMat = spawnTile.GetComponent<Renderer>().material;
var initialColor = Color.white;
var p = enemiesRemainingToSpawn * 1.0f / currentWave.enemyCount;
var flashColor = Color.Lerp(map.CurrentMap.foregroundColor, map.CurrentMap.backgroundColor, p);
var spawnTimer = 0f;
while (spawnTimer < spawnDelay)
{
tileMat.color = Color.Lerp(initialColor, flashColor, Mathf.PingPong(spawnTimer * tileFlashSpeed, 1));
spawnTimer += Time.deltaTime;
yield return null;
}
Enemy spawnedEnemy = Instantiate(enemy, spawnTile.position + Vector3.up, Quaternion.identity) as Enemy;
spawnedEnemy.OnDeath += OnEnemyDeath;
spawnedEnemy.SetCharacteristics(currentWave.moveSpeed, currentWave.hitsToKillPlayer, currentWave.enemyHealth, currentWave.skinColor);
}
void OnPlayerDeath()
{
isDisabled = true;
}
void OnEnemyDeath()
{
enemiesRemainingAlive--;
if (enemiesRemainingAlive == 0)
{
NextWave();
}
}
void ResetPlayerPosition()
{
playerT.position = map.GetTileFromPosition(Vector3.zero).position + Vector3.up * 3;
}
void NextWave()
{
currentWaveNumber++;
print(string.Format("Current Wave: {0}", currentWaveNumber));
if (currentWaveNumber - 1 < waves.Length)
{
currentWave = waves[currentWaveNumber - 1];
enemiesRemainingToSpawn = currentWave.enemyCount;
enemiesRemainingAlive = enemiesRemainingToSpawn;
if (OnNewWave != null) OnNewWave(currentWaveNumber);
ResetPlayerPosition();
}
}
[System.Serializable]
public class Wave
{
public bool infinite;
public int enemyCount;
public float timeBetweenSpawns;
public float moveSpeed;
public int hitsToKillPlayer;
public float enemyHealth;
public Color skinColor;
}
}