Skip to content

Instantly share code, notes, and snippets.

@ryan-scott-dev
Created January 24, 2015 11:19
Show Gist options
  • Save ryan-scott-dev/bb57d35990f25d14b755 to your computer and use it in GitHub Desktop.
Save ryan-scott-dev/bb57d35990f25d14b755 to your computer and use it in GitHub Desktop.
Elevators
{
init: function(elevators, floors) {
// Find the closest elevator which isn't full
var findSuitableElevator = function(floor) {
var floorNum = floor.floorNum();
var idealElevator =
_.chain(elevators)
.filter(function(elevator) {
return elevator.loadFactor() < 0.7;
})
.sortBy(function(elevator) {
return (elevator.currentFloor() - floorNum);
})
.first()
.value();
var elevator = idealElevator || elevators[1];
elevator.goToFloor(floorNum);
};
_.each(floors, function(floor) {
floor.on("up_button_pressed down_button_pressed", function() {
findSuitableElevator(floor);
});
});
_.each(elevators, function(elevator) {
// elevator.on("idle", function() {
// this.goToFloor(0);
// });
elevator.on("floor_button_pressed", function(floorNum) {
this.goToFloor(floorNum);
});
});
},
update: function(dt, elevators, floors) {
// We normally don't need to do anything here
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment