Skip to content

Instantly share code, notes, and snippets.

@jkronz
Created January 27, 2015 19:25
Show Gist options
  • Save jkronz/3dcb7edd199d591c0399 to your computer and use it in GitHub Desktop.
Save jkronz/3dcb7edd199d591c0399 to your computer and use it in GitHub Desktop.
{
init: function(elevators, floors) {
var floorRequests = {
up: [],
down: []
};
elevators.map(function(elevator, i){
var destinations = [];
function nextDestination() {
destinations.sort();
nextDest = destinations.shift();
log('next destination', nextDest);
return nextDest;
}
function nextRequest() {
if (floorRequests.down.length > floorRequests.up.length) {
//more people want to go down, so go to the max of the downs.
} else {
//more people want to go up
}
}
function log(message) {
console.log(arguments);
}
elevator.on("idle", function() {
log("elevator is idle");
if (destinations.length > 0) {
elevator.goToFloor(nextDestination());
return;
}
elevator.goToFloor(0);
});
elevator.on("floor_button_pressed", function(floorNum) {
log("floor button pressed", floorNum);
destinations.unshift(floorNum);
});
elevator.on("passing_floor", function(floorNum, direction) {
log("passing floor", floorNum, direction);
// if we have a dropoff or someone that needs picked up going our way, stop
if (destinations[floorNum] || floorRequests[direction][floorNum]) {
elevator.goToFloor(floorNum, true);
}
});
elevator.on("stopped_at_floor", function(floorNum) {
log("stopped at floor", floorNum);
});
});
floors.map(function(floor, i) {
floor.on("up_button_pressed", function() {
floorRequests.up[i] = true;
});
floor.on("down_button_pressed", function() {
floorRequests.down[i] = true;
});
});
},
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