Created
January 13, 2016 23:06
-
-
Save tuefekci/be0535fd3dc048acfb6f to your computer and use it in GitHub Desktop.
elevator level5.js - same like level 4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
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