Last active
August 29, 2015 14:14
-
-
Save vcarl/b7714da9d1b595505556 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
{ | |
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