Last active
August 29, 2015 14:14
-
-
Save seriallos/ec3b658e677158d46956 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) { | |
numElevators = elevators.length; | |
topFloor = floors.length - 1; | |
bottomFloor = 0; | |
rotator = 0; | |
var nextElevator = function() { | |
rotator++; | |
return elevators[rotator % numElevators]; | |
} | |
elevators.forEach(function(el, i) { | |
el.id = i; | |
el.dedupeQueue = function() { | |
el.destinationQueue = _.uniq(el.destinationQueue); | |
el.checkDestinationQueue(); | |
} | |
el.sortQueue = function() { | |
queueAbove = _.filter(el.destinationQueue, function(num) { | |
return num > el.currentFloor(); | |
}).sort(); | |
queueBelow = _.filter(el.destinationQueue, function(num) { | |
return num < el.currentFloor(); | |
}).sort().reverse(); | |
queueAbove.sort(); | |
el.destinationQueue = []; | |
if(el.goingUpIndicator()) { | |
el.destinationQueue = _.union(queueAbove, queueBelow); | |
} else { | |
el.destinationQueue = _.union(queueBelow, queueAbove); | |
} | |
el.checkDestinationQueue(); | |
console.log(el.destinationQueue); | |
} | |
el.on("idle", function() { | |
dir = i % 2 ? 'up' : 'down' | |
el.goToFloor(dir == 'up' ? bottomFloor : topFloor); | |
if(dir == 'up') { | |
el.goingUpIndicator(true); | |
} else { | |
el.goingDownIndicator(true); | |
} | |
el.dedupeQueue() | |
}); | |
el.on("floor_button_pressed", function(num) { | |
el.goToFloor(num); | |
el.dedupeQueue() | |
el.sortQueue(); | |
}); | |
el.on("stopped_at_floor", function(num) { | |
el.dedupeQueue(); | |
el.sortQueue(); | |
next = el.destinationQueue[0]; | |
if(next > num) { | |
el.goingUpIndicator(true); | |
} else { | |
el.goingDownIndicator(true); | |
} | |
}); | |
}); | |
floors.forEach(function(floor) { | |
floor.on("up_button_pressed", function() { | |
el = nextElevator(); | |
while(el.loadFactor >= 0.8) { | |
el.nextElevator(); | |
} | |
el.goToFloor(floor.floorNum()); | |
}); | |
floor.on("down_button_pressed", function() { | |
el = nextElevator(); | |
while(el.loadFactor >= 0.8) { | |
el.nextElevator(); | |
} | |
el.goToFloor(floor.floorNum()); | |
}); | |
}); | |
}, | |
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