-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScene.cpp
More file actions
211 lines (192 loc) · 6.7 KB
/
Scene.cpp
File metadata and controls
211 lines (192 loc) · 6.7 KB
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
202
203
204
205
206
207
208
209
210
211
#include "Scene.hpp"
#include "CellOccupier.hpp"
#include "SnakeException.hpp"
#include <algorithm>
const QBrush Scene::FOOD_BRUSH = QBrush(Qt::yellow);
const QBrush Scene::WALL_BRUSH = QBrush(Qt::blue);
const QBrush Scene::DEAD_BRUSH = QBrush(Qt::red);
const QBrush Scene::PLAYER_BRUSHES[2] = {QBrush(Qt::green), QBrush(Qt::cyan)};
Scene::Scene(shared_ptr<Board> board, shared_ptr<Rules> rules)
: m_transform(), m_last_objects(), m_directions(),
m_timer(new QTimer(this)){
if (rules->get_player_count() > MAX_PLAYERS){
throw TooManyPlayersError();
}
m_board = board;
m_rules = rules;
m_playing = false;
m_directions.resize(m_rules->get_player_count());
reset_directions();
view.resize(m_board->get_width()*CellObject::get_width(), m_board->get_height()*CellObject::get_height());
view.setAlignment(Qt::AlignTop | Qt::AlignLeft);
view.setScene(this);
view.setBackgroundBrush(Qt::black);
view.setWindowTitle("Sarah's Amazing Snake Game");
connect(m_timer.get(), SIGNAL(timeout()), this, SLOT(move_snake()));
display_walls();
update_view();
view.show();
}
void Scene::reset_directions(){
for (int player = 0; player < m_rules->get_player_count(); ++player){
m_directions[player] = (m_rules->get_snake(player))->get_direction();
}
}
void Scene::move_snake(){
bool result = true;
for (int player = 0; player < m_rules->get_player_count(); ++player){
result &= m_rules->move_snake(player, m_directions[player]);
}
if(!result){
end_game();
}
update_view();
}
void Scene::end_game(){
m_timer->stop();
m_playing = false;
}
void Scene::keyPressEvent(QKeyEvent* event){
//TODO: Change this to "dispatch handler", basically a map of key presses
// to methods.
// if(m_playing){
// playing_handler(key);
// } else
// not_playing_handler(key);
// Playing handles handle key
// handlekey(key){
// handle = my_map.find(key);
// if (!handler){
// handle(key);
// }
// }
// if (parentDispatch != NULL)
// parentDispatch->handleyKey(key);
// Gives mappable keyboards.
if (event->key() == Qt::Key_R && !m_playing){
if(m_rules->snake_dead()){
m_rules->reset();
reset_directions();
update_view();
}
}
if (event->key() == Qt::Key_Space && !m_playing){
const Snake* snake = m_rules->get_snake(0);
m_timer->start(100*snake->get_speed());
m_playing = true;
} else{
if (m_playing){
switch(event->key()){
case Qt::Key_Up:
m_directions[0] = Vector::UP;
break;
case Qt::Key_Down:
m_directions[0] = Vector::DOWN;
break;
case Qt::Key_Left:
m_directions[0] = Vector::LEFT;
break;
case Qt::Key_Right:
m_directions[0] = Vector::RIGHT;
break;
case Qt::Key_W:
m_directions[1] = Vector::UP;
break;
case Qt::Key_S:
m_directions[1] = Vector::DOWN;
break;
case Qt::Key_A:
m_directions[1] = Vector::LEFT;
break;
case Qt::Key_D:
m_directions[1] = Vector::RIGHT;
break;
default:
break;
}
QGraphicsScene::keyPressEvent(event);
}
}
}
int Scene::map_to_view(int x, int size){
return x*size;
}
Coord Scene::get_scene_coord(const CellOccupier* occupier){
Coord coord = m_board->find(occupier);
int x = map_to_view(coord.get_x(), CellObject::get_width());
int y = map_to_view(coord.get_y(), CellObject::get_height());
return Coord(x,y);
}
QGraphicsItem* Scene::find_item(Coord coord){
return itemAt(coord.get_x(), coord.get_y(), m_transform);
}
void Scene::add_object(QGraphicsItem* obj, set<QGraphicsItem*>* new_objects){
addItem(obj);
if (new_objects){
new_objects->insert(obj);
}
}
CellObject* Scene::create_new_cell_object(Coord coord, QBrush brush){
CellObject* obj = new CellObject(coord);
obj->setBrush(brush);
return obj;
}
void Scene::update_view(){
set<QGraphicsItem*> new_objects;
int players = m_rules->get_player_count();
for (int player = 0; player < players; ++player){
const Snake* snake = m_rules->get_snake(player);
bool dead = !snake->is_alive();
for (SnakeIterator itr = snake->begin(); itr != snake->end(); ++itr){
Coord coord = get_scene_coord(*itr);
QGraphicsItem* item = find_item(coord);
if (!item){
if (dead){
add_object(create_new_cell_object(coord, DEAD_BRUSH));
} else {
add_object(create_new_cell_object(coord, PLAYER_BRUSHES[player]), &new_objects);
}
} else {
CellObject* obj;
if((obj = dynamic_cast<CellObject *>(item))){
if (dead){
obj->setBrush(DEAD_BRUSH);
} else{
obj->setBrush(PLAYER_BRUSHES[player]);
}
new_objects.insert(item);
}
}
}
}
Coord coord = get_scene_coord(m_rules->get_food());
QGraphicsItem* item = find_item(coord);
if(!item){
add_object(create_new_cell_object(coord, FOOD_BRUSH), &new_objects);
} else{
new_objects.insert(item);
}
set<QGraphicsItem*> result;
set_difference(m_last_objects.begin(), m_last_objects.end(),
new_objects.begin(), new_objects.end(), std::inserter(result, result.end()));
for (set<QGraphicsItem*>::iterator itr = result.begin(); itr != result.end(); ++itr){
removeItem(*itr);
delete *itr;
}
m_last_objects = new_objects;
}
void Scene::display_walls(){
vector<CellOccupier*> walls = m_rules->get_walls();
for(vector<CellOccupier*>::iterator wall = walls.begin(); wall != walls.end(); ++wall ){
vector<Coord> coords = m_board->find_all(*wall);
for (vector<Coord>::iterator itr = coords.begin(); itr != coords.end(); ++itr){
int x = map_to_view((*itr).get_x(), CellObject::get_width());
int y = map_to_view((*itr).get_y(), CellObject::get_height());
Coord coord = Coord(x,y);
QGraphicsItem* item = find_item(coord);
if(!item){
add_object(create_new_cell_object(coord, WALL_BRUSH));
}
}
}
}