-
Notifications
You must be signed in to change notification settings - Fork 0
/
walls.cpp
77 lines (71 loc) · 1.64 KB
/
walls.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
#include "walls.h"
#include <QDebug>
// public methods:
Walls::Walls(QWidget *parent) :
QWidget(parent)
{
}
void Walls::render(QPainter *painter)
{
painter->setBrush(QColor(WALL_COLOR));
for(int i = 0; i < list.size(); ++i) {
QPoint p = list[i];
painter->drawRect(p.rx(), p.ry(), GRID_SIZE, GRID_SIZE);
}
}
void Walls::add(QPoint pos) {
if(list.contains(pos)) {
return;
}
list.append(pos);
emit wallAdded(pos);
}
void Walls::remove(QPoint pos) {
list.removeOne(pos);
}
void Walls::generateSurroundingWalls() {
int minX = -MAP_WIDTH / 2, maxX = -minX;
int minY = -MAP_HEIGHT / 2, maxY = -minY;
// up
for(int x = minX; x < maxX; x += GRID_SIZE) {
list.push_back(QPoint(x, minY));
}
// down
for(int x = minX; x < maxX; x += GRID_SIZE) {
list.push_back(QPoint(x, maxY - GRID_SIZE));
}
// left
for(int y = minY; y < maxY; y += GRID_SIZE) {
list.push_back(QPoint(minX, y));
}
// right
for(int y = minY; y < maxY; y += GRID_SIZE) {
list.push_back(QPoint(maxX - GRID_SIZE, y));
}
}
Walls::~Walls()
{
}
// slots:
void Walls::checkHit(int snakeId, const QPoint& head) {
if(list.contains(head)) {
emit hitWall(snakeId);
}
}
void Walls::checkOverwrite(const QPoint& p) {
if(list.contains(p)) {
emit overwritten(p);
}
}
void Walls::handleOverwritten(const QPoint& p) {
list.removeOne(p);
}
// friends:
QDataStream& operator<<(QDataStream& out, const Walls& walls) {
out<<walls.list;
return out;
}
QDataStream& operator>>(QDataStream& in, Walls& walls) {
in>>walls.list;
return in;
}