diff --git a/car.cpp b/car.cpp index 1639d28..65b361c 100644 --- a/car.cpp +++ b/car.cpp @@ -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; diff --git a/car.h b/car.h index 3d09c0d..4abb7bb 100644 --- a/car.h +++ b/car.h @@ -6,16 +6,22 @@ #include #include #include +#include +#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; }; diff --git a/path.cpp b/path.cpp index e33a09e..eed37f5 100644 --- a/path.cpp +++ b/path.cpp @@ -37,3 +37,9 @@ int Path::pointsCount() return path.length(); } +PathPoint &Path::getNext() +{ + PathPoint &tmp = path.first(); + path.removeFirst(); + return tmp; +} diff --git a/path.h b/path.h index adebc24..85b3ed8 100644 --- a/path.h +++ b/path.h @@ -34,10 +34,12 @@ public: void addPoint(const PathPoint &point); bool isEmpty(); int pointsCount(); + PathPoint &getNext(); private: QList path; int len; + int i; }; #endif // PATH_H