也许完成了寻路

This commit is contained in:
TurnMeOn committed 2017-12-24 21:47:03 +08:00
1 parent 640c7b182c
commit 52364ff0ef
4 files changed
+69 -4

No files matched your search

+38 -1
View File
@@ -1,7 +1,10 @@
#include "car.h"
Car::Car(QGraphicsItem *parent, int dir, Car::Color color) : QGraphicsPixmapItem(parent)
Car::Car(QGraphicsItem *parent, int dir, Car::Color color) :
QGraphicsPixmapItem(parent),
posAni(new QPropertyAnimation(this,"m_pos")),
m_target(PathPoint(this->pos(),this->rotation()))
{
m_pos = this->pos();
this->setRotation(dir);
@@ -25,6 +28,7 @@ Car::Car(QGraphicsItem *parent, int dir, Car::Color color) : QGraphicsPixmapItem
m_pic = m_pic.scaled(M_WID, M_LEN);
this->setPixmap(m_pic);
setTransformOriginPoint(M_WID/2, M_LEN/2); //设置旋转中心
connect(posAni,QPropertyAnimation::finished,this,followPath);
}
void Car::Forward(qreal vel)
@@ -75,6 +79,39 @@ void Car::turnRight(int r, double ang)
Rotate(ang);
}
void Car::setPath(Path &path)
{
m_path = path;
}
void Car::moveTo(QPointF target)
{
posAni->setDuration(1000);
posAni->setEndValue(target);
posAni->start();
}
void Car::followPath()
{
setRotation(m_target.dir);
if(m_path.isEmpty())
{
m_target = m_path.getNext();
moveTo(m_target.point);
}
}
QPointF Car::getmPos()
{
return this->pos();
}
void Car::setmPos(QPointF pos)
{
this->setPos(pos);
this->m_pos = pos;
}
Car::Color Car::getColor()
{
return this->m_color;
+23 -3
View File
@@ -6,16 +6,22 @@
#include <QString>
#include <QPoint>
#include <QtMath>
#include <QPropertyAnimation>
#include "path.h"
//车身长宽
#define M_LEN (200)
#define M_WID (130)
class Car : public QGraphicsPixmapItem
class Car : public QObject, public QGraphicsPixmapItem
{
Q_OBJECT
Q_PROPERTY(QPointF m_pos READ getmPos WRITE setmPos)
Q_PROPERTY(int m_dir READ getDir WRITE setRotation)
public:
enum Color { Pink, Red, Yellow, RANDOM };
explicit Car(QGraphicsItem *parent = Q_NULLPTR, int dir = 0, Car::Color color = RANDOM);
//基础移动
void Forward(qreal vel = 3); //前进
void Backward(qreal vel = 3); //后退
void moveLeft(qreal vel = 3); //向左平移
@@ -23,15 +29,29 @@ public:
void Rotate(qreal ang = 30); //原地旋转
void turnLeft(int r, double ang); //左转
void turnRight(int r, double ang); //右转
void setPath(Path &path);
//动画
void moveTo(QPointF target);
void followPath();
//为了Property
QPointF getmPos();
void setmPos(QPointF pos);
Color getColor();
qreal getDir();
QPropertyAnimation *posAni;
private:
Color m_color; //车身颜色 N为0, 顺时针递增
Color m_color; //车身颜色 N为0, 顺时针递增
QPixmap m_pic;
QString m_number;
QPointF m_pos;
QPointF m_pos;
qreal m_dir;
Path m_path;
PathPoint m_target;
};
+6
View File
@@ -37,3 +37,9 @@ int Path::pointsCount()
return path.length();
}
PathPoint &Path::getNext()
{
PathPoint &tmp = path.first();
path.removeFirst();
return tmp;
}
+2
View File
@@ -34,10 +34,12 @@ public:
void addPoint(const PathPoint &point);
bool isEmpty();
int pointsCount();
PathPoint &getNext();
private:
QList<PathPoint> path;
int len;
int i;
};
#endif // PATH_H