From 8475ff4ee0b8db1d795adae4e1a1039fdb1f9800 Mon Sep 17 00:00:00 2001 From: DylanVsn <43576618+DylanVsn@users.noreply.github.com> Date: Thu, 17 Oct 2019 22:04:43 +0200 Subject: [PATCH] =?UTF-8?q?voil=C3=A0,=20le=20graphique=20est=20r=C3=A9gl?= =?UTF-8?q?=C3=A9,=20l'ascenseur=20va=20de=200=20=C3=A0=201=20(=C3=A0=20pr?= =?UTF-8?q?=C3=A9cision=20flottante=20pr=C3=A8s)=20et=20graphiquement=20-?= =?UTF-8?q?=20probl=C3=A8me=20de=20queue=20r=C3=A9gl=C3=A9e,=20on=20est=20?= =?UTF-8?q?plus=20bloqu=C3=A9s?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/commandSystem/BasicInstructionQueue.java | 4 ++ src/gui/ElevatorCanvas.java | 5 +- src/simulation/Elevator.java | 76 ++++++++++++-------- 3 files changed, 53 insertions(+), 32 deletions(-) diff --git a/src/commandSystem/BasicInstructionQueue.java b/src/commandSystem/BasicInstructionQueue.java index 234345e..7e1aaf0 100644 --- a/src/commandSystem/BasicInstructionQueue.java +++ b/src/commandSystem/BasicInstructionQueue.java @@ -204,6 +204,10 @@ Next instruction: 0 default: break; } + // eliminate floor if next instruction is floor because already satisfied + if (getNextInstruction() == floor) { + removeInstruction(floor); + } } /** diff --git a/src/gui/ElevatorCanvas.java b/src/gui/ElevatorCanvas.java index 861e138..a45ea0f 100644 --- a/src/gui/ElevatorCanvas.java +++ b/src/gui/ElevatorCanvas.java @@ -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(); } } diff --git a/src/simulation/Elevator.java b/src/simulation/Elevator.java index ca1893d..81dde43 100644 --- a/src/simulation/Elevator.java +++ b/src/simulation/Elevator.java @@ -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; }