Created
January 24, 2015 11:19
-
-
Save ryan-scott-dev/bb57d35990f25d14b755 to your computer and use it in GitHub Desktop.
Elevators
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) { | |
// Find the closest elevator which isn't full | |
var findSuitableElevator = function(floor) { | |
var floorNum = floor.floorNum(); | |
var idealElevator = | |
_.chain(elevators) | |
.filter(function(elevator) { | |
return elevator.loadFactor() < 0.7; | |
}) | |
.sortBy(function(elevator) { | |
return (elevator.currentFloor() - floorNum); | |
}) | |
.first() | |
.value(); | |
var elevator = idealElevator || elevators[1]; | |
elevator.goToFloor(floorNum); | |
}; | |
_.each(floors, function(floor) { | |
floor.on("up_button_pressed down_button_pressed", function() { | |
findSuitableElevator(floor); | |
}); | |
}); | |
_.each(elevators, function(elevator) { | |
// elevator.on("idle", function() { | |
// this.goToFloor(0); | |
// }); | |
elevator.on("floor_button_pressed", function(floorNum) { | |
this.goToFloor(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