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
153
154
155
|
using System.Collections;
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class WaveSpawner : MonoBehaviour {
[Header("Attributes")]
public float TimeBetweenWaves;
public float SpawnRate;
[Header("Objects")]
public Transform SpawnPoint;
public Transform PathWay;
public Text WaveCountdownText;
[Header("Every possible enemy")]
public EnemyType[] Enemies;
[Header("Scripting vars")]
public Player Player; // Reference to the player object, should be set in designer
private Transform _parentObject;
private List<Vector3> _waypoints = new List<Vector3>();
public static int EnemiesAlive = 0;
private float _countdown = 2f;
private int _waveIndex = 0;
private bool _lastWavePayed;
void Awake() {
foreach (Transform child in PathWay) {
_waypoints.Add (child.position);
}
}
void Start() {
_parentObject = transform.Find ("enemies").gameObject.GetComponent <Transform> ();
}
void Update () {
if (EnemiesAlive > 0) {
return;
}
if (EnemiesAlive == 0 && !_lastWavePayed) {
Player.Payday();
_lastWavePayed = true;
}
if (_countdown <= 0f) {
StartCoroutine(SpawnWave()); // TODO Bytt ut denne med SpawnWaveRand (Gjør ferdig SpawnWaveRand)
_countdown = TimeBetweenWaves;
return;
}
Debug.Log(_countdown);
_countdown -= Time.deltaTime;
_countdown = Mathf.Clamp(_countdown, 0f, Mathf.Infinity);
//waveCountdownText.text = string.Format("{0:00.00}", countdown);
}
private int WaveEnemyCount(int waveNum) {
// 10.64 * e^0,57x
float pow = (float) Math.Pow( Math.E, 0.57f * waveNum);
return (int) Math.Floor(10.64f * pow);
}
private float EnemyAmountThing(int currentEnemy, int maxTypes) {
// TODO Change the for loop into a faster method
float rest = 1;
for (int i=1; i <= maxTypes; i++) {
if (i != maxTypes) { rest = rest / 2; }
if (i == currentEnemy + 1) { return rest; }
}
return 0;
}
IEnumerator SpawnWave () {
int enemiesToSpawn = WaveEnemyCount(_waveIndex);
EnemiesAlive = enemiesToSpawn;
List<WaveElement> wave = new List<WaveElement>();
for (int i = 0; i < Enemies.Length; i++) {
EnemyType enemy = Enemies[i];
float amount = enemiesToSpawn * EnemyAmountThing(i, Enemies.Length);
if (amount >= 1) {
wave.Add(new WaveElement {Prefab = enemy.Enemy, Amount = (int)Math.Floor(amount)} );
}
}
foreach (var enemyType in wave) {
for (int i = 0; i < enemyType.Amount; i++) {
SpawnEnemy(enemyType.Prefab);
yield return new WaitForSeconds(1f / SpawnRate);
}
yield return new WaitForSeconds(1f / 100f);
}
SpawnRate = SpawnRate * 2;
_waveIndex++;
_lastWavePayed = false;
}
IEnumerator SpawnWaveRand() {
int enemiesToSpawn = WaveEnemyCount(_waveIndex);
int[] waveEnemies = new int[Enemies.Length - 1];
EnemiesAlive = enemiesToSpawn;
for (int i = 1; i < enemiesToSpawn; i++) {
int currentEnemyInt = UnityEngine.Random.Range(0, Enemies.Length - 1);
EnemyType currentEnemy = Enemies[currentEnemyInt];
if (_waveIndex <= 1) {
} else if (_waveIndex <= 5) {
}
yield return new WaitForSeconds(1f / SpawnRate);
}
SpawnRate = SpawnRate * 2;
_waveIndex++;
}
void SpawnEnemy (GameObject enemyPrefab) {
GameObject newEnemy = Instantiate (enemyPrefab, new Vector3(0, 0, 0), Quaternion.identity, _parentObject);
Enemy script = newEnemy.GetComponent <Enemy> ();
Transform transform = newEnemy.GetComponent <Transform>();
script.Waypoints = _waypoints;
script.Player = Player;
transform.position = new Vector3 (0.93f, 0.483f, 0f);
}
[System.Serializable]
public class EnemyType {
public string Name;
public GameObject Enemy;
[Range(0, 1)]
public float Percentage;
}
public class WaveElement {
public GameObject Prefab { get; set; }
public int Amount { get; set; }
}
}
|