Skip to content

Instantly share code, notes, and snippets.

@radleta
Created January 23, 2015 12:30
Show Gist options
  • Save radleta/52f65fd2afba9b34d90b to your computer and use it in GitHub Desktop.
Save radleta/52f65fd2afba9b34d90b to your computer and use it in GitHub Desktop.
Elevator Saga Challenge #2
{
init: function(elevators, floors) {
var elevator = elevators[0]; // Let's use the first elevator
elevator.on("idle", function() {
// The elevator is idle, so let's go to all the floors (or did we forget one?)
elevator.goToFloor(2);
});
elevator.on("floor_button_pressed", function(floorNum) {
elevator.goToFloor(floorNum, true);
});
elevator.on("passing_floor", function(floorNum, direction) {
var stopAtFloor = false;
elevator.destinationQueue = elevator.destinationQueue.filter(function (n, i) {
if (n == floorNum) {
stopAtFloor = true;
}
return n != floorNum;
});
if (stopAtFloor) {
elevator.checkDestinationQueue();
elevator.goToFloor(floorNum, true);
}
});
function initFloor(floor) {
floor.on("up_button_pressed", function() {
// Maybe tell an elevator to go to this floor?
elevator.goToFloor(floor.floorNum());
});
floor.on("down_button_pressed", function() {
// Maybe tell an elevator to go to this floor?
elevator.goToFloor(floor.floorNum());
});
}
for (var floorNum = 0; floorNum < floors.length; floorNum++) {
var floor = floors[floorNum];
initFloor(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