Skip to content

Instantly share code, notes, and snippets.

@pedroxs
Created November 19, 2014 02:57
Show Gist options
  • Select an option

  • Save pedroxs/ec8dfa1828a84de8aa0d to your computer and use it in GitHub Desktop.

Select an option

Save pedroxs/ec8dfa1828a84de8aa0d to your computer and use it in GitHub Desktop.
{
init: function(elevators, floors) {
//current #challenge=7
var idleElevators = _.range(elevators.length);
elevators.forEach(function(elevator, index) {
elevator.index = index;
elevator.on("idle", updateIdleQueue);
elevator.on("floor_button_pressed", traverse);
elevator.on("goto_floor", traverse);
});
floors.forEach(function(floor) {
floor.on("up_button_pressed", function() {
pickElevator(this.floorNum());
});
floor.on("down_button_pressed", function() {
pickElevator(this.floorNum());
});
});
function updateIdleQueue() {
idleElevators.push(this.index);
}
function traverse(floorNum) {
if(!_.contains(this.destinationQueue, floorNum)) {
this.goToFloor(floorNum);
}
}
function pickElevator(floorNum) {
if(_.isEmpty(idleElevators)) return;
var elevatorIndex = _.sample(idleElevators);
_.pull(idleElevators, elevatorIndex);
elevators[elevatorIndex].trigger("goto_floor", 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