Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- init: function(elevators, floors) {
- function is_in_array(arr,obj) {
- return (arr.indexOf(obj) != -1);
- }
- function elevatorMagic(elevator) {
- if (elevator.destinationQueue.length > 1 && elevator.destinationQueue[0] > elevator.currentFloor()) {
- // going up.
- elevator.goingUpIndicator(true);
- elevator.goingDownIndicator(false);
- elevator.destinationQueue.sort();
- }
- else if (elevator.destinationQueue.length > 1 && elevator.destinationQueue[0] < elevator.currentFloor()) {
- // going down.
- elevator.goingDownIndicator(true);
- elevator.goingUpIndicator(false);
- elevator.destinationQueue.sort().reverse();
- }
- // When somebody gets on an elevator and clicks a floor, add that to the queue.
- // only if it's not already in the queue, (todo) and we're going in the right direction.
- elevator.on("floor_button_pressed", function(floorNum) {
- if (!is_in_array(elevator.destinationQueue, floorNum)) {
- console.log('Queued ' + floorNum );
- elevator.goToFloor(floorNum);
- }
- else {
- console.log('Elevator already going to floor ' + floorNum);
- }
- });
- function callelevator(floorNum, direction) {
- // is an elevator already coming?
- var elevatorcoming = elevators.some(function (elevator, index) {
- if (is_in_array(elevator.destinationQueue, floorNum)) {
- console.log('Elevator ' + index + ' already on its way!');
- return true;
- }
- else {
- return false;
- }
- });
- if (!elevatorcoming) {
- console.log('No elevator already coming, finding a candidate elevator.');
- // Find a candidate elevator and send it this way.
- var elevatorqueued = elevators.some(function (elevator, index) {
- console.log('Trying elevator ' + index);
- // if going up and elevator is below us.
- if (direction == 'up' && elevator.currentFloor() <= floorNum && elevator.loadFactor < .8) {
- console.log('Elevator ' + index + ' is below us. Sending it to floor ' + floorNum);
- //console.log(elevator.destinationQueue);
- elevator.goingDownIndicator(false);
- elevator.goingUpIndicator(true);
- elevator.goToFloor(floorNum);
- //console.log(elevator.destinationQueue);
- return true;
- }
- else if (direction == 'down' && elevator.currentFloor() >= floorNum && elevator.loadFactor < .8) {
- console.log('Elevator ' + index + ' is above us. Sending it to floor ' + floorNum);
- //console.log(elevator.destinationQueue());
- elevator.goingDownIndicator(true);
- elevator.goingUpIndicator(false);
- elevator.goToFloor(floorNum);
- //console.log(elevator.destinationQueue());
- return true;
- }
- else if (elevator.destinationQueue.length == 0) {
- console.log('Elevator ' + index + ' is idle. Sending it to floor ' + floorNum + '!');
- if (elevator.currentFloor() < floorNum) {
- elevator.goingDownIndicator(false);
- elevator.goingUpIndicator(true);
- }
- else {
- elevator.goingDownIndicator(true);
- elevator.goingUpIndicator(false);
- }
- elevator.goToFloor(floorNum);
- return true;
- }
- console.log('no');
- return false;
- });
- }
- }
- // Check a floor for a button press, and if pressed, call an elevator.
- function checkforbutton(floor) {
- floor.on("up_button_pressed", function() {
- //elevator.goToFloor(floor.floorNum());
- console.log('Somebody wants to go *up* from floor *' + floor.floorNum() + '*');
- callelevator(floor.floorNum(), 'up');
- });
- floor.on("down_button_pressed", function() {
- //elevator.goToFloor(floor.floorNum());
- console.log('Somebody wants to go *down* from floor *' + floor.floorNum() + '*');
- callelevator(floor.floorNum(), 'down');
- });
- }
- // When an elevator is idle, wait until somebody clicks a button on a floor.
- elevator.on("idle", function() {
- console.log('Elevator is idle.');
- elevator.goToFloor(elevator.currentFloor());
- elevator.goingDownIndicator(true);
- elevator.goingUpIndicator(true);
- floors.forEach(checkforbutton);
- });
- }
- // Make magic with each elevator.
- elevators.forEach(elevatorMagic);
- },
- update: function(dt, elevators, floors) {
- // We normally don't need to do anything here
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement