implement slowing down before reaching the target floor

This commit is contained in:
papush! 2019-10-19 19:07:01 +02:00
parent 9b42c0f1c1
commit 9d1796ef4c
4 changed files with 49 additions and 16 deletions

View File

@ -113,7 +113,7 @@ public class BasicInstructionQueue implements InstructionQueue {
public int getNextInstruction() { public int getNextInstruction() {
int nextFloor = -1; int nextFloor = -1;
System.out.println("we go " + currentDirection + " current floor: " + currentFloor + " queues: " + upQueue + downQueue); // System.out.println("we go " + currentDirection + " current floor: " + currentFloor + " queues: " + upQueue + downQueue);
switch (currentDirection) { switch (currentDirection) {
// get the first element of upQueue that is ABOVE the elevator's current floor // get the first element of upQueue that is ABOVE the elevator's current floor

View File

@ -9,7 +9,7 @@ import commandSystem.Direction;
class ElevatorCanvas extends JPanel { class ElevatorCanvas extends JPanel {
private final static int WIDTH = 40; private final static int WIDTH = 40;
private final static int HEIGHT = 50; private final static int HEIGHT = 50;
private final static Dimension DIMENSIONS = new Dimension(WIDTH, 200); private final static Dimension DIMENSIONS = new Dimension(WIDTH, 300);
private Elevator elevator; private Elevator elevator;
@ -33,21 +33,26 @@ class ElevatorCanvas extends JPanel {
@Override @Override
public void paintComponent(Graphics g) { public void paintComponent(Graphics g) {
super.paintComponent(g); super.paintComponent(g);
if (elevator.getStoppingNextFloor()) { if (elevator.getEmergency()) {
g.setColor(Color.RED);
}
else if (elevator.getStoppingNextFloor()) {
g.setColor(Color.BLUE); g.setColor(Color.BLUE);
} }
else if (elevator.getDirection() == Direction.NONE) { else if (elevator.getDirection() == Direction.NONE) {
g.setColor(Color.GRAY); g.setColor(Color.GREEN);
}
else if (elevator.getEmergency()) {
g.setColor(Color.RED);
} }
else { else {
g.setColor(Color.BLACK); g.setColor(Color.BLACK);
} }
// g.fillRect(0, 200 - (int) Math.floor(elevator.getHeight() * 200) - HEIGHT, WIDTH, HEIGHT); g.fillRect(0, (int) DIMENSIONS.getHeight() - HEIGHT - (int) (((int) DIMENSIONS.getHeight() - HEIGHT) * elevator.getHeight()), WIDTH, HEIGHT);
// System.out.println("elevator height:" + elevator.getHeight());
g.fillRect(0, 200 - HEIGHT - (int) ((200 - HEIGHT) * elevator.getHeight()), WIDTH, HEIGHT); g.setColor(Color.GRAY);
int nbFloors = elevator.getNbFloors();
for (int i = 0; i < nbFloors; i++) {
int y = i * ((int) DIMENSIONS.getHeight() / (nbFloors - 1));
g.fillRect(0, y-1, WIDTH, 3);
}
Toolkit.getDefaultToolkit().sync(); Toolkit.getDefaultToolkit().sync();
} }

View File

@ -11,16 +11,20 @@ public class Elevator {
static final int PRECISION = 100; static final int PRECISION = 100;
private double floorHeight; private double floorHeight;
private double step; private double step;
private double slow_step;
private double height; private double height;
private Direction direction = Direction.NONE; private Direction direction = Direction.NONE;
private boolean stoppingNextFloor = false; private boolean stoppingNextFloor = false;
private boolean emergency = false; private boolean emergency = false;
private ElevatorListener listener; private ElevatorListener listener;
private int nbFloors;
public Elevator(int currentFloor, int nbFloors) { public Elevator(int currentFloor, int nbFloors) {
this.nbFloors = nbFloors;
height = (double) currentFloor / (nbFloors - 1); // -1 car rez-de-chaussée height = (double) currentFloor / (nbFloors - 1); // -1 car rez-de-chaussée
floorHeight = 1. / (nbFloors - 1); floorHeight = 1. / (nbFloors - 1);
step = floorHeight / PRECISION; step = floorHeight / PRECISION;
slow_step = step / 2;
System.out.println("step: " + step + ", floorHeight: " + floorHeight); System.out.println("step: " + step + ", floorHeight: " + floorHeight);
} }
@ -42,6 +46,7 @@ public class Elevator {
public void stop() { public void stop() {
direction = Direction.NONE; direction = Direction.NONE;
stoppingNextFloor = false;
} }
public void stopNextFloor() { public void stopNextFloor() {
@ -59,16 +64,25 @@ public class Elevator {
public void update() { public void update() {
if (emergency) return; if (emergency) return;
double delta;
if (stoppingNextFloor) {
delta = slow_step;
}
else {
delta = step;
}
switch (direction) { switch (direction) {
case DOWN: case DOWN:
height -= step; height -= delta;
if (height % floorHeight < step && listener != null) { if (height % floorHeight < delta && listener != null) {
listener.floorChange(Direction.DOWN); listener.floorChange(Direction.DOWN);
} }
break; break;
case UP: case UP:
height += step; height += delta;
if (height % floorHeight < step && listener != null) { if (height % floorHeight < delta && listener != null) {
listener.floorChange(Direction.UP); listener.floorChange(Direction.UP);
} }
break; break;
@ -92,4 +106,8 @@ public class Elevator {
public boolean getEmergency() { public boolean getEmergency() {
return emergency; return emergency;
} }
public int getNbFloors() {
return nbFloors;
}
} }

View File

@ -56,18 +56,28 @@ public class Simulation implements ElevatorListener {
private void update() { private void update() {
final int next = queue.getNextInstruction(); final int next = queue.getNextInstruction();
System.out.println("Current floor: " + currentFloor);
System.out.println("Next instruction: " + next); System.out.println("Next instruction: " + next);
if (next == -1 || next == currentFloor) { if (next == -1 || next == currentFloor) {
System.out.println("Stopping."); System.out.println("Stopping.");
elevator.stop(); elevator.stop();
return;
} }
else if (next > currentFloor) { else if (next > currentFloor) {
System.out.println("Going up."); System.out.print("Going up.");
elevator.goUp(); elevator.goUp();
} }
else if (next < currentFloor) { else if (next < currentFloor) {
System.out.println("Going down."); System.out.print("Going down.");
elevator.goDown(); elevator.goDown();
} }
if (next == currentFloor + 1 || next == currentFloor - 1) {
System.out.print("\b (slowly).");
elevator.stopNextFloor();
}
System.out.println("\n");
} }
} }