From e295662429c464c060a104c99ad7acad92ed41a2 Mon Sep 17 00:00:00 2001 From: w-mj Date: Sun, 24 Dec 2017 23:09:56 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=AF=E8=BF=90=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 但是有一堆warning --- car.cpp | 28 +++++++++++++++++++++++++--- car.h | 10 +++++++++- parkingLotManager.cpp | 24 ++++++++++++------------ parkinglotgraph.cpp | 8 ++++---- parkinglotgraph.h | 2 +- 5 files changed, 51 insertions(+), 21 deletions(-) diff --git a/car.cpp b/car.cpp index ccd9644..b04e8e3 100644 --- a/car.cpp +++ b/car.cpp @@ -2,9 +2,11 @@ Car::Car(QGraphicsItem *parent, int dir, Car::Color color) : + QObject(0), QGraphicsPixmapItem(parent), posAni(new QPropertyAnimation(this,"m_pos")), - m_target(PathPoint(this->pos(),this->rotation())) + m_target(PathPoint(this->pos(),this->rotation())), + m_current(PathPoint(this->pos(),this->rotation())) { m_pos = this->pos(); this->setRotation(dir); @@ -81,12 +83,31 @@ void Car::turnRight(int r, double ang) void Car::setPath(Path *path) { - m_path = path; + m_path = path; +} + +int Car::getNum() +{ + return num; +} + +int Car::getFloor() +{ + return floor; +} + +int Car::getEntryNum() +{ + return entryNum; } void Car::moveTo(QPointF target) { - posAni->setDuration(1000); + qreal dx = m_current.point.x()-m_target.point.x(); + qreal dy = m_current.point.y()-m_target.point.y(); + qreal dis = qSqrt(qPow(dx,2)+qPow(dy,2)); + qreal dur = dis/0.3; + posAni->setDuration(dur); posAni->setEndValue(target); posAni->start(); } @@ -113,6 +134,7 @@ void Car::followPath() } if(!m_path->isEmpty()) { + m_current = m_target; m_target = m_path->getNext(); moveTo(m_target.point); } diff --git a/car.h b/car.h index 770c4ac..d8a283e 100644 --- a/car.h +++ b/car.h @@ -7,6 +7,7 @@ #include #include #include +#include #include "path.h" //车身长宽 @@ -31,6 +32,10 @@ public: void turnRight(int r, double ang); //右转 void setPath(Path *path); + int getNum(); + int getFloor(); + int getEntryNum(); + //动画 void moveTo(QPointF target); void followPath(); @@ -52,7 +57,10 @@ private: qreal m_dir; Path *m_path; PathPoint m_target; - + PathPoint m_current; + int entryNum; + int floor; + int num; signals: void entry(Car* car); diff --git a/parkingLotManager.cpp b/parkingLotManager.cpp index 4eb77e6..38e0104 100644 --- a/parkingLotManager.cpp +++ b/parkingLotManager.cpp @@ -56,7 +56,7 @@ ParkingLotManager::ParkingLotManager(QObject* objectParent, QGraphicsScene* scen generatePool(true); Car *car = new Car(); scene->addItem(car); - Path* p = m_graph[1]->finaPath(ParkingLotGraph::Node::entry, 1, ParkingLotGraph::Node::space, 100); + Path* p = m_graph[1]->findPath(ParkingLotGraph::Node::entry, 1, ParkingLotGraph::Node::space, 100); car->setPath(p); car->followPath(); } @@ -119,7 +119,7 @@ void ParkingLotManager::drawPath(int n1, int n2) { QPen pen(QColor(242, 23, 242)); pen.setWidth(3); - Path *p = m_graph[m_current_floor]->finaPath(getType(n1), n1, getType(n2), n2); + Path *p = m_graph[m_current_floor]->findPath(getType(n1), n1, getType(n2), n2); for(int i = 1; i < p->pointsCount(); i++) { QLineF line(p->getPoint(i-1).point, p->getPoint(i).point); QPointF dir(qCos(qDegreesToRadians(p->getPoint(i-1).dir)) * 10, @@ -132,38 +132,38 @@ void ParkingLotManager::drawPath(int n1, int n2) } } -void ParkingLotManager::requestOut(Car *car, int exit = -1) +void ParkingLotManager::requestOut(Car *car, int exit) { if (exit != -1) exit = rand() % m_num_of_entry; Path *p; // TODO:请求出口 } -ParkingLotManager::RequestSpace ParkingLotManager::requestIn(Car* car) +void ParkingLotManager::requestIn(Car* car) { + int entry = car->getFloor(); if (!m_pool.empty()) { int l = m_pool.first().first; int n = m_pool.first().second; m_pool.removeAt(0); Path* p; if (l == m_current_floor) - p = m_graph[l]->finaPath(NODE_TYPE::queueHead, entry, NODE_TYPE::space, n); + p = m_graph[l]->findPath(NODE_TYPE::queueHead, entry, NODE_TYPE::space, n); else { p = m_graph[m_current_floor]->findPath(NODE_TYPE::queueHead, entry, NODE_TYPE::stair, entry); } - m_waitting.at(entry).removeFirst(); // 等候队列中的第一个移除 - m_cars.at(l).at(n) = car; + m_waitting[entry].removeFirst(); // 等候队列中的第一个移除 + m_cars[l][n] = car; Log::i(QString("分配第") + l + "层" + n + "号车位,剩余" + QString::number(m_pool.size()) + "个空车位"); - - return RequestSpace(l, n, p); } else { Log::i("请求失败,车位已满"); } - return RequestSpace(); } void ParkingLotManager::leave(Car* car) { + int l = car->getFloor(); + int n = car->getNum(); m_pool.append(qMakePair(l, n)); Log::i(QString("有车离开,当前共有%1个空车位").arg(m_pool.size())); emit carLeave(); @@ -174,8 +174,8 @@ void ParkingLotManager::addCar(int entry) { if (entry == -1) entry = rand() % m_num_of_entry; - Car *car = new Car(0, 0); // TODO: 修改构造函数 - m_waitting.at(entry).append(car); + Car *car = new Car(); // TODO: 修改构造函数 + m_waitting[entry].append(car); // TODO: 添加路径 } diff --git a/parkinglotgraph.cpp b/parkinglotgraph.cpp index f1ac574..4b76a35 100644 --- a/parkinglotgraph.cpp +++ b/parkinglotgraph.cpp @@ -173,9 +173,9 @@ uint ParkingLotGraph::getNodeId(ParkingLotGraph::Node::Type t, int n) { return m_actionList.at(n - 1)->getId(); } else { Road::Action act = static_cast(t); - for (auto n: m_actionList) { - if (n->action == act && --n == 0) - return n->id; + for (auto node: m_actionList) { + if (node->action == act && --n == 0) + return node->id; } } return -1; @@ -186,7 +186,7 @@ qreal angle(const QPoint& p) { } // Dijkstra 求最短路径 -Path *ParkingLotGraph::finaPath(ParkingLotGraph::Node::Type t1, int n1, ParkingLotGraph::Node::Type t2, int n2) +Path *ParkingLotGraph::findPath(ParkingLotGraph::Node::Type t1, int n1, ParkingLotGraph::Node::Type t2, int n2) { struct HeapNode { uint d, u;//d为到该点的总路径,u为这一点 diff --git a/parkinglotgraph.h b/parkinglotgraph.h index b1cdfd9..a14fc00 100644 --- a/parkinglotgraph.h +++ b/parkinglotgraph.h @@ -40,7 +40,7 @@ public: } void paint(QGraphicsScene* scene); - Path* finaPath(Node::Type t1, int n1, Node::Type t2, int n2); + Path* findPath(Node::Type t1, int n1, Node::Type t2, int n2); uint getNodeId(Node::Type t, int n); ~ParkingLotGraph(); QPixmap *getPixmap();