Advertisement
Guest User

Untitled

a guest
May 25th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.98 KB | None | 0 0
  1. {
  2. init: function(elevators, floors) {
  3. var elevator = elevators[0]; // Let's use the first elevator
  4. var pressedFloorNumbers = [];
  5.  
  6. for (var i = 0; i < floors.length; i++) {
  7.  
  8. // Накапливаем информацию об этажах, на которых нажаты кнопки вверх или вниз
  9. floors[i].on("up_button_pressed", function(floor) {
  10. if (pressedFloorNumbers.indexOf(floor.floorNum()) === -1) {
  11. pressedFloorNumbers.push(floor.floorNum());
  12. console.log(pressedFloorNumbers);
  13. }
  14. });
  15. floors[i].on("down_button_pressed", function(floor) {
  16. if (pressedFloorNumbers.indexOf(floor.floorNum()) === -1) {
  17. pressedFloorNumbers.push(floor.floorNum());
  18. console.log(pressedFloorNumbers);
  19. }
  20. });
  21.  
  22. }
  23.  
  24. // Когда в лифте нажали на цифру - едем туда
  25. elevator.on("floor_button_pressed", function(floorNum){
  26. elevator.goToFloor(floorNum);
  27. });
  28.  
  29. // Когда лифт останавливается на этаже удаляем номер этого этажа из массива ожидающих
  30. elevator.on("stopped_at_floor", function (floorNum) {
  31. var index = pressedFloorNumbers.indexOf(floorNum);
  32.  
  33. if (index !== -1) {
  34. pressedFloorNumbers.splice(index, 1);
  35. }
  36. });
  37.  
  38.  
  39. // Когда нет задач лифт идет на первый из очереди нажатых
  40. elevator.on("idle", function() {
  41. if (pressedFloorNumbers.length > 0) {
  42. elevator.goToFloor(pressedFloorNumbers[0]);
  43. }
  44. });
  45. },
  46. update: function(dt, elevators, floors) {
  47. // We normally don't need to do anything here
  48. }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement