Skip to content

Instantly share code, notes, and snippets.

@hypomodern
Created March 10, 2015 18:39
Show Gist options
  • Save hypomodern/dbcc330f9fde05237fcc to your computer and use it in GitHub Desktop.
Save hypomodern/dbcc330f9fde05237fcc to your computer and use it in GitHub Desktop.
Elevator Saga
{
init: function(elevators, floors) {
elevators.forEach(function(elevator) {
elevator.floorIsDestination = function(floorNum) {
return elevator.destinationQueue.indexOf(floorNum) != -1;
}
elevator.on("floor_button_pressed", function(floorNum) {
if (!elevator.floorIsDestination(floorNum)) {
elevator.goToFloor(floorNum);
}
});
elevator.on("passing_floor", function(floorNum, direction) {
if (elevator.floorIsDestination(floorNum) && elevator.loadFactor() < 1) {
elevator.destinationQueue = elevator.destinationQueue.filter(function(f) { return f != floorNum; });
elevator.goToFloor(floorNum, true);
}
});
});
floors.forEach(function(floor) {
floor.on("up_button_pressed down_button_pressed", function() {
if (elevators.some(function(elevator) { return elevator.floorIsDestination(floor.floorNum()); })) {
return;
}
var vator = elevators[0];
for(var i = 0; i < elevators.length; i++)
if (elevators[i].destinationQueue.length < vator.destinationQueue.length && elevators[i].loadFactor() < 1) {
vator = elevators[i];
}
if (!vator.floorIsDestination(floor.floorNum())) {
vator.goToFloor(floor.floorNum());
}
});
});
},
update: function(dt, elevators, floors) { }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment