voilà, le graphique est réglé, l'ascenseur va de 0 à 1 (à précision flottante près) et graphiquement - problème de queue réglée, on est plus bloqués

This commit is contained in:
DylanVsn 2019-10-17 22:04:43 +02:00
parent f53827dd4f
commit 8475ff4ee0
3 changed files with 53 additions and 32 deletions

View File

@ -204,6 +204,10 @@ Next instruction: 0
default:
break;
}
// eliminate floor if next instruction is floor because already satisfied
if (getNextInstruction() == floor) {
removeInstruction(floor);
}
}
/**

View File

@ -45,7 +45,10 @@ class ElevatorCanvas extends JPanel {
else {
g.setColor(Color.BLACK);
}
g.fillRect(0, 200 - (int) Math.floor(elevator.getHeight() * 200) - HEIGHT, WIDTH, HEIGHT);
// 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);
Toolkit.getDefaultToolkit().sync();
}
}

View File

@ -20,8 +20,8 @@ public class Elevator {
private ElevatorListener listener;
public Elevator(int currentFloor, int nbFloors) {
height = (double) currentFloor / nbFloors;
floorHeight = 1. / nbFloors;
height = (double) currentFloor / (nbFloors - 1); // -1 car rez-de-chaussée
floorHeight = 1. / (nbFloors - 1);
step = floorHeight / PRECISION;
System.out.println("step: " + step + ", floorHeight: " + floorHeight);
}
@ -61,42 +61,56 @@ public class Elevator {
public void update() {
if (emergency) return;
int prevFloor = (int) Math.floor(height / floorHeight);
// int prevFloor = (int) Math.floor(height / floorHeight);
switch (direction) {
// case DOWN:
// height -= step;
// /* Bottom reached. */
// if (height < step) {
// height = 0;
// direction = Direction.NONE;
// }
// /* Passed a floor, invoke the listener. */
// if (Math.floor(height / floorHeight) < prevFloor && listener != null) {
// listener.floorChange(Direction.DOWN);
// }
// // /* Go halfway below. */
// // if (!betweenFloors) currentFloor--;
// // betweenFloors = !betweenFloors;
// break;
// case UP:
// height += step;
// /* Top reached. */
// if (height > 1-step) {
// height = 1;
// direction = Direction.NONE;
// }
// /* Passed a floor, invoke the listener. */
// if (Math.floor(height / floorHeight) > prevFloor && listener != null) {
// listener.floorChange(Direction.UP);
// }
// // /* Go halfway above. */
// // if (betweenFloors) currentFloor++;
// // betweenFloors = !betweenFloors;
// break;
case DOWN:
height -= step;
/* Bottom reached. */
if (height < step) {
height = 0;
direction = Direction.NONE;
}
/* Passed a floor, invoke the listener. */
if (Math.floor(height / floorHeight) < prevFloor && listener != null) {
if (height % floorHeight < step && listener != null) {
listener.floorChange(Direction.DOWN);
}
// /* Go halfway below. */
// if (!betweenFloors) currentFloor--;
// betweenFloors = !betweenFloors;
break;
case UP:
height += step;
/* Top reached. */
if (height > 1-step) {
height = 1;
direction = Direction.NONE;
}
/* Passed a floor, invoke the listener. */
if (Math.floor(height / floorHeight) > prevFloor && listener != null) {
listener.floorChange(Direction.UP);
}
// /* Go halfway above. */
// if (betweenFloors) currentFloor++;
// betweenFloors = !betweenFloors;
break;
case UP:
height += step;
if (height % floorHeight < step && listener != null) {
listener.floorChange(Direction.UP);
}
break;
default:
break;
}