-
Notifications
You must be signed in to change notification settings - Fork 2
/
Controller.cpp
157 lines (144 loc) · 5.19 KB
/
Controller.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
/*
* PythonBot - A game by a developer for developers.
* Copyright (C) 2015-2021 Jean-Marie BARAN ([email protected])
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Refer to 'LICENSE.txt' for the full notice.
*/
#include "Controller.h"
Controller::Controller(Model &model, View &view, bool startPaused)
: m_startPaused(startPaused), m_model(model), m_view(view) {}
void Controller::loop() {
bool gameFinished(false), paused(m_startPaused);
while (!gameFinished) {
// Deal with all events.
switch (m_view.read_events()) {
case View::REQUEST_QUIT: // Request to quit the game.
gameFinished = true;
break;
case View::REQUEST_PAUSE: // Request to pause/unpause the game.
paused = !paused;
break;
default: // No request made.
break;
}
// Draw the scene.
m_view.draw(m_model.get_circles());
m_view.draw(m_model.get_dead_bots());
m_view.draw(m_model.get_alive_bots());
m_view.draw(m_model.get_bullets());
m_view.draw(m_model.get_scans());
if (paused)
m_view.draw_pause();
m_view.render();
if (!paused) {
// Manage bullets.
update_bullets();
// Manage bots.
update_bots();
}
}
}
void Controller::update_bullets() {
Bullet *bullet(0);
Bot *bot(0);
sf::Vector2f hitPoint;
for (unsigned int i(0); i < m_model.get_bullets().size(); i++) {
// Get a direct pointer for convenience.
bullet = (Bullet *)m_model.get_bullets()[i];
if (m_model.collides(bullet, hitPoint)) {
// Collision with circles and world' boundaries.
m_view.add_explosion(hitPoint);
m_model.erase_bullet(i);
} else if (m_model.collides(bullet, &bot, hitPoint)) {
// Collision with bots.
m_view.add_explosion(hitPoint);
m_model.inflict_damages(bot);
if (!bot->is_alive()) {
bullet->get_bot_origin()->add_kill();
m_view.add_bot_explosion(bot->get_position());
}
m_model.erase_bullet(i);
} else {
// Update position.
bullet->move();
}
}
}
void Controller::update_bots() {
// Loop over all the bots.
Bot *bot(0);
sf::Vector2f hitPoint;
// See bot death below for the cast.
for (int i(0); i < (int)m_model.get_alive_bots().size(); i++) {
// Get a direct pointer for convenience.
bot = (Bot *)m_model.get_alive_bots()[i];
// Get and manage the request of each bot.
BotConnector::Request const &request(bot->get_connector().get_request());
unsigned int circlesCount(0), botsCount(0),
bulletsCount(0); // Results for a scan.
switch (request.type) {
case BotConnector::SKIP: // The bot asked to skip its turn.
bot->get_connector().answer_request(
bot->get_health(), bot->get_bullets_left(), bot->get_num_kills());
break;
case BotConnector::MOVE: // The bot asked to move forward.
bot->move();
bot->get_connector().answer_request(
bot->get_health(), bot->get_bullets_left(), bot->get_num_kills());
break;
case BotConnector::ROTATE: // The bot asked to be rotated.
bot->rotate(request.rotationDegrees);
bot->get_connector().answer_request(
bot->get_health(), bot->get_bullets_left(), bot->get_num_kills());
break;
case BotConnector::FIRE: // The bot asked to fire a bullet.
if (bot->get_bullets_left() > 0)
m_model.fire_bullet(bot);
bot->get_connector().answer_request(
bot->get_health(), bot->get_bullets_left(), bot->get_num_kills());
break;
case BotConnector::SCAN: // The bot asked a scan.
bot->set_scan(request.scanDistance, request.scanRadius);
m_model.scan(bot, request.scanCircles, circlesCount, request.scanBots,
botsCount, request.scanBullets, bulletsCount);
bot->get_connector().answer_scan_request(
bot->get_health(), bot->get_bullets_left(), bot->get_num_kills(),
circlesCount, botsCount, bulletsCount);
break;
default: // The bot did not ask for anything.
break;
}
// Manage collisions.
Bot *botCollision(0);
if (m_model.collides(bot, &botCollision, hitPoint)) {
m_view.add_explosion(hitPoint);
m_model.inflict_damages(bot);
if (!bot->is_alive()) {
if (botCollision != 0) {
/* If the bot collides with another bot and not the world, add a kill
* to this other bot. */
botCollision->add_kill();
}
m_view.add_bot_explosion(bot->get_position());
/* If the bot has been removed from the vector (dead), the next bot will
* be on the current index. */
i--;
}
}
// Load new bullets.
bot->load_bullet();
}
}