-
Notifications
You must be signed in to change notification settings - Fork 1
/
EnemyWave.h
201 lines (167 loc) · 5.46 KB
/
EnemyWave.h
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#ifndef _ENEMY_WAVE_H_
#define _ENEMY_WAVE_H_
#include <tuple>
#include <memory>
#include <algorithm>
#include "Enemy.h"
#include "Random.h"
namespace typing
{
class EnemyWave
{
public:
virtual bool IsFinished() const = 0;
virtual void Start()
{
}
virtual void Spawn()
{
}
};
typedef std::shared_ptr<EnemyWave> EnemyWavePtr;
typedef std::weak_ptr<EnemyWave> EnemyWaveWeakPtr;
typedef EnemyWavePtr (*WaveCreateFn)();
template <typename T> static EnemyWavePtr CreateEnemyWave()
{
return EnemyWavePtr(new T);
}
class RandomEnemyWaveFactory
{
public:
template <typename T> void AddWave(unsigned int minLevel)
{
m_waves.push_back(std::make_tuple(minLevel,
&CreateEnemyWave<T>));
}
EnemyWavePtr CreateWave(unsigned int level)
{
// Find the list of waves that are valid for the current level.
WaveCreateVec validWaves(m_waves.size());
auto iter = std::copy_if(
m_waves.begin(), m_waves.end(), validWaves.begin(),
[=](WaveMapping m) -> bool { return std::get<0>(m) <= level; });
validWaves.resize(std::distance(validWaves.begin(), iter));
// Pick a random wave from the valid waves.
auto createTup =
validWaves[RAND.Range(
static_cast<unsigned int>(0),
static_cast<unsigned int>(validWaves.size() - 1))];
WaveCreateFn createFn = std::get<1>(createTup);
return createFn();
}
private:
typedef std::tuple<unsigned int, WaveCreateFn> WaveMapping;
typedef std::vector<WaveMapping> WaveCreateVec;
WaveCreateVec m_waves;
};
class CyclicEnemyWaveFactory
{
public:
template <typename T> void AddWave()
{
m_waves.push_back(&CreateEnemyWave<T>);
}
EnemyWavePtr CreateWave()
{
WaveCreateFn createFn = m_waves[m_index++ % m_waves.size()];
return createFn();
}
void Reset()
{
m_index = 0;
}
unsigned int Cycles() const
{
if (m_waves.empty()) {
return 0;
} else {
return static_cast<unsigned int>(m_index / m_waves.size());
}
}
private:
typedef std::vector<WaveCreateFn> WaveCreateVec;
WaveCreateVec m_waves;
unsigned int m_index;
};
//////////////////////////////////////////////////////////////////////////
// BasicEnemyWave
//////////////////////////////////////////////////////////////////////////
class BasicEnemyWave : public EnemyWave
{
public:
void Start();
void Spawn();
bool IsFinished() const;
private:
BasicEnemyVec m_enemies;
unsigned int m_enemyCount;
float m_enemySpeed;
float m_nextSpawnTime;
};
//////////////////////////////////////////////////////////////////////////
// AccelEnemyWave
//////////////////////////////////////////////////////////////////////////
class AccelEnemyWave : public EnemyWave
{
public:
void Start();
void Spawn();
bool IsFinished() const;
private:
AccelEnemyVec m_enemies;
unsigned int m_enemyCount;
float m_enemySpeed;
float m_nextSpawnTime;
};
//////////////////////////////////////////////////////////////////////////
// MissileEnemyWave
//////////////////////////////////////////////////////////////////////////
class MissileEnemyWave : public EnemyWave
{
public:
void Start();
void Spawn();
bool IsFinished() const;
private:
MissileEnemyVec m_enemies;
unsigned int m_enemyCount;
float m_nextSpawnTime;
};
//////////////////////////////////////////////////////////////////////////
// BombEnemyWave
//////////////////////////////////////////////////////////////////////////
class BombEnemyWave : public EnemyWave
{
public:
BombEnemyWave()
{
}
void Start();
void Spawn();
bool IsFinished() const;
private:
BombEnemyVec m_enemies;
unsigned int m_enemyCount;
float m_nextSpawnTime;
};
//////////////////////////////////////////////////////////////////////////
// SeekerEnemyWave
//////////////////////////////////////////////////////////////////////////
class SeekerEnemyWave : public EnemyWave
{
public:
void Start();
void Spawn();
bool IsFinished() const;
private:
static const unsigned int SEEKERENEMYWAVE_MIN_ENEMIES = 6;
static const unsigned int SEEKERENEMYWAVE_MAX_ENEMIES = 12;
static const float SEEKERENEMYWAVE_SPAWN_GAP;
static const float SEEKERENEMYWAVE_SPAWN_Y;
static const float SEEKERENEMYWAVE_MAX_SPAWN_X;
SeekerEnemyVec m_enemies;
unsigned int m_enemyCount;
float m_nextSpawnTime;
};
}
#endif // _ENEMY_WAVE_H_