Skip to content

Instantly share code, notes, and snippets.

@pfrenssen
Created January 27, 2015 00:21
Show Gist options
  • Save pfrenssen/5f8a2fc1e3bc7c80841d to your computer and use it in GitHub Desktop.
Save pfrenssen/5f8a2fc1e3bc7c80841d to your computer and use it in GitHub Desktop.
Elevatorsaga
{
init: function(elevators, floors) {
var queue = [];
elevators.forEach(function (elevator) {
elevator.on("idle", function() {
// If there is a floor in the queue, go there.
if (queue.length > 0) {
var floor = queue.shift();
goToFloor(elevator, floor);
}
else {
// Turn our lights on. We can go anywhere!
elevator.goingUpIndicator(true);
elevator.goingDownIndicator(true);
// If we remain stationary people don't get on unless we "go to our own floor".
// Presumably this causes the doors to open.
elevator.goToFloor(0);
}
});
elevator.on('floor_button_pressed', function (floor) {
goToFloor(elevator, floor);
});
elevator.on('stopped_at_floor', function(floor) {
// If we stop and are empty, turn our lights back on.
if (elevator.loadFactor() == 0) {
elevator.goingUpIndicator(true);
elevator.goingDownIndicator(true);
}
});
});
floors.forEach(function(floor) {
floor.on('up_button_pressed', function() {
queue.push(floor.floorNum());
});
floor.on('down_button_pressed', function() {
queue.push(floor.floorNum());
});
});
var goToFloor = function(elevator, floor) {
if (floor > elevator.currentFloor()) {
elevator.goingDownIndicator(false);
elevator.goingUpIndicator(true);
}
else {
elevator.goingDownIndicator(true);
elevator.goingUpIndicator(false);
}
elevator.goToFloor(floor);
}
},
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