Skip to content

Instantly share code, notes, and snippets.

@vcarl
Last active August 29, 2015 14:14
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 vcarl/b7714da9d1b595505556 to your computer and use it in GitHub Desktop.
Save vcarl/b7714da9d1b595505556 to your computer and use it in GitHub Desktop.
{
init: function(elevators, floors) {
setTimeout(function(){
window.queue = [];
function removeIfInQueue(floor) {
window.queue.forEach(function(val, index) {
if (val.floor === floor) {
window.queue.splice(index, 1);
}
});
}
floors.forEach(function(floor) {
floor.on("up_button_pressed", function() {
window.queue.unshift({floor: floor.floorNum(), direction: 'up'});
});
floor.on("down_button_pressed", function() {
window.queue.unshift({floor: floor.floorNum(), direction: 'down'});
});
});
elevators.forEach(function(elevator, index) {
elevator.on("idle", function() {
if (window.queue.length > 0) {
elevator.goToFloor(window.queue[0].floor);
} else {
//if (index > elevators.length / 2) {
elevator.goToFloor(0);
//} else {
// elevator.goToFloor(Math.floor(floors.length / 2));
//}
}
});
elevator.on("stopped_at_floor", function(floor) {
removeIfInQueue(floor);
elevator.lastFloor = floor;
if (elevator.destinationQueue[0] > elevator.currentFloor()) {
elevator.goingUpIndicator(true);
} else if (elevator.destinationQueue[0] < elevator.currentFloor()) {
elevator.goingDownIndicator(true);
}
});
elevator.on("passing_floor", function(floor, direction) {
function inQueue(floor, direction) {
var stop = false;
window.queue.forEach(function(val) {
if (val.floor === floor && val.direction === direction) {
stop = true;
}
});
return stop;
}
var stop = inQueue(floor, direction);
var dest = elevator.destinationQueue.indexOf(floor);
if (dest !== -1) {
elevator.destinationQueue.splice(dest, 1);
elevator.checkDestinationQueue();
stop = true
}
if (stop) {
removeIfInQueue(floor);
elevator.goToFloor(floor);
}
});
elevator.on("floor_button_pressed", function(floor) {
removeIfInQueue(floor);
elevator.goToFloor(floor);
});
elevator.goToFloor(0);
});
});
},
update: function(dt, elevators, floors) {
elevators.forEach(function(elevator, index) {
elevator.destinationQueue.sort(function(a, b){
return Math.abs(a - elevator.lastFloor) - Math.abs(b - elevator.lastFloor);
});
elevator.checkDestinationQueue();
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment