Created
November 19, 2014 02:57
-
-
Save pedroxs/ec8dfa1828a84de8aa0d to your computer and use it in GitHub Desktop.
This file contains hidden or 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) { | |
| //current #challenge=7 | |
| var idleElevators = _.range(elevators.length); | |
| elevators.forEach(function(elevator, index) { | |
| elevator.index = index; | |
| elevator.on("idle", updateIdleQueue); | |
| elevator.on("floor_button_pressed", traverse); | |
| elevator.on("goto_floor", traverse); | |
| }); | |
| floors.forEach(function(floor) { | |
| floor.on("up_button_pressed", function() { | |
| pickElevator(this.floorNum()); | |
| }); | |
| floor.on("down_button_pressed", function() { | |
| pickElevator(this.floorNum()); | |
| }); | |
| }); | |
| function updateIdleQueue() { | |
| idleElevators.push(this.index); | |
| } | |
| function traverse(floorNum) { | |
| if(!_.contains(this.destinationQueue, floorNum)) { | |
| this.goToFloor(floorNum); | |
| } | |
| } | |
| function pickElevator(floorNum) { | |
| if(_.isEmpty(idleElevators)) return; | |
| var elevatorIndex = _.sample(idleElevators); | |
| _.pull(idleElevators, elevatorIndex); | |
| elevators[elevatorIndex].trigger("goto_floor", floorNum); | |
| } | |
| }, | |
| 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