From 517e3081a8010b8de52832cff23ad82a86646e49 Mon Sep 17 00:00:00 2001 From: w-mj Date: Mon, 25 Dec 2017 10:11:15 +0800 Subject: [PATCH] add car & request in --- car.cpp | 19 ++++++++++++++++++- car.h | 10 +++++++--- monitor.cpp | 2 +- parkingLotManager.cpp | 29 +++++++++++++++++------------ parkingLotManager.h | 3 +++ path.cpp | 12 ++++++++++-- path.h | 14 +++++++++++--- 7 files changed, 67 insertions(+), 22 deletions(-) diff --git a/car.cpp b/car.cpp index b04e8e3..454b53c 100644 --- a/car.cpp +++ b/car.cpp @@ -1,7 +1,7 @@ #include "car.h" -Car::Car(QGraphicsItem *parent, int dir, Car::Color color) : +Car::Car(ParkingLotManager * manager, QGraphicsItem *parent, int dir, Car::Color color) : QObject(0), QGraphicsPixmapItem(parent), posAni(new QPropertyAnimation(this,"m_pos")), @@ -31,6 +31,8 @@ Car::Car(QGraphicsItem *parent, int dir, Car::Color color) : this->setPixmap(m_pic); setTransformOriginPoint(M_WID/2, M_LEN/2); //设置旋转中心 connect(posAni,QPropertyAnimation::finished,this,followPath); + connect(this, &Car::queueHead, manager, &ParkingLotManager::requestIn); + connect(this, &Car::exit, manager, &ParkingLotManager::leave); } void Car::Forward(qreal vel) @@ -160,3 +162,18 @@ qreal Car::getDir() { return this->rotation(); } + +void Car::setEntryNum(int value) +{ + entryNum = value; +} + +void Car::setFloor(int value) +{ + floor = value; +} + +void Car::setNum(int value) +{ + num = value; +} diff --git a/car.h b/car.h index 53da392..eca5e62 100644 --- a/car.h +++ b/car.h @@ -9,6 +9,8 @@ #include #include #include "path.h" +#include "parkingLotManager.h" +class ParkingLotManager; //车身长宽 #define M_LEN (60) @@ -21,7 +23,8 @@ class Car : public QObject, public QGraphicsPixmapItem 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); + explicit Car(ParkingLotManager * manager, QGraphicsItem *parent = Q_NULLPTR, + int dir = 0, Car::Color color = RANDOM); //基础移动 void Forward(qreal vel = 3); //前进 void Backward(qreal vel = 3); //后退 @@ -35,6 +38,9 @@ public: int getNum(); int getFloor(); int getEntryNum(); + void setEntryNum(int value); + void setFloor(int value); + void setNum(int value); //动画 void moveTo(QPointF target); @@ -66,8 +72,6 @@ signals: void stair(Car* car); void exit(Car* car); void queueHead(Car* car); - - }; #endif // CAR_H diff --git a/monitor.cpp b/monitor.cpp index 8b9d1c0..7f3bef4 100644 --- a/monitor.cpp +++ b/monitor.cpp @@ -38,7 +38,7 @@ Monitor::Monitor(MainWindow *parent) : QObject::connect(ui->addCar, &QPushButton::clicked, [this]() { Log::i(checkPlate(this->ui->plateNumBox->text())==true?"true":"false"); }); - QObject::connect(ui->random, &QPushButton::clicked, this, &Monitor::addCar); + QObject::connect(ui->random, &QPushButton::clicked, pk, &ParkingLotManager::addCar); pk->showParkingLot(); ui->view->setScene(scene); diff --git a/parkingLotManager.cpp b/parkingLotManager.cpp index 38e0104..87f8321 100644 --- a/parkingLotManager.cpp +++ b/parkingLotManager.cpp @@ -54,11 +54,6 @@ ParkingLotManager::ParkingLotManager(QObject* objectParent, QGraphicsScene* scen QObject::connect(widget, &ParkingLotWidget::banParkingSpace, [i, this](bool b, int n){banSpace(b, i, n);}); } generatePool(true); - Car *car = new Car(); - scene->addItem(car); - Path* p = m_graph[1]->findPath(ParkingLotGraph::Node::entry, 1, ParkingLotGraph::Node::space, 100); - car->setPath(p); - car->followPath(); } void ParkingLotManager::showParkingLot(uint pos) @@ -141,10 +136,10 @@ void ParkingLotManager::requestOut(Car *car, int exit) void ParkingLotManager::requestIn(Car* car) { - int entry = car->getFloor(); + int entry = car->getEntryNum(); if (!m_pool.empty()) { - int l = m_pool.first().first; - int n = m_pool.first().second; + uint l = m_pool.first().first; + int n = m_pool.first().second + 1; m_pool.removeAt(0); Path* p; if (l == m_current_floor) @@ -152,8 +147,12 @@ void ParkingLotManager::requestIn(Car* car) else { p = m_graph[m_current_floor]->findPath(NODE_TYPE::queueHead, entry, NODE_TYPE::stair, entry); } - m_waitting[entry].removeFirst(); // 等候队列中的第一个移除 + m_waitting[entry - 1].removeOne(car); // 移除等候 m_cars[l][n] = car; + car->setPath(p); + car->setFloor(l); + car->setNum(n); + car->followPath(); Log::i(QString("分配第") + l + "层" + n + "号车位,剩余" + QString::number(m_pool.size()) + "个空车位"); } else { Log::i("请求失败,车位已满"); @@ -172,11 +171,17 @@ void ParkingLotManager::leave(Car* car) void ParkingLotManager::addCar(int entry) { + Log::i("生成车"); if (entry == -1) - entry = rand() % m_num_of_entry; - Car *car = new Car(); // TODO: 修改构造函数 + entry = (rand() % m_num_of_entry); + Car *car = new Car(this); + car->setEntryNum(entry + 1); m_waitting[entry].append(car); - // TODO: 添加路径 + Path *p = m_graph[1]->findPath(NODE_TYPE::entry, entry + 1, NODE_TYPE::queueHead, entry + 1); + car->setPath(p); + car->followPath(); + m_all_cars.append(car); + m_scene->addItem(car); } void ParkingLotManager::showMargin(bool enable) diff --git a/parkingLotManager.h b/parkingLotManager.h index 0192b3a..85a8994 100644 --- a/parkingLotManager.h +++ b/parkingLotManager.h @@ -9,6 +9,8 @@ #include "ParkingLotWidget.h" #include "parkinglotgraph.h" +class Car; + class ParkingLotManager: public QObject { Q_OBJECT @@ -46,6 +48,7 @@ private: QVector m_num_of_cars; // 每一层当前停的车数量 QVector > m_cars; // 停在每一层的车 QVector >m_waitting; // 在每个入口等待的车 + QList m_all_cars; QVector m_graph; QVector m_graph_pixmap; QStringList m_xml_path; // 每一层xml文件地址 diff --git a/path.cpp b/path.cpp index eed37f5..3355a71 100644 --- a/path.cpp +++ b/path.cpp @@ -37,9 +37,17 @@ int Path::pointsCount() return path.length(); } -PathPoint &Path::getNext() +PathPoint Path::getNext() { - PathPoint &tmp = path.first(); + PathPoint tmp = path.first(); path.removeFirst(); return tmp; } + +void PathPoint::operator=(const PathPoint &another) +{ + point = another.point; + dir = another.dir; + pointId = another.pointId; + action = another.action; +} diff --git a/path.h b/path.h index 85b3ed8..42a1661 100644 --- a/path.h +++ b/path.h @@ -14,19 +14,27 @@ struct PathPoint int pointId; // 为了做直角生成的辅助节点的id为-1 PathPoint() {} PathPoint(QPointF p, qreal d, Road::Action a=Road::Action::none, int id=-1): - point(p), dir(d), action(a), pointId(id) {} + point(p), action(a), pointId(id) { + while (d < 0 || d > 360) { + if (d < 0) + d += 360; + else if (d > 360) + d -= 360; + } + dir = d; + } PathPoint(const PathPoint& another) { point = another.point; dir = another.dir; pointId = another.pointId; action = another.action; } + void operator=(const PathPoint& another); }; class Path { public: - Path(); Path(PathPoint points[], int i); PathPoint getPoint(int i); @@ -34,7 +42,7 @@ public: void addPoint(const PathPoint &point); bool isEmpty(); int pointsCount(); - PathPoint &getNext(); + PathPoint getNext(); private: QList path;