-
Notifications
You must be signed in to change notification settings - Fork 0
/
route.cpp
183 lines (163 loc) · 4.27 KB
/
route.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
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
#include "route.h"
#include "gameobject.h"
#include <cmath>
#include <QRect>
#include <iostream>
Route::Route(class GameObject* object, Path path, QPoint end)
{
parent = object;
this->path =path;
this->start = parent->getPoint();
this->end = end;
this->position = start;
theEnd = false;
q=M_PI/2;
}
Route::Route(class GameObject* object, Path path)
{
parent = object;
this->path =path;
this->start = parent->getPoint();
this->end = QPoint(-1000,-1000);
this->position = start;
this->path =path;
theEnd = false;
q=M_PI/2;
}
Route::Path Route::getRoutePath(){
return path;
}
void Route::setStart(){
start = parent->getPoint();
position = start;
theEnd = false;
if(path == Path::Stay){
end = position;
}
}
QPoint Route::getNextPoint(double speed){
if(theEnd){
return end;
}
if(path != Path::Stay && QRect(end - QPoint(8,8),end+QPoint(8,8)).intersects(QRect(position - QPoint(8,8),position+QPoint(8,8)))){
theEnd = true;
position = end;
return end;
}else{
if(path != Path::Lemniscate && path != Path::Stay){
setStart();
}
}
if(path == Path::Left){
dx = -speed;
dy = 0;
}
if(path == Path::Right){
dx = speed;
dy = 0;
}
if(path == Path::None){
dx = 0;
dy = 0;
}
if(path == Path::Top){
dx = 0;
dy = -speed;
}
if(path == Path::Bottom){
dx = 0;
dy = speed;
}
if(path== Path::Line){
double length = sqrt((end.x() - start.x())*(end.x() - start.x())+(end.y() - start.y())*(end.y() - start.y()));
dx = speed*(end.x() - start.x())/length;
dy = speed*(end.y() - start.y())/length;
}
if(path== Path::Stay){
if(std::abs(end.x()-position.x()) > 5){
dx = dx + 1;
}else{
dx = dx - 1;
}
}
if(path == Path::Sin){
q = q + 0.4;
double t = q;
double length = sqrt((end.x() - start.x())*(end.x() - start.x())+(end.y() - start.y())*(end.y() - start.y()));
dx = speed*(end.x() - start.x())/length+ 4*cos(t);
dy = speed*(end.y() - start.y())/length + 4*sin(t)*sin(t);
}
if(path == Path::Lemniscate){
q = q + 0.04;
double t = q;
dx = speed/4*sqrt(2)*cos(t);
dy = speed*sqrt(2)*cos(t)*sin(t);
if(q > M_PI/2 and q < M_PI){
dy = -dy;
}
if(q > M_PI and q < 3*M_PI/2){
dy = -dy;
}
if(q > 3*M_PI/2 and q < 2*M_PI){
}
if(q > 2.5*M_PI){
theEnd = true;
end = position;
}
}
if(path == Path::HalfCircleRightLeft){
q = q + 0.04;
double t = q;
dx = speed/4*sqrt(2)*cos(t);
dy = speed*sqrt(2)*cos(t)*sin(t);
if(q > M_PI/2 and q < M_PI){
dy = -dy;
}
if(q > M_PI and q < 3*M_PI/2){
dy = -dy;
}
if(q > 3*M_PI/2 and q < 2*M_PI){
theEnd = true;
end = position;
}
}
position = QPoint(position.x()+qRound(dx),position.y()+qRound(dy));
return position;
}
bool Route::isEnded(){
return theEnd;
}
void Route::setTheEnd(bool tf){
theEnd = tf;
}
void Route::read(const QJsonObject &json)
{
std::cout <<"Route::read1"<<std::endl;
path = Route::Path(json["path"].toInt());
theEnd = json["theEnd"].toBool();
QJsonObject startObject = json["start"].toObject();
QJsonObject endObject = json["end"].toObject();
QJsonObject positionObject = json["position"].toObject();
start.setX(startObject.value("x").toInt());
start.setY(startObject.value("y").toInt());
end.setX(endObject.value("x").toInt());
end.setY(endObject.value("y").toInt());
// position.setX(positionObject.value("x").toInt());
// position.setY(positionObject.value("y").toInt());
std::cout <<"Route::read2"<<std::endl;
}
void Route::write(QJsonObject &json) const
{
json["path"] = static_cast<int>(path);
json["theEnd"] = false;
QJsonObject startObject, endObject, positionObject;
startObject.insert("x",start.x());
startObject.insert("y",start.y());
endObject.insert("x",end.x());
endObject.insert("y",end.y());
// positionObject.insert("x",position.x());
// positionObject.insert("y",position.y());
json["start"] = startObject;
json["end"] = endObject;
json["position"] = positionObject;
}