-
Notifications
You must be signed in to change notification settings - Fork 1
/
autofindpath.cpp
118 lines (95 loc) · 2.67 KB
/
autofindpath.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
#include "autofindpath.h"
#include <QtMath>
#include <QDebug>
#include <QGraphicsScene>
#include <QGraphicsView>
QMap<Role*,AutoFindPath*> AutoFindPath::rolePathMap_;
AutoFindPath::AutoFindPath(QObject *parent) : QObject(parent)
{
pace_ = 5;
radius_ = pace_ - 1;
timer_.setInterval(40);
connect(&timer_, SIGNAL(timeout()), this, SLOT(onTimeout()));
}
AutoFindPath::~AutoFindPath()
{
}
void AutoFindPath::setRange(QPointF from, QPointF to)
{
fromPointF_ = from;
toPointF_ = to;
}
void AutoFindPath::setRole(Role *role)
{
role_ = role;
}
void AutoFindPath::start()
{
tempPointF_ = fromPointF_;
timer_.start();
}
void AutoFindPath::stop()
{
timer_.stop();
if (rolePathMap_.contains(role_)) {
rolePathMap_.remove(role_);
}
this->deleteLater();
}
void AutoFindPath::FindPath(Role *role, QPointF from, QPointF to)
{
if (rolePathMap_.contains(role)) {
AutoFindPath* p = rolePathMap_.value(role);
p->stop();
p->deleteLater();
rolePathMap_.remove(role);
}
AutoFindPath * path = new AutoFindPath;
rolePathMap_.insert(role,path);
path->setRange(from, to);
path->setRole(role);
path->start();
}
void AutoFindPath::onTimeout()
{
qreal x1 = tempPointF_.x();
qreal y1 = tempPointF_.y();
qreal x2 = toPointF_.x();
qreal y2 = toPointF_.y();
qreal xNegative = (x2 - x1) > 0 ? 1.0 : -1.0;
qreal yNegative = (y2 - y1) > 0 ? 1.0 : -1.0;
xNegative = 1.0;
yNegative = 1.0;
qreal fromDistance = qSqrt(qPow(x2-x1, 2) + qPow(y2-y1, 2));
qreal xdif = xNegative * pace_ * (x2 - x1) / fromDistance;
qreal ydif = yNegative * pace_ * (y2 - y1) / fromDistance;
qreal x3 = x1 + xdif;
qreal y3 = y1 + ydif;
qreal toDistance = qSqrt(qPow(x3-x2, 2) + qPow(y3-y2, 2));
// qDebug() << tempPointF_ << toPointF_<< xNegative << yNegative<<fromDistance<<toDistance<< xdif << ydif;
tempPointF_.setX(x3);
tempPointF_.setY(y3);
if (toDistance <= radius_) {
// qDebug() << "STOP!";
timer_.stop();
if (rolePathMap_.contains(role_)) {
rolePathMap_.remove(role_);
}
this->deleteLater();
}
qreal ration = qAtan2(ydif, xdif);
qreal degree = qRadiansToDegrees(ration);
// qDebug() << ration << degree;
role_->setRotation(degree + 90);
// setRotation(rotation() + dx);
role_->setPos(tempPointF_);
// role_->setEyeDirection();
// role_->update();
// QRectF rect = role_->mapRectToScene(role_->boundingRect());
QPointF pos = role_->mapToScene(role_->pos());
QRectF rect;
rect.setTopLeft(pos);
rect.setWidth(20);
rect.setHeight(20);
role_->scene()->invalidate();
}