Created
May 17, 2016 16:46
-
-
Save megatolya/2f0da60b4336a012b2ddda033bf4ace9 to your computer and use it in GitHub Desktop.
This file contains 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
//http://play.elevatorsaga.com/#challenge=4 | |
{ | |
init: function(elevators, floors) { | |
var floorsQueue = []; | |
floors.forEach(function (floor) { | |
floor.on("up_button_pressed", function() { | |
if (floorsQueue.indexOf(floor.floorNum()) === -1) { | |
floorsQueue.push(floor.floorNum()); | |
} | |
}); | |
floor.on("down_button_pressed", function() { | |
if (floorsQueue.indexOf(floor.floorNum()) === -1) { | |
floorsQueue.push(floor.floorNum()); | |
} | |
}); | |
}); | |
var e = elevators[0]; // Let's use the first elevator | |
// Whenever the elevator is idle (has no more queued destinations) ... | |
e.on("idle", function() { | |
// let's go to all the floors (or did we forget one?) | |
console.log(e.destinationQueue); | |
console.log(e.getPressedFloors()); | |
var pressed = e.getPressedFloors(); | |
pressed.forEach(function (number) { | |
e.goToFloor(number); | |
}); | |
floorsQueue.forEach(function (number) { | |
e.goToFloor(number); | |
}); | |
floorsQueue = []; | |
// console.log(e.currentFloor()); | |
}); | |
}, | |
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