-
Notifications
You must be signed in to change notification settings - Fork 0
/
EnemySpawner.cpp
127 lines (105 loc) · 2.44 KB
/
EnemySpawner.cpp
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
#include "Global.h"
#include "EnemySpawner.h"
#include "Game.h"
#include "Slime.h"
#include "string"
using namespace General;
void EnemySpawner::Initialize()
{
m_initDelayTime = 1.0f;
m_delayTime = m_initDelayTime;
m_timer = 0.0f;
//Every 5 seconds, we reduce the delay time to spawn new enemy
m_timeMark = 5.0;
//The reduce value per 5 seconds
m_delayTimeReduce = 0.1f;
m_minDelayTime = 0.5f;
m_numberSpawned = 0;
m_shouldUpdate = true;
}
void EnemySpawner::Update(float deltaTime)
{
if (!m_shouldUpdate) return;
UpdateTimer(deltaTime);
UpdateEnemy(deltaTime);
}
/// <summary>
/// Stop and kill all enemies in the screen
/// </summary>
void EnemySpawner::StopAndClear()
{
m_shouldUpdate = false;
for (auto enemy : m_spawnList)
{
if (!enemy.second->IsActive())
{
m_deadKeyList.push_back(enemy.first);
}
else
{
dynamic_cast<Slime*>(enemy.second)->SelfKill();
}
}
for (auto index : m_deadKeyList)
{
delete m_spawnList[index];
m_spawnList.erase(index);
}
m_deadKeyList.clear();
m_spawnList.clear();
}
void EnemySpawner::UpdateTimer(float deltaTime)
{
m_timer += deltaTime;
m_timerReduce += deltaTime;
if (m_timerReduce >= m_timeMark)
{
m_timerReduce = 0;
m_delayTime -= m_delayTimeReduce;
if (m_delayTime <= m_minDelayTime)
{
m_delayTime = m_minDelayTime;
}
}
if (m_timer >= m_delayTime)
{
SpawnNextEnemy();
m_timer = 0;
}
}
void EnemySpawner::UpdateEnemy(float deltaTime)
{
for (auto enemy : m_spawnList)
{
if (!enemy.second->IsActive())
{
m_deadKeyList.push_back(enemy.first);
}
else
{
enemy.second->Update(deltaTime);
Game::m_quadTreev2->Insert(enemy.second);
}
}
for (auto index : m_deadKeyList)
{
m_spawnList.erase(index);
}
m_deadKeyList.clear();
}
void EnemySpawner::SpawnNextEnemy()
{
auto name = std::to_string(m_numberSpawned);
std::mt19937 rng(rd());
std::uniform_int_distribution<int> distributionX(0, g_WindowSettings.Width);
std::uniform_int_distribution<int> distributionY(0, g_WindowSettings.Height);
float x = static_cast<float>(distributionX(rng));
float y = static_cast<float>(distributionY(rng));
auto newEnemy = new Slime(name.c_str(), TEXTURE_ID::SLIME_TEX, x, y, 16, 16, m_numberSpawned + 100);
newEnemy->SetLayer(Layer::ENEMY);
//EnemyHealthEventDispatcher::Attach(newEnemy);
m_spawnList[m_numberSpawned] = newEnemy;
Game::CurrentScene->Add(newEnemy,Render::RenderLayer::ENEMY);
Game::m_quadTreev2->Insert(newEnemy);
m_numberSpawned++;
}