-
Notifications
You must be signed in to change notification settings - Fork 2
/
robot.h
195 lines (171 loc) · 5.42 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
/*
* 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 <QLineF>
#include <QList>
#include "robotbase.h"
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 RobotBase
{
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 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 Clear the detect list data.
*/
void resetDetectList();
/*!
* \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();
}
/*!
* \brief This will return the detected list is empty or not. It's used to
* reset the robot.
* \return If the list is empty, return true.
*/
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;
}
};
//Robot list information.
QList<Robot *> m_detectedRobotList;
//Gardian line information
bool m_hasGuardianLine;
QLineF m_guardianLine, m_oppositeGuardianLine;
qreal m_toP1Distance, m_movingSpeed;
};
#endif // ROBOT_H