Skip to content

Instantly share code, notes, and snippets.

@ergusto
Created January 24, 2015 09:03
Show Gist options
  • Save ergusto/730df778fe11ca955232 to your computer and use it in GitHub Desktop.
Save ergusto/730df778fe11ca955232 to your computer and use it in GitHub Desktop.
Solution for http://play.elevatorsaga.com/ levels 1-6
{
init: function(elevators, floors) {
var waitingFloors = [];
_.each(elevators, function(elevator) {
elevator.on("floor_button_pressed", function(floorNum) {
elevator.goToFloor(floorNum);
});
elevator.on("up_button_pressed", function(floorNum) {
elevator.goToFloor(floorNum);
});
elevator.on("idle", function() {
var waiting = waitingFloors[0];
if (waiting) {
elevator.goToFloor(waiting.level);
} else {
elevator.goToFloor(0);
}
});
elevator.on("stopped_at_floor", function(floorNum) {
var floor = floors[floorNum];
var index = waitingFloors.indexOf(floor);
if (index) {
waitingFloors.splice(index, 1);
}
});
});
_.each(floors, function(floor) {
floor.on("up_button_pressed down_button_pressed", function() {
waitingFloors.push(floor);
});
});
},
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