Permalink
Browse files

添加乘客进出的动画

  • Loading branch information...
1 parent c535ead commit 648d2ecaa8f10b8bbfc2fdeac5befebd4f744623 @ranxian committed Oct 19, 2012
Showing with 106 additions and 30 deletions.
  1. +5 −0 Headers/floor.h
  2. +6 −1 Headers/passenger.h
  3. +1 −0 Sources/elevator.cpp
  4. +5 −2 Sources/floor.cpp
  5. +3 −3 Sources/main.cpp
  6. +27 −6 Sources/passenger.cpp
  7. +59 −18 Sources/qt/emulator.cpp
View
5 Headers/floor.h
@@ -8,8 +8,11 @@ class Order;
class Floor
{
friend class Passenger;
+ friend class Emulator;
private:
list<Passenger*> passengers;
+ list<Passenger*> inPassengers;
+ list<Passenger*> outPassengers;
Order* upOrder;
Order* downOrder;
public:
@@ -38,5 +41,7 @@ class Floor
void CancelOrder(char dir); /* 取消 dir 方向的请求 */
void CheckOrder(); /* 楼层检查是否有乘客要去某方向,但是这个方向的按钮没有被按,如果有这种情况,就让该乘客请求一部电梯 */
+
+ void PushOutPassengers(Passenger* pass);
};
#endif
View
7 Headers/passenger.h
@@ -20,6 +20,9 @@ class Passenger
static int totalDuration;
static int nCompleteDuration;
+ double fpos;
+ int status;
+
public:
Passenger(int from_, int to_); /* Construct a passenfer from `from_` to `to_` */
@@ -30,7 +33,7 @@ class Passenger
void Wait(int t = 1); /* Wait for t unit time */
- void Move(int t = 1); /* Move for t unit time */
+ bool Move(int t = 1); /* Move for t unit time */
static int Total(); /* Total number of all passengers */
@@ -50,5 +53,7 @@ class Passenger
void MakeAnOrder(); /* 乘客按下电梯按钮,每层楼有两个按钮,乘客将根据目的地选择,以按下了乘客所需
* 按钮时乘客不会再按 */
+
+ double Get_fpos();
};
#endif
View
1 Sources/elevator.cpp
@@ -94,6 +94,7 @@ void Elevator::Letout() {
if((*iter)->Get_to() == pos) {
throughput++;
(*iter)->Arrive();
+ Emulator::floors[pos].PushOutPassengers(*iter);
}
}
passengers.remove_if( to_is_pos(pos) );
View
7 Sources/floor.cpp
@@ -9,8 +9,7 @@ Floor::Floor() {
}
void Floor::PushPassenger(Passenger *p) {
- passengers.push_back(p);
-
+ inPassengers.push_back(p);
}
Passenger *Floor::PopPassenger() {
@@ -86,4 +85,8 @@ void Floor::CheckOrder() {
for (iter = passengers.begin(); iter != passengers.end(); iter++) {
(*iter)->MakeAnOrder();
}
+}
+
+void Floor::PushOutPassengers(Passenger* pass) {
+ outPassengers.push_back(pass);
}
View
6 Sources/main.cpp
@@ -45,9 +45,9 @@ int main(int argc, char *argv[])
/* Set runtime parameters */
Emulator::nFloor = 10;
- Emulator::lambda = 0.1;
+ Emulator::lambda = 0.0005;
Emulator::nElevator = 10;
- Emulator::elevatorType = "costflow";
+ Emulator::elevatorType = "fcfs";
/* Initialize elevators */
for(int i = 0; i < Emulator::nElevator; i++) {
@@ -57,7 +57,7 @@ int main(int argc, char *argv[])
Emulator window;
- window.resize(800, 600);
+ window.resize(1024, 600);
window.show();
window.setWindowTitle("Elevator Emulator");
View
33 Sources/passenger.cpp
@@ -1,12 +1,14 @@
-#include "../Headers/passenger.h"
+ #include "../Headers/passenger.h"
#include "../Headers/floor.h"
#include "../Headers/emulator.h"
#include "../Headers/order.h"
Passenger::Passenger(int from_, int to_): from(from_), to(to_) {
- total += 1;
- timeWait = 0;
- timeMove = 0;
+ total += 1;
+ timeWait = 0;
+ timeMove = 0;
+ status = 0;
+ fpos = 0;
}
int Passenger::Total() {
@@ -56,8 +58,21 @@ void Passenger::Wait(int t) {
maxWaitTime = std::max(maxWaitTime, timeWait);
}
-void Passenger::Move(int t) {
- timeMove += t;
+bool Passenger::Move(int t) {
+ if (status == 0) {
+ fpos += 0.1;
+ if (fpos >= 10) {
+ return true;
+ }
+ } else if (status == 1) {
+ timeMove += t;
+ } else if (status == 2) {
+ fpos += 0.1;
+ if (fpos >= 20) {
+ return true;
+ }
+ }
+ return false;
}
void Passenger::Arrive() {
@@ -67,6 +82,8 @@ void Passenger::Arrive() {
nCompleteDuration += 1;
minDuration = std::min(minDuration, duration);
maxDuration = std::max(maxDuration, duration);
+
+ status = 2;
}
void Passenger::MakeAnOrder() {
@@ -82,4 +99,8 @@ void Passenger::MakeAnOrder() {
floor->Set_downOrder(order);
Emulator::orders.push_back(order);
}
+}
+
+double Passenger::Get_fpos() {
+ return fpos;
}
View
77 Sources/qt/emulator.cpp
@@ -2,6 +2,7 @@
#include "../Headers/PoissRand.h"
#include "../Headers/internlib.h"
#include "../Headers/controller.h"
+#include "../Headers/passenger.h"
#include <QPushButton>
#include <QApplication>
#include <QGridLayout>
@@ -13,7 +14,7 @@ Emulator::Emulator(QWidget* parent) : QWidget(parent) {
START = 0;
QLabel* labelP;
labelP = new QLabel("Elevator:", this);
- int bias = 20;
+ int bias = 20 + 150;
labelP->move(40 + bias, 530);
nEle = new QLabel("0 ", this);
nEle->move(100 + bias, 530);
@@ -54,10 +55,10 @@ Emulator::Emulator(QWidget* parent) : QWidget(parent) {
avThr->move(bias + 180, 570);
start = new QPushButton("START", this);
- start->move(700, 550);
+ start->setGeometry(720, 550, 100, 20);
QPushButton *quit = new QPushButton("Quit ", this);
- quit->move(700, 570);
+ quit->setGeometry(720, 570, 100, 20);
connect(quit, SIGNAL(clicked()), qApp, SLOT(quit()));
connect(start, SIGNAL(clicked()), this, SLOT(startEmulationSlots()));
@@ -93,12 +94,13 @@ void Emulator::timerEvent(QTimerEvent *event)
Q_UNUSED(event);
if(START) {
if (timer == 0) {
- Born(); /* 产生乘客,电梯请求 */
+
Control(); /* 调用调度算法 */
Update(); /* 更新统计数据 */
}
+ Born(); /* 产生乘客,电梯请求 */
Move(); /* 电梯依据指令或默认逻辑移动 */
@@ -112,38 +114,59 @@ void Emulator::timerEvent(QTimerEvent *event)
void Emulator::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event);
+ int bias = 150;
QPainter painter(this);
QFont font("Courier", 40);
painter.setFont(font);
painter.setBrush(QBrush("#c56c00"));
int i = 0;
+
/* 绘制电梯 */
for(EleIter iter = elevators.begin(); iter != elevators.end(); iter++) {
i++;
- painter.drawRect(150 + i * 50, height() - (128 + (*iter)->Get_fpos() * 50), 40, 40);
+ painter.drawRect(bias + 150 + i * 50, height() - (128 + (*iter)->Get_fpos() * 50), 40, 40);
painter.setPen(Qt::white);
- painter.drawText(150 + i * 50, height() - (95 + (*iter)->Get_fpos() * 50), QString::number((*iter)->Get_nPassenger()));//QString::number((*iter)->Get_inst()->size() ));
+ painter.drawText(bias + 150 + i * 50, height() - (95 + (*iter)->Get_fpos() * 50), QString::number((*iter)->Get_nPassenger()));//QString::number((*iter)->Get_inst()->size() ));
painter.setPen(QColor(Qt::black));
}
/* 绘制楼层 */
for (i = 0; i < nFloor; i++) {
- painter.drawRect(60, height() - (84 + i * 50), 110, 4);
+ painter.drawRect(bias + 60, height() - (84 + i * 50), 110, 4);
painter.setPen(QColor(Qt::gray));
- painter.drawText(105, height() - (90 + i * 50), QString::number(floors[i].Get_nPassenger()));
+ painter.drawText(bias + 105, height() - (90 + i * 50), QString::number(floors[i].Get_nPassenger()));
painter.setPen(QColor(Qt::red));
if (floors[i].UpOrdered())
- painter.drawText(145, height() - (90 + i * 50), "^");
+ painter.drawText(bias + 145, height() - (90 + i * 50), "^");
if (floors[i].DownOrdered()) {
- painter.drawText(145, height() - (90 + i * 50) + 5, "v");
+ painter.drawText(bias + 145, height() - (90 + i * 50) + 5, "v");
}
painter.setPen(QColor(Qt::black));
}
QPen pen(Qt::gray, 2, Qt::DashDotLine);
painter.setPen(pen);
+ /* 绘制楼层分界 */
+ for (i = 0; i < nFloor; i++) {
+ painter.drawLine(bias + 180, 67 + i * 50, 700 + bias, 67 + i * 50);
+ }
+
+ /* 绘制乘客 */
+ for (i = 0; i < nFloor; i++) {
+ list<Passenger*>::iterator iter;
+ for (iter = floors[i].inPassengers.begin(); iter != floors[i].inPassengers.end(); iter++) {
+ painter.setBrush(QColor(Qt::gray));
+ painter.drawRect(bias - 100 + 13*(*iter)->Get_fpos(), height() - (120 + i * 50), 30, 30);
+ }
+ for (iter = floors[i].outPassengers.begin(); iter != floors[i].outPassengers.end(); iter++) {
+ painter.setBrush(QColor(Qt::gray));
+ painter.drawRect(bias + 550 + 13*(*iter)->Get_fpos(), height() - (120 + i * 50), 30, 30);
+ }
+
+ }
+
/* 打印统计数据 */
nEle->setText (QString::number(nElevator));
nPas->setText (QString::number(Passenger::Total()));
@@ -153,11 +176,6 @@ void Emulator::paintEvent(QPaintEvent *event)
mxDur->setText(QString::number(Passenger::MaxDuration()));
mnDur->setText(QString::number(Passenger::MinDuration()));
avThr->setText(QString::number(Elevator::AverThrput()));
-
- /* 绘制楼层分界 */
- for (i = 0; i < nFloor; i++) {
- painter.drawLine(180, 67 + i * 50, 700, 67 + i * 50);
- }
}
/* Qt::键盘响应事件映射 */
@@ -184,8 +202,6 @@ void Emulator::Born() {
Passenger *pass = new Passenger(i, floor(Rand(0, nFloor)));
floors[i].PushPassenger(pass);
-
- pass->MakeAnOrder();
}
}
}
@@ -203,12 +219,37 @@ void Emulator::Control() {
}
}
+struct fpos_lt
+{
+ int pos;
+ fpos_lt(double pos_) : pos(pos_){};
+ bool operator () (Passenger *p) {
+ return p->Get_fpos() >= pos;
+ }
+};
/* 电梯依据指令或默认逻辑移动 */
void Emulator::Move() {
EleIter iter;
- for(iter = elevators.begin(); iter != elevators.end(); iter++) {
+ list<Passenger*>::iterator pass_iter;
+ list<Passenger*> toDelete;
+
+ for (iter = elevators.begin(); iter != elevators.end(); iter++) {
(*iter)->Move();
}
+ for (int i = 0; i < nFloor; i++) {
+ for (pass_iter = floors[i].inPassengers.begin(); pass_iter != floors[i].inPassengers.end(); pass_iter++) {
+ if ((*pass_iter)->Move()) {
+ floors[i].passengers.push_back(*pass_iter);
+ (*pass_iter)->MakeAnOrder();
+ }
+ }
+ floors[i].inPassengers.remove_if(fpos_lt(10));
+ for (pass_iter = floors[i].outPassengers.begin(); pass_iter != floors[i].outPassengers.end(); pass_iter++) {
+ (*pass_iter)->Move();
+ }
+ floors[i].outPassengers.remove_if(fpos_lt(20));
+ }
+
}
/* 更新统计数据 */

0 comments on commit 648d2ec

Please sign in to comment.