-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfield.cpp
More file actions
178 lines (155 loc) · 5.88 KB
/
Copy pathfield.cpp
File metadata and controls
178 lines (155 loc) · 5.88 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
#include "field.hpp"
const int Field::field_[config::FIELD_SIZE_Y][config::FIELD_SIZE_X] = {
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1},
{1, 6, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 6, 1},
{1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,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1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}};
bool Field::canGo(const Field::Tile& tile, Field::Direction direction)
{
int type = field_[tile.y][tile.x];
switch (direction)
{
case UP:
type = field_[(tile.y - 1 + config::FIELD_SIZE_Y) % config::FIELD_SIZE_Y][tile.x];
break;
case LEFT:
type = field_[tile.y][(tile.x - 1 + config::FIELD_SIZE_X) % config::FIELD_SIZE_X];
break;
case DOWN:
type = field_[(tile.y + 1) % config::FIELD_SIZE_Y][tile.x];
break;
case RIGHT:
type = field_[tile.y][(tile.x + 1) % config::FIELD_SIZE_X];
break;
}
return (type != Tile::Type::WALL && type != Tile::Type::GHOST_ONLY);
return false;
}
QPointF Field::Tile::toPoint() const
{
return QPointF(x * config::TILE_SIZE + config::TILE_SIZE / 2, y * config::TILE_SIZE + config::TILE_SIZE / 2);
}
Field::Tile Field::toTile(const QPointF& point)
{
return {static_cast<int>(point.x()) / config::TILE_SIZE, static_cast<int>(point.y()) / config::TILE_SIZE};
}
QPointF Field::getOffset(const QPointF& point, Field::Direction direction)
{
switch (direction)
{
case UP:
if (point.y() < (-config::ENTITY_SIZE / 2))
{
return QPointF(0, config::FIELD_SIZE_Y_PX + config::ENTITY_SIZE);
}
return QPointF(0, -1);
case LEFT:
if (point.x() < (-config::ENTITY_SIZE / 2))
{
return QPointF(config::FIELD_SIZE_X_PX + config::ENTITY_SIZE, 0);
}
return QPointF(-1, 0);
case DOWN:
if (point.y() > (config::FIELD_SIZE_Y_PX + config::ENTITY_SIZE / 2))
{
return QPointF(0, -config::FIELD_SIZE_Y_PX - config::ENTITY_SIZE);
}
return QPointF(0, 1);
case RIGHT:
if (point.x() > (config::FIELD_SIZE_X_PX + config::ENTITY_SIZE / 2))
{
return QPointF(-config::FIELD_SIZE_X_PX - config::ENTITY_SIZE, 0);
}
return QPointF(1, 0);
}
return QPointF(0, 0);
}
bool Field::isVertiacl(Field::Direction direction)
{
if (direction == UP || direction == DOWN)
{
return true;
}
return false;
}
bool Field::isHorizontal(Field::Direction direction)
{
return !isVertiacl(direction);
}
Field::Direction Field::opposite(Field::Direction direction)
{
switch (direction)
{
case UP:
return DOWN;
case LEFT:
return RIGHT;
case DOWN:
return UP;
case RIGHT:
return LEFT;
}
return UP;
}
bool Field::onField(const QPointF& point)
{
return point.x() > 0 && point.x() < config::FIELD_SIZE_X_PX && point.y() > 0 && point.y() < config::FIELD_SIZE_Y_PX;
}
int Field::Tile::distSqr(const Field::Tile& rhs) const
{
return (x - rhs.x) * (x - rhs.x) + (y - rhs.y) * (y - rhs.y);
}
Field::Tile Field::Tile::nextTile(Field::Direction direction, int dist) const
{
switch (direction)
{
case UP:
return {x, y - dist};
case LEFT:
return {x - dist, y};
case DOWN:
return {x, y + dist};
case RIGHT:
return {x + dist, y};
}
return *this;
}
Field::Tile::Type Field::Tile::getType() const
{
return static_cast<Type>(field_[y][x]);
}
bool Field::Tile::operator==(const Field::Tile& rhs) const
{
return (x == rhs.x) && (y == rhs.y);
}
bool Field::Tile::operator!=(const Field::Tile& rhs) const
{
return !(*this == rhs);
}