forked from Harinlen/Robot-Emulator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
robot.h
295 lines (255 loc) · 8.18 KB
/
robot.h
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/*
* Copyright (C) Kreogist Dev Team
*
* 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 2
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifndef ROBOT_H
#define ROBOT_H
#include <QPointF>
#include <QLineF>
#include <QList>
#include <QColor>
class QPainter;
/*!
* \brief The Robot class contains all the information and description of a
* robot, it contains the position, moving speed and direction of a robot. \n
* You can set the start position of a robot at any position you want. \n
* You have to recover the memory yourself. It don't have a parent. \n
* This is not a QObject for memory reduce, so there's no signal and slots.
*/
class Robot
{
public:
/*!
* \brief Construct the robot. The start position of this type of construct
* will be QPointF(0, 0);
*/
Robot();
/*!
* \brief Construct the robot with the give position.
* \param pos The start position of the robot.
*/
Robot(QPointF pos);
/*!
* \brief Construct the robot with the give position.
* \param x The start x position of the robot.
* \param y The start y position of the robot.
*/
Robot(qreal x, qreal y);
/*!
* \brief The position of the robot.
* \return The QPointF format robot position.
*/
QPointF pos() const;
/*!
* \brief Set the position of a robot.
* \param pos The position of the robot.
*/
void setPos(const QPointF &pos);
/*!
* \brief This is an overloaded function.\n
* Set the position of a robot.
* \param x The x position of the robot.
* \param y The y position of the robot.
*/
inline void setPos(qreal x, qreal y)
{
setPos(QPointF(x, y));
}
/*!
* \brief Paint the robot with the specific painter.
* \param painter
*/
void paintRobot(QPainter *painter);
/*!
* \brief Get the size of all the robots.
* \return The size of all robots.
*/
static int robotSize();
/*!
* \brief Change the size of all the robots.
* \param robotSize The new size of all robots.
*/
static void setRobotSize(int robotSize);
/*!
* \brief Get the detect radius of all the robots.
* \return The detect radius of all robots.
*/
static int detectRadius();
/*!
* \brief Change all the detect radius of all the robots.
* \param detectRadius The new detect radius of all the robots.
*/
static void setDetectRadius(int detectRadius);
/*!
* \brief Sets the color of the robot.
* \param robotColor The prefer color of all robots.
*/
static void setRobotColor(const QColor &robotColor);
/*!
* \brief Get the color of all robots.
* \return The QColor of the robots.
*/
static QColor robotColor();
/*!
* \brief Get the color of the detection radius border of the robots.
* \return The QColor of the detection radius border of the robots.
*/
static QColor detectRadiusColor();
/*!
* \brief Change the color of the robot detection radius border.
* \param robotColor The prefer color of all the detection radius border of
* robots.
*/
static void setDetectRadiusColor(const QColor &detectRadiusColor);
/*!
* \brief Get the moving angle of the robot. \n
* The default angle of the robot is 0 (3'o clock position).
* \return The angle of the robot.
*/
qreal angle() const;
/*!
* \brief Change the moving angle of the robot, the value should be ranged
* from 0 degrees to 360 degrees. The program will automatically change the
* value.
* \param angle The prefer angle of the robot.
*/
void setAngle(const qreal &angle);
/*!
* \brief Get the color the all the direction line color of the robots.
* \return The color of the direction line.
*/
static QColor directionLineColor();
/*!
* \brief Change the color of the direction line.
* \param directionLineColor The direction line color.
*/
static void setDirectionLineColor(const QColor &directionLineColor);
/*!
* \brief Add a detected robot to the detection list. \n
* If the robot has been in the detect list, it won't add it twice.
* \param robot The robot which has been detected.
*/
void addToDetectList(Robot *robot);
/*!
* \brief Remove one robot from the detect list.
* If the robot is not in the list, nothing will happened.
* \param robot The robot which should be removed.
*/
void removeFromDetectList(Robot *robot);
/*!
* \brief Call this function will make the robot move 1 pixel length to the
* direction.
*/
void moveOneStep();
/*!
* \brief Get the next step of the robot is going to be.
* \return The next step position.
*/
QPointF nextStep() const;
/*!
* \brief Update the direction of the robot according to the detected list.
*/
void updateDirection();
/*!
* \brief Get whether the robot has got a line to guard, each robot should
* guard one line.
* \return If the robot has a line to guard, return true, or else false.
*/
bool hasGuardianLine() const;
/*!
* \brief Set a guardian line of to the robot. If the lenght of the line is
* 0, will remove the guardian state of the robot.
* \param line The guardian line of the robot.
*/
void setGuardianLine(const QLineF &line, const QPointF &footPoint);
/*!
* \brief Clear the guardian line state data.
*/
void resetGuardianLine();
/*!
* \brief This function only available when there's a guardian line.\n
* Move to the opposite direction of the line.
*/
void moveToOppositeDirection();
/*!
* \brief The moving speed is only available when the robot got a guardian
* line, return the moving speed.\n
* This vale should be whether 1.0 or -1.0.
* \return The moving speed on a guardian line.
*/
qreal movingSpeed() const
{
return m_movingSpeed;
}
/*!
* \brief This function is only available when the robot has a guardian
* line. It's a parameter for changing the direction.
* \return The robot distance to the p1 point of the guardian line.
*/
qreal toP1Distance() const
{
return m_toP1Distance;
}
/*!
* \brief Get the guardian line of the current robot.
* \return The guardian line in QLineF format.
*/
QLineF guardianLine() const
{
return m_guardianLine;
}
/*!
* \brief Calculate the distance of two point.
* \param p1 The first point.
* \param p2 The second point.
* \return The distance of two point.
*/
static qreal pointDistance(const QPointF p1, const QPointF p2)
{
return QLineF(p1, p2).length();
}
bool isDetectedListEmpty()
{
return m_detectedRobotList.isEmpty();
}
private:
struct RobotStatus
{
Robot *robot;
qreal distance;
RobotStatus():
robot(nullptr),
distance(0.0)
{
;
}
friend bool operator < (const RobotStatus &status1,
const RobotStatus &status2)
{
return status1.distance < status2.distance;
}
};
static int m_robotSize, m_detectRadius;
QPointF m_pos;
QList<Robot *> m_detectedRobotList;
static QColor m_robotColor, m_detectRadiusColor, m_directionLineColor;
qreal m_angle;
//Gardian line information
bool m_hasGuardianLine;
QLineF m_guardianLine, m_oppositeGuardianLine;
qreal m_toP1Distance, m_movingSpeed;
};
#endif // ROBOT_H