Created
January 27, 2015 19:25
-
-
Save jkronz/3dcb7edd199d591c0399 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) { | |
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