Skip to content

Instantly share code, notes, and snippets.

@kutyel
Last active August 29, 2015 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kutyel/37af5e7dedeb241f45ef to your computer and use it in GitHub Desktop.
Save kutyel/37af5e7dedeb241f45ef to your computer and use it in GitHub Desktop.
This is my awesome algorythm for the Elevator Saga browser game!
{
init: function(elevators, floors) {
var reqFloor = 0, dir = "";
floors.forEach(function(floor){
floor.on("up_button_pressed", function(){
dir = "up";
reqFloor = floor.floorNum();
});
floor.on("down_button_pressed", function(){
dir = "down";
reqFloor = floor.floorNum();
});
});
elevators.forEach(function(elevator){
elevator.on("passing_floor", function(floor, direction){
if (direction === dir && floor === reqFloor && elevator.loadFactor() < 0.75){
elevator.goToFloor(reqFloor, true);
}
})
elevator.on("floor_button_pressed", function(numFloor){
elevator.goToFloor(numFloor);
});
elevator.on("idle", function(){
elevator.goToFloor(reqFloor);
});
});
},
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