Guest User

http://play.elevatorsaga.com/

a guest
Jan 29th, 2015
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. {
  2.     init: function(elevators, floors) {
  3.         var elevator = elevators[0]; // Let's use the first elevator
  4.         var up = [];
  5.         var down = [];
  6.         floors.forEach(function(floor) {
  7.             floor.on("up_button_pressed", function() {
  8.                 up.push(floor.floorNum());
  9.             });
  10.             floor.on("down_button_pressed", function() {
  11.                 down.push(floor.floorNum());
  12.             });
  13.         });
  14.         elevator.on("idle stopped_at_floor", function() {
  15.             var floorNum = 0;
  16.             if (elevator.getPressedFloors().length > 0) {
  17.                 if (elevator.goingUpIndicator()) {
  18.                     floorNum = Math.max.apply(Math, elevator.getPressedFloors());
  19.                 }
  20.                 else {
  21.                     floorNum = Math.min.apply(Math, elevator.getPressedFloors());
  22.                 }
  23.             }
  24.             else if (up.length > 0)
  25.             {
  26.                 if (down.length > 0)
  27.                 {
  28.                     if (elevator.currentFloor() >= floors.length / 2)
  29.                     {
  30.                         floorNum = Math.max.apply(Math, down);
  31.                     }
  32.                     else
  33.                     {
  34.                         floorNum = Math.min.apply(Math, up);
  35.                     }
  36.                 }
  37.                 else
  38.                 {
  39.                     floorNum = Math.min.apply(Math, up);
  40.                 }
  41.             }
  42.             else if (down.length > 0)
  43.             {
  44.                 floorNum = Math.max.apply(Math, down);
  45.             }
  46.             elevator.goToFloor(floorNum);
  47.             if (floorNum < elevator.currentFloor()) {
  48.                 elevator.goingUpIndicator(false);
  49.                 elevator.goingDownIndicator(true);
  50.                 if (down.indexOf(elevator.currentFloor()) != -1) {
  51.                     down.splice(down.indexOf(elevator.currentFloor()), 1);
  52.                 }
  53.             }
  54.             else if (floorNum > elevator.currentFloor()) {
  55.                 elevator.goingUpIndicator(true);
  56.                 elevator.goingDownIndicator(false);
  57.                 if (up.indexOf(elevator.currentFloor()) != -1) {
  58.                     up.splice(up.indexOf(elevator.currentFloor()), 1);
  59.                 }
  60.             }
  61.             else {
  62.                 elevator.goingUpIndicator(true);
  63.                 elevator.goingDownIndicator(true);
  64.             }
  65.         });
  66.         elevator.on("floor_button_pressed", function(floorNum) {
  67.             if (elevator.destinationQueue.length == 0) {
  68.                 elevator.goToFloor(floorNum);
  69.                 if (floorNum < elevator.currentFloor()) {
  70.                     elevator.goingUpIndicator(false);
  71.                     elevator.goingDownIndicator(true);
  72.                     if (down.indexOf(elevator.currentFloor()) != -1) {
  73.                         down.splice(down.indexOf(elevator.currentFloor()), 1);
  74.                     }
  75.                 }
  76.                 else if (floorNum > elevator.currentFloor()) {
  77.                     elevator.goingUpIndicator(true);
  78.                     elevator.goingDownIndicator(false);
  79.                     if (up.indexOf(elevator.currentFloor()) != -1) {
  80.                         up.splice(up.indexOf(elevator.currentFloor()), 1);
  81.                     }
  82.                 }
  83.                 else {
  84.                     elevator.goingUpIndicator(true);
  85.                     elevator.goingDownIndicator(true);
  86.                 }
  87.             }
  88.         });
  89.         elevator.on("passing_floor", function(floorNum, direction) {
  90.             var bShouldStop = false;
  91.             if (elevator.getPressedFloors().indexOf(floorNum) != -1) {
  92.                 bShouldStop = true;
  93.             } else if (elevator.loadFactor() < 0.6) {
  94.                 if (direction == "up" && up.indexOf(floorNum) != -1) {
  95.                     bShouldStop = true;
  96.                 } else if (direction == "down" && down.indexOf(floorNum) != -1) {
  97.                     bShouldStop = true;
  98.                 }
  99.             }
  100.             if (bShouldStop) {
  101.                 elevator.stop();
  102.                 elevator.goToFloor(floorNum);
  103.             }
  104.         });
  105.     },
  106.     update: function(dt, elevators, floors) {
  107.         // We normally don't need to do anything here
  108.     }
  109. }
Add Comment
Please, Sign In to add comment