Skip to content

Instantly share code, notes, and snippets.

@tuefekci
Created January 13, 2016 23:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tuefekci/c004d98e28f1a44fd57e to your computer and use it in GitHub Desktop.
Save tuefekci/c004d98e28f1a44fd57e to your computer and use it in GitHub Desktop.
elevator level4.js
{
init: function(elevators, floors) {
console.log(elevators);
// # Elevators
elevators.forEach(function(elevator) {
elevator.on("stopped_at_floor", function(floorNum) {
if(elevator.destinationQueue.indexOf(floorNum) !== -1) {
elevator.destinationQueue.splice(elevator.destinationQueue.indexOf(floorNum), 1);
}
});
// 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(0);
}
elevator.on("floor_button_pressed", function(floorNum) {
elevator.goToFloor(floorNum);
});
});
});
// # Floors
floors.forEach(function(floor) {
function addFloorToDestinationQueue(floor) {
var elevatorWithLowestCount = elevators[0];
elevators.forEach(function(elevator) {
if(elevator.destinationQueue.lenght < elevatorWithLowestCount.destinationQueue.lenght) {
elevatorWithLowestCount = elevator;
}
});
elevatorWithLowestCount.destinationQueue.push(floor);
}
floor.on("up_button_pressed", function() {
addFloorToDestinationQueue(floor.level);
});
floor.on("down_button_pressed", function() {
addFloorToDestinationQueue(floor.level);
});
});
},
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