Skip to content

Instantly share code, notes, and snippets.

@jnewc
Last active August 29, 2015 14:14
Show Gist options
  • Save jnewc/2ae116f8eb67405c0119 to your computer and use it in GitHub Desktop.
Save jnewc/2ae116f8eb67405c0119 to your computer and use it in GitHub Desktop.
Elevator Saga Simple Algorithm ( < Challenge 6)
{
init: function(elevators, floors) {
var findElevator = function() {
return _.find(elevators, function(e) { return e.loadFactor() < 0.5; });
};
var setupFloor = function(floor) {
floor.on('up_button_pressed', function(floorNum) {
console.log("UP: ", this.level);
var e = findElevator();
e && e.goToFloor(this.level);
});
floor.on('down_button_pressed', function(floorNum) {
console.log("DOWN: ", this.level);
var e = findElevator();
e && e.goToFloor(this.level);
});
};
var setupElevator = function(elevator) {
elevator.on('floor_button_pressed', function(floorNum) {
elevator.goToFloor(floorNum);
});
elevator.on('idle', function() {
});
};
floors.forEach(setupFloor);
elevators.forEach(setupElevator);
},
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