public Building() { //Set exitLocation on 0 floor exitLocation = 677; //Initialize floors arrayOfAllFloors = new Floor[4]; arrayOfAllFloors[0] = new Floor(this, 0, 373); arrayOfAllFloors[1] = new Floor(this, 1, 254); arrayOfAllFloors[2] = new Floor(this, 2, 144); arrayOfAllFloors[3] = new Floor(this, 3, 32); //Initialize elevators (each elevator starts on randomly choosen floor) arrayOfAllElevators = new Elevator[3]; Random random = new Random(); arrayOfAllElevators[0] = new Elevator(this, 124, arrayOfAllFloors[random.Next(arrayOfAllFloors.Length)]); arrayOfAllElevators[1] = new Elevator(this, 210, arrayOfAllFloors[random.Next(arrayOfAllFloors.Length)]); arrayOfAllElevators[2] = new Elevator(this, 295, arrayOfAllFloors[random.Next(arrayOfAllFloors.Length)]); //Initialize list of all people inside (to track who's inside and need to be animated) ListOfAllPeopleWhoNeedAnimation = new List<Passenger>(); //Initialize ElevatorManager object ElevatorManager = new ElevatorManager(ArrayOfAllElevators, ArrayOfAllFloors); }
public ElevatorManager(Elevator[] ArrayOfAllElevators, Floor[] ArrayOfAllFloors) { //Initialize array with elevators this.arrayOfAllElevators = ArrayOfAllElevators; //Subscribe to elevators' events for (int i = 0; i < arrayOfAllElevators.Length; i++) { arrayOfAllElevators[i].ElevatorIsFull += new EventHandler(ElevatorManager_ElevatorIsFull); //Subscribe to all ElevatorIsFull events } //Initialize array with floors this.arrayOfAllFloors = ArrayOfAllFloors; //Initialize list of free elevators this.listOfAllFreeElevators = new List<Elevator>(); //Launch timer to periodically check, if some floor doesn't need an elevator this.floorChecker = new System.Timers.Timer(1000); this.floorChecker.Elapsed += new ElapsedEventHandler(this.ElevatorManager_TimerElapsed); this.floorChecker.Start(); }
public Elevator(Building Mybuilding, int HorizontalPosition, Floor StartingFloor) { this.myBuilding = Mybuilding; this.currentFloor = StartingFloor; this.listOfFloorsToVisit = new List<Floor>(); this.elevatorDirection = Direction.None; this.elevatorStatus = ElevatorStatus.Idle; this.maximumPeopleInside = 2; this.listOfPeopleInside = new List<Passenger>(); this.IsFull = false; this.elevatorPosition = new Point(HorizontalPosition, currentFloor.GetFloorLevelInPixels()); currentFrameNumber = 0; elevatorFrames = new Bitmap[] { Properties.Resources.LiftDoors_Open, Properties.Resources.LiftDoors_4, Properties.Resources.LiftDoors_3, Properties.Resources.LiftDoors_2, Properties.Resources.LiftDoors_1, Properties.Resources.LiftDoors_Closed }; this.elevatorAnimationDelay = 8; this.elevatorTimer = new System.Timers.Timer(5000); //set timer to 5 seconds this.elevatorTimer.Elapsed += new ElapsedEventHandler(this.Elevator_ElevatorTimerElapsed); this.PassengerEnteredTheElevator += new EventHandler(this.Elevator_PassengerEnteredTheElevator); //Add new elevator to floor's list currentFloor.AddRemoveElevatorToTheListOfElevatorsWaitingHere(this, true); }
public void AddNewFloorToTheList(Floor FloorToBeAdded) { lock (locker) //Method can be invoked from ElevatorManager thread (SendAnElevator()) or passenger's thread (AddNewPassengerIfPossible()) { //If FloorToBeAdded is already on the list, do nothing if(GetListOfAllFloorsToVisit().Contains(FloorToBeAdded)) { return; } //If elevator is going up if (this.currentFloor.FloorIndex < FloorToBeAdded.FloorIndex) { for (int i = this.currentFloor.FloorIndex + 1; i <= FloorToBeAdded.FloorIndex; i++) { if (!GetListOfAllFloorsToVisit().Contains(myBuilding.ArrayOfAllFloors[i])) { GetListOfAllFloorsToVisit().Add(myBuilding.ArrayOfAllFloors[i]); } } } //If elevator is going down if (this.currentFloor.FloorIndex > FloorToBeAdded.FloorIndex) { for (int i = this.currentFloor.FloorIndex - 1; i >= FloorToBeAdded.FloorIndex; i--) { if (!GetListOfAllFloorsToVisit().Contains(myBuilding.ArrayOfAllFloors[i])) { this.GetListOfAllFloorsToVisit().Add(myBuilding.ArrayOfAllFloors[i]); } } } //Update ElevatorDirection UpdateElevatorDirection(); } }
public Passenger(Building MyBuilding, Floor CurrentFloor, int TargetFloorIndex) { this.myBuilding = MyBuilding; this.currentFloor = CurrentFloor; this.currentFloorIndex = CurrentFloor.FloorIndex; this.passengerStatus = PassengerStatus.WaitingForAnElevator; this.targetFloor = MyBuilding.ArrayOfAllFloors[TargetFloorIndex]; this.targetFloorIndex = TargetFloorIndex; this.PassengerPosition = new Point(); Random random = new Random(); this.thisPassengerGraphic = new Bitmap(Passenger.ArrayOfAllPassengerGraphics[random.Next(ArrayOfAllPassengerGraphics.Length)]); this.visible = true; this.passengerAnimationDelay = 8; //Subscribe to events this.currentFloor.NewPassengerAppeared += new EventHandler(currentFloor.Floor_NewPassengerAppeared); this.currentFloor.NewPassengerAppeared += new EventHandler(this.Passenger_NewPassengerAppeared); this.currentFloor.ElevatorHasArrivedOrIsNotFullAnymore += new EventHandler(this.Passenger_ElevatorHasArrivedOrIsNoteFullAnymore); }
public void PassengerNeedsAnElevator(Floor PassengersFloor, Direction PassengersDirection) { lock (locker)//Can be invoked from ElevatorManager thread or its timer thread { //Turn on appropriate lamp on the floor if (PassengersDirection == Direction.Up) { PassengersFloor.LampUp = true; } else if (PassengersDirection == Direction.Down) { PassengersFloor.LampDown = true; } //Search elevator FindAllElevatorsWhichCanBeSent(PassengersFloor, PassengersDirection); Elevator ElevatorToSend = ChooseOptimalElevatorToSend(PassengersFloor); if (ElevatorToSend != null) { SendAnElevator(ElevatorToSend, PassengersFloor); } } }
private void SendAnElevator(Elevator ElevatorToSend, Floor TargetFloor) { ElevatorToSend.AddNewFloorToTheList(TargetFloor); //Create new thread and send the elevator ThreadPool.QueueUserWorkItem(delegate { ElevatorToSend.PrepareElevatorToGoToNextFloorOnTheList(); }); }
private void FindAllElevatorsWhichCanBeSent(Floor PassengersFloor, Direction PassengersDirection) { listOfAllFreeElevators.Clear(); //Find elevators in their way to Passenger's floor (e.g. called by someone else) for (int i = 0; i < arrayOfAllElevators.Length; i++) { //Get list of floors to visit List<Floor> ListOfFloorsToVisit = arrayOfAllElevators[i].GetListOfAllFloorsToVisit(); //Check list of floors to visit if (ListOfFloorsToVisit.Contains(PassengersFloor)) { listOfAllFreeElevators.Clear(); return; //Some elevator is already in its way, no need to send new one } } //Find elevators, which are idling now (do not moving anywhere) for (int i = 0; i < arrayOfAllElevators.Length; i++) { if (arrayOfAllElevators[i].GetElevatorStatus() == ElevatorStatus.Idle) { listOfAllFreeElevators.Add(arrayOfAllElevators[i]); } } }
private Elevator ChooseOptimalElevatorToSend(Floor FloorWhereTheCallCameFrom) { //Check if listOfAllFreeElevators is not empty if (listOfAllFreeElevators.Count == 0) { return null; } //Return first elevator from the list return listOfAllFreeElevators[0]; }
private void GoToNextFloorOnTheList() { //Move control on the UI if (elevatorDirection == Direction.Down) //move down { this.SetElevatorStatus(ElevatorStatus.GoingDown); this.MoveTheElevatorGraphicDown(GetNextFloorToVisit().GetFloorLevelInPixels()); } else if (elevatorDirection == Direction.Up) //move up { this.SetElevatorStatus(ElevatorStatus.GoingUp); this.MoveTheElevatorGraphicUp(GetNextFloorToVisit().GetFloorLevelInPixels()); } //Update currentFloor this.currentFloor = GetNextFloorToVisit(); //Remove current floor from the list of floors to visit this.listOfFloorsToVisit.RemoveAt(0); //Update elevator's direction UpdateElevatorDirection(); //If one of passengers inside wants to get out here or this is end of the road, //then finalize going to next floor on the list if (SomePassengersWantsToGetOutOnThisFloor() || (this.elevatorDirection == Direction.None)) { FinalizeGoingToNextFloorOnTheList(); return; } //If elevator is not full, then check lamps on the floor if (!this.IsFull) { if (((this.elevatorDirection == Direction.Up) && (currentFloor.LampUp)) || ((this.elevatorDirection == Direction.Down) && (currentFloor.LampDown))) { FinalizeGoingToNextFloorOnTheList(); return; } } //If elevator doesn't stop here, let it go to next floor GoToNextFloorOnTheList(); }
public bool AddNewPassengerIfPossible(Passenger NewPassenger, Floor TargetFloor) { //Passengers are added synchronically. Lock not needed. if (!IsFull && //check, if there is a place for another passenger ((GetElevatorStatus() == ElevatorStatus.Idle) || (GetElevatorStatus() == ElevatorStatus.WaitingForPassengersToGetInAndGetOut))) { //Reset elevator timer, so the passenger has time to get in this.ResetElevatorTimer(); this.listOfPeopleInside.Add(NewPassenger); //add new passenger this.AddNewFloorToTheList(TargetFloor); //add new floor if (this.listOfPeopleInside.Count >= this.maximumPeopleInside) //set flag, if needed { this.IsFull = true; this.SetElevatorStatus(ElevatorStatus.PreparingForJob); // to prevent other passengers attempt to get in } return true; //new passenger added successfully } else return false; //new passenger not added due to lack of space in the elevator }