-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameMonitor.cpp
122 lines (99 loc) · 2.53 KB
/
GameMonitor.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
#include "GameMonitor.h"
#include <algorithm>
//#include <cmath>
#include <string>
void GameMonitor::update(long delta)
{
if (state == State::Hanging) {
hangingTime += delta;
if (hangingTime >= STAGE_HANGING_TIME) {
resume();
}
return;
}
updateTimer(delta);
checkFarmerSight();
checkAbductionBeam();
}
void GameMonitor::reset()
{
score = 0;
lives = PLAYER_STARTING_LIVES;
timer = TIMER_STARTING_TIME;
}
void GameMonitor::updateTimer(long delta)
{
sumTimer += delta;
if (sumTimer >= 1000) {
sumTimer %= 1000;
if (--timer <= 0)
killPlayer();
}
}
void GameMonitor::checkFarmerSight()
{
if (stage->enemy->spottedUFO())
killPlayer();
}
void GameMonitor::checkAbductionBeam()
{
if (!stage->player->readyForAbduction()) return;
// searches animals list for abductable animal
std::vector<Animal*> animals = stage->animals->list();
for (std::vector<Animal*>::iterator it = animals.begin(); it != animals.end(); ++it) {
if (abs(((*it)->center().X - stage->player->center().X)) <= PLAYER_ABDUCT_RADIUS) {
stage->animals->erase((*it));
timer = min(timer + TIMER_BONUS_PER_ANIMAL, TIMER_MAX_TIME);
score = min(score + SCORE_BONUS_PER_ANIMAL, SCORE_MAX_SCORE);
}
}
}
void GameMonitor::killPlayer()
{
state = State::Hanging;
lives--;
}
void GameMonitor::resume()
{
state = State::Running;
hangingTime = 0;
if (lives <= 0) {
stage->gameover();
}
else {
timer = TIMER_STARTING_TIME;
stage->setup();
}
}
COORD GameMonitor::getPlayerCenter()
{
return stage->player->center();
}
bool GameMonitor::isPlayerHidden()
{
return stage->player->hidden();
}
/**
* Displays the HUD
*/
void GameMonitor::paint()
{
ConsoleBuffer& buffer = ConsoleBuffer::getInstance();
// pads and writes the score
std::string score = std::to_string(this->score);
buffer.write(7 - score.length(), 1, score, HUD_FG_COLOR_ON, HUD_BG_COLOR);
// prints scores's "turned off" leading zeros
for (unsigned i = 0; i < 5 - score.length(); i++)
buffer.write(2 + i, 1, "0", HUD_FG_COLOR_OFF, HUD_BG_COLOR);
// pads and writes the timer
std::string timer = std::to_string(this->timer);
buffer.write(40 - timer.length(), 1, timer, HUD_FG_COLOR_ON, HUD_BG_COLOR);
// prints timer's "turned off" leading zero
if (this->timer < 10)
buffer.write(38, 1, "0", HUD_FG_COLOR_OFF, HUD_BG_COLOR);
// displays player's life icons
for (short i = 0; i < PLAYER_STARTING_LIVES; i++) {
int color = i < lives ? HUD_FG_COLOR_ON : Color::GRAY;
buffer.write(SCREEN_WIDTH - 7 + (2 * i), 1, std::string(1, PLAYER_LIFE_ICON_CODE), color, HUD_BG_COLOR);
}
}