Created
January 23, 2015 20:13
-
-
Save kitsunde/0856f280735f74cb64ba 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) { | |
passengersGoingDown = new Set(); | |
passengersGoingUp = new Set(); | |
idleElevators = []; | |
function provisionElevatorOrAdd(queue){ | |
return function(){ | |
if(idleElevators.length){ | |
// We should probbly pick the cloesest one. | |
var elevator = idleElevators.shift(); | |
elevator.goToFloor(this.level); | |
elevator.goingUpIndicator(elevator.currentFloor < this.level); | |
elevator.goingDownIndicator(elevator.currentFloor > this.level); | |
}else{ | |
queue.add(this.level); | |
} | |
} | |
} | |
floors.forEach(function(floor){ | |
floor.on("up_button_pressed", provisionElevatorOrAdd(passengersGoingUp)); | |
floor.on("down_button_pressed", provisionElevatorOrAdd(passengersGoingDown)); | |
}); | |
elevators.forEach(function(elevator){ | |
elevator.on("idle", function(){ | |
idleElevators.push(elevator); | |
}); | |
elevator.on("floor_button_pressed", function(floorNum){ | |
// Can this happen at any point? Docs are unclear. | |
elevator.goToFloor(floorNum); | |
}); | |
elevator.on("passing_floor", function(floorNum, direction){ | |
// If we are passing a floor, and we have space for more passangers, | |
// and they are going in the same direction, pick them up. | |
if(elevator.loadFactor < 1 && ( | |
elevator.goingUpIndicator() && passengersGoingUp.has(floorNum) || | |
elevator.goingDownIndicator() && passengersGoingDown.has(floorNum)) | |
){ | |
elevator.goToFloor(floorNum, true); | |
elevator.checkDestinationQueue(); | |
} | |
}); | |
elevator.on("stopped_at_floor", function(floorNum){ | |
// If we came to pickup passengers and the light is still on after pickup, we should reschedule. | |
if(elevator.goingUpIndicator() && floors[floorNum].up){ | |
provisionElevatorOrAdd(passangersGoingUp)(floorNum); | |
} | |
if(elevator.goingDownIndicator() && floors[floorNum].down){ | |
provisionElevatorOrAdd(passangersGoingDown)(floorNum); | |
} | |
// Set the desitnation to the nearest queued floor. | |
elevator.destinationQueue = elevator.destinationQueue.sort(function(a,b){return Math.abs(a-4) > Math.abs(b-4)}); | |
elevator.checkDestinationQueue(); | |
var nextFloor = elevator.destinationQueue[0]; | |
elevator.goingUpIndicator(nextFloor === undefined || floorNum < nextFloor); | |
elevator.goingDownIndicator(nextFloor === undefined || floorNum > nextFloor); | |
}); | |
}); | |
}, | |
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