Skip to content

Instantly share code, notes, and snippets.

@tuefekci
Created January 13, 2016 22:36
Show Gist options
  • Save tuefekci/017558c39b95ee6ecd32 to your computer and use it in GitHub Desktop.
Save tuefekci/017558c39b95ee6ecd32 to your computer and use it in GitHub Desktop.
elevetor level2.js
{
init: function(elevators, floors) {
var elevator = elevators[0]; // Let's use the first elevator
floors.forEach(function(floor) {
function addFloorToDestinationQueue(floor) {
if(elevator.destinationQueue.indexOf(floor) === -1) {
elevator.destinationQueue.push(floor);
}
}
floor.on("up_button_pressed", function() {
addFloorToDestinationQueue(floor.level);
});
floor.on("down_button_pressed", function() {
addFloorToDestinationQueue(floor.level);
});
});
// Whenever the elevator is idle (has no more queued destinations) ...
elevator.on("idle", function() {
baseFloor = Math.round((floors.length-1)/2);
if(elevator.currentFloor() !== baseFloor) {
elevator.goToFloor(baseFloor);
}
elevator.on("floor_button_pressed", function(floorNum) {
elevator.goToFloor(floorNum);
});
// let's go to all the floors (or did we forget one?)
//elevator.goToFloor(0);
//elevator.goToFloor(1);
//elevator.goToFloor(2);
});
},
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