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() {
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) {
// 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 {
private final static int WIDTH = 40;
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;
@ -33,21 +33,26 @@ class ElevatorCanvas extends JPanel {
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
if (elevator.getStoppingNextFloor()) {
if (elevator.getEmergency()) {
g.setColor(Color.RED);
}
else if (elevator.getStoppingNextFloor()) {
g.setColor(Color.BLUE);
}
else if (elevator.getDirection() == Direction.NONE) {
g.setColor(Color.GRAY);
}
else if (elevator.getEmergency()) {
g.setColor(Color.RED);
g.setColor(Color.GREEN);
}
else {
g.setColor(Color.BLACK);
}
// g.fillRect(0, 200 - (int) Math.floor(elevator.getHeight() * 200) - HEIGHT, WIDTH, HEIGHT);
// System.out.println("elevator height:" + elevator.getHeight());
g.fillRect(0, 200 - HEIGHT - (int) ((200 - HEIGHT) * elevator.getHeight()), WIDTH, HEIGHT);
g.fillRect(0, (int) DIMENSIONS.getHeight() - HEIGHT - (int) (((int) DIMENSIONS.getHeight() - 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();
}

View File

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

View File

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