diff --git a/src/gui/Direction.java b/src/gui/Direction.java deleted file mode 100644 index 15adff9..0000000 --- a/src/gui/Direction.java +++ /dev/null @@ -1,7 +0,0 @@ -package gui; - -public enum Direction { - NONE, - UP, - DOWN, -} diff --git a/src/gui/Elevator.java b/src/gui/Elevator.java deleted file mode 100644 index 8499815..0000000 --- a/src/gui/Elevator.java +++ /dev/null @@ -1,71 +0,0 @@ -package gui; -public class Elevator { - private int currentFloor = 0; - private boolean betweenFloors = false; /* Between currentFloor and the one above it. */ - private Direction direction = Direction.NONE; - private boolean stoppingNextFloor = false; - private boolean emergency = false; - - public Elevator(int currentFloor) { - this.currentFloor = currentFloor; - } - - public void goUp() { - if (emergency) return; - stoppingNextFloor = false; - direction = Direction.UP; - } - - public void goDown() { - if (emergency) return; - stoppingNextFloor = false; - direction = Direction.DOWN; - } - - public void stopElevator() { - direction = Direction.NONE; - } - - public void stopNextFloor() { - stoppingNextFloor = true; - } - - public void emergencyStop() { - emergency = true; - direction = Direction.NONE; - } - - public void cancelEmergency() { - emergency = false; - } - - public void move() { - if (emergency) return; - switch (direction) { - case DOWN: - /* Go halfway below. */ - if (!betweenFloors) currentFloor--; - betweenFloors = !betweenFloors; - break; - case UP: - /* Go halfway above. */ - if (betweenFloors) currentFloor++; - betweenFloors = !betweenFloors; - break; - default: - break; - } - } - - public int getCurrentFloor() { - return currentFloor; - } - - public boolean getBetweenFloors() { - return betweenFloors; - } - - public Direction getDirection() { - return direction; - } -} diff --git a/src/simulation/Elevator.java b/src/simulation/Elevator.java new file mode 100644 index 0000000..a0438d5 --- /dev/null +++ b/src/simulation/Elevator.java @@ -0,0 +1,127 @@ +package simulation; + +import java.awt.event.ActionListener; +import java.awt.event.ActionEvent; + +import commandSystem.Direction; +import commandSystem.ElevatorListener; + + +public class Elevator { + static final int PRECISION = 10; + private double floorHeight; + private double step; + private double height; + // private int currentFloor = 0; + // private boolean betweenFloors = false; /* Between currentFloor and the one above it. */ + private Direction direction = Direction.NONE; + private boolean stoppingNextFloor = false; + private boolean emergency = false; + private ElevatorListener listener; + + public Elevator(int currentFloor, int nbFloors) { + height = (double) currentFloor / nbFloors; + floorHeight = 1. / nbFloors; + step = floorHeight / PRECISION; + } + + public void setActionListener(ElevatorListener listener) { + this.listener = listener; + } + + public void goUp() { + if (emergency) return; + stoppingNextFloor = false; + direction = Direction.UP; + } + + public void goDown() { + if (emergency) return; + stoppingNextFloor = false; + direction = Direction.DOWN; + } + + public void stopElevator() { + direction = Direction.NONE; + } + + public void stopNextFloor() { + stoppingNextFloor = true; + } + + public void emergencyStop() { + emergency = true; + direction = Direction.NONE; + } + + public void cancelEmergency() { + emergency = false; + } + + public void update() { + if (emergency) return; + 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.UP); + } + // /* 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.DOWN); + } + // /* Go halfway above. */ + // if (betweenFloors) currentFloor++; + // betweenFloors = !betweenFloors; + break; + default: + break; + } + } + + // public int getCurrentFloor() { + // return currentFloor; + // } + + // public boolean getBetweenFloors() { + // return betweenFloors; + // } + + public Direction getDirection() { + return direction; + } + + public boolean getStoppingNextFloor() { + return stoppingNextFloor; + } + + public double getHeight() { + return height; + } + + public boolean getEmergency() { + return emergency; + } +} diff --git a/src/simulation/Simulation.java b/src/simulation/Simulation.java new file mode 100644 index 0000000..f8884bc --- /dev/null +++ b/src/simulation/Simulation.java @@ -0,0 +1,63 @@ +package simulation; + +import Requests.CallFromElevatorRequest; +import Requests.CallFromFloorRequest; + +import commandSystem.EventQueue; +import commandSystem.Direction; +import commandSystem.ElevatorListener; +import simulation.Elevator; + + +public class Simulation implements ElevatorListener { + private boolean running = false; + private int currentFloor = 0; + private EventQueue queue; + private Elevator elevator; + + public Simulation(Elevator elevator) { + this.elevator = elevator; + } + + public void elevatorCall(int floor) { + } + + public void floorCall(int floor, Direction direction) { + } + + public void floorChange(Direction d) { + switch (d) { + case UP: + currentFloor++; + break; + case DOWN: + currentFloor--; + break; + } + // iter(); + } + + // public void iter() { + // int nextDestination = queue.getNextInstruction(); + // int currentFloor = 0; + // //System.out.println("next dest = " + nextDestination); + // if (!running) { + // if (nextDestination > 0) { /* We have a destination to go to. */ + // running = true; + // elevator.goDown(); + // } + // } + // else { + // if (elevator.currentFloor == nextDestination) { + // queue.removeInstruction(elevator.currentFloor, elevator.direction); + // running = false; + // } + // else { + // elevator.direction = elevator.currentFloor < nextDestination ? Direction.UP : Direction.DOWN; + // elevator.currentFloor += elevator.direction == Direction.UP ? 1 : -1; + // } + // } + // System.out.println("elevator floor " + elevator.currentFloor + " direction " + + // (elevator.direction == Direction.UP? "up" : "down") + " running: " + running); + // } +}