Skip to content

Instantly share code, notes, and snippets.

@fosterbrereton
Created January 13, 2016 08:06
Show Gist options
  • Save fosterbrereton/6634745beeb421d49532 to your computer and use it in GitHub Desktop.
Save fosterbrereton/6634745beeb421d49532 to your computer and use it in GitHub Desktop.
Elevator Saga (play.elevatorsaga.com)
{
init: function(elevators, floors) {
var closestToFloor = function (floor) {
var elevator = null;
var distance = 1000;
console.log("Call to floor " + floor + "...");
elevators.forEach(function (cur_elevator) {
var cur_distance = Math.abs(cur_elevator.currentFloor() - floor);
if (cur_distance >= distance) {
return;
}
distance = cur_distance;
elevator = cur_elevator;
});
return elevator;
};
var travel = function(elevator, floor) {
elevator.goToFloor(floor);
var direction = elevator.destinationDirection();
elevator.goingUpIndicator(direction != "down");
elevator.goingDownIndicator(direction != "up");
}
elevators.forEach(function (elevator) {
elevator.on("idle", function () {
travel(elevator, 0);
});
elevator.on("floor_button_pressed", function (floor) {
travel(elevator, floor);
});
});
floors.forEach(function(floor) {
floor.on("up_button_pressed", function () {
var curFloor = floor.floorNum();
var elevator = closestToFloor(curFloor);
if (elevator !== null) {
travel(elevator, curFloor);
}
});
floor.on("down_button_pressed", function () {
var curFloor = floor.floorNum();
var elevator = closestToFloor(curFloor);
if (elevator !== null) {
travel(elevator, curFloor);
}
});
});
},
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