Last active
August 8, 2017 22:25
-
-
Save buttercookie42/a91e5defbfd7014c334f45d0a62c9a04 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) { | |
const DIR_NONE = 0x0; | |
const DIR_UP = 0x1; | |
const DIR_DOWN = 0x2; | |
let floorCount = floors.length; | |
let upRequests = initArray(floorCount); | |
let downRequests = initArray(floorCount); | |
for (var elevator of elevators) { | |
initElevator(elevator); | |
} | |
for (var floor of floors) { | |
initFloor(floor); | |
} | |
function initElevator(elevator) { | |
elevator.requests = initArray(floorCount); | |
elevator.setDirection = function(direction) { | |
let goingUp = direction & DIR_UP; | |
let goingDown = direction & DIR_DOWN; | |
elevator.goingUpIndicator(goingUp); | |
elevator.goingDownIndicator(goingDown); | |
} | |
elevator.handleRequest = function(floorNum, direction) { | |
if (elevator.destinationDirection() == "stopped") { | |
elevator.setDirection(direction); | |
elevator.goToFloor(floorNum); | |
} | |
} | |
// Whenever the elevator is idle (has no more queued destinations) ... | |
elevator.on("idle", function() { | |
elevator.setDirection(DIR_NONE); | |
}); | |
elevator.on("floor_button_pressed", function(floorNum) { | |
elevator.requests[floorNum] = true; | |
let direction = floorNum - elevator.currentFloor() > 0 ? DIR_UP : DIR_DOWN; | |
elevator.handleRequest(floorNum, direction); | |
}); | |
elevator.on("passing_floor", function(floorNum, direction) { | |
let externalRequests = direction == "up" ? upRequests : downRequests; | |
if (externalRequests[floorNum] && elevator.loadFactor() < 0.95) { | |
elevator.goToFloor(floorNum, true); | |
} | |
}); | |
elevator.on("stopped_at_floor", function(floorNum) { | |
elevator.requests[floorNum] = false; | |
upRequests[floorNum] = false; | |
downRequests[floorNum] = false; | |
}); | |
} | |
function initFloor(floor) { | |
let floorNum = floor.floorNum(); | |
floor.on("up_button_pressed", function() { | |
upRequests[floorNum] = true; | |
getElevator(floorNum, DIR_UP).handleRequest(floorNum, DIR_UP); | |
}); | |
floor.on("down_button_pressed", function() { | |
downRequests[floorNum] = true; | |
getElevator(floorNum, DIR_DOWN).handleRequest(floorNum, DIR_DOWN); | |
}); | |
} | |
function getElevator(floorNum, direction) { | |
return elevators[0]; | |
} | |
function initArray(size) { | |
return new Array(size).fill(false); | |
} | |
}, | |
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