-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameManager.cpp
186 lines (168 loc) · 3.63 KB
/
GameManager.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
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
#include "GameManager.h"
#include "basetypes.h"
#include "SpriteManager.h"
#include "Sprite.h"
#include "game.h"
#include <assert.h>
#include <windows.h>
#include <time.h>
GameManager* GameManager::sInstance = nullptr;
const float_t GameManager::timeForReload = 0.7f;
GameManager* GameManager::createInstance()
{
if (sInstance == nullptr)
{
sInstance = new GameManager();
}
return sInstance;
}
void GameManager::init()
{
srand(time(NULL));
gameEnd = FALSE;
numberOfBulletsFiredA = 0;
numberOfBulletsFiredB = 0;
reloadRequiredA = FALSE;
reloadRequiredB = FALSE;
timeSpentReloadingA = 0.0f;
timeSpentReloadingB = 0.0f;
livesLeftA = 5;
livesLeftB = 5;
}
void GameManager::update(DWORD deltaTime)
{
reloadTurrets(deltaTime);
resetBullets();
displayGameEndScreen();
}
uint8_t GameManager::getNumberOfBulletsFired(uint8_t character)
{
assert(character == 'A' || character == 'B');
if (character == 'A')
{
return numberOfBulletsFiredA;
}
else if (character == 'B')
{
return numberOfBulletsFiredB;
}
}
void GameManager::incrementNumberOfBulletsFired(uint8_t character)
{
assert(character == 'A' || character == 'B');
if (character == 'A')
{
numberOfBulletsFiredA++;
reloadRequiredA = TRUE;
// If turret A is empty, reset the counter for new set of bullets
if (numberOfBulletsFiredA == 3) numberOfBulletsFiredA = 0;
}
else if (character == 'B')
{
numberOfBulletsFiredB++;
reloadRequiredB = TRUE;
// If turret B is empty, reset the counter for new set of bullets
if (numberOfBulletsFiredB == 3) numberOfBulletsFiredB = 0;
}
}
bool8_t GameManager::getReloadRequired(uint8_t character)
{
assert(character == 'A' || character == 'B');
if (character == 'A')
{
return reloadRequiredA;
}
else if (character == 'B')
{
return reloadRequiredB;
}
}
void GameManager::reloadTurrets(DWORD deltaTime)
{
if (reloadRequiredA)
{
timeSpentReloadingA += (deltaTime / 1000.0f);
if (timeSpentReloadingA > timeForReload)
{
timeSpentReloadingA = 0.0f;
reloadRequiredA = FALSE;
}
}
if (reloadRequiredB)
{
timeSpentReloadingB += (deltaTime / 1000.0f);
if (timeSpentReloadingB > timeForReload)
{
timeSpentReloadingB = 0.0f;
reloadRequiredB = FALSE;
}
}
}
void GameManager::resetBullets()
{
Sprite** spritePtrs = SpriteManager::getInstance()->getSpritePtrs();
for (uint8_t i = SpriteManager::SpriteName::BULLET_A_1; i <= SpriteManager::SpriteName::BULLET_B_3; i++)
{
if (spritePtrs[i]->getPosition()->y > CGame::mScreenHeight)
{
resetBullet(spritePtrs[i], i);
}
}
}
void GameManager::resetBullet(Sprite* bulletSprite, uint8_t position)
{
if (position <= SpriteManager::SpriteName::BULLET_A_3)
{
bulletSprite->setVelocity(0, 0);
bulletSprite->setPosition(124, 47);
}
else
{
bulletSprite->setVelocity(0, 0);
bulletSprite->setPosition(890, 47);
}
}
void GameManager::decrementLife(char8_t player)
{
assert(player == 'A' || player == 'B');
if (gameEnd)
{
return;
}
Sprite** spritePtrs = SpriteManager::getInstance()->getSpritePtrs();
if (player == 'A')
{
livesLeftA--;
spritePtrs[SpriteManager::SpriteName::LIFE_A_1 + livesLeftA]->disableSprite();
if (livesLeftA == 0)
{
gameEnd = TRUE;
}
}
if (player == 'B')
{
livesLeftB--;
spritePtrs[SpriteManager::SpriteName::LIFE_B_1 + livesLeftB]->disableSprite();
if (livesLeftB == 0)
{
gameEnd = TRUE;
}
}
}
bool8_t GameManager::getGameEnd()
{
return gameEnd;
}
void GameManager::displayGameEndScreen()
{
if (!gameEnd)
{
return;
}
Sprite** spritePtrs = SpriteManager::getInstance()->getSpritePtrs();
spritePtrs[SpriteManager::SpriteName::GAME_END]->enableSprite();
}
void GameManager::resetGameEnd()
{
gameEnd = FALSE;
}