Created
January 16, 2016 20:55
-
-
Save ddustin/2957e3004024bf0d1302 to your computer and use it in GitHub Desktop.
This file contains hidden or 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) { | |
function getMethod(i, method) { | |
var methods = | |
{ | |
"idle": function() { | |
var elevator = elevators[i]; | |
elevator.goToFloor(Math.round(floors.length * i / elevators.length)); | |
}, | |
"floor_button_pressed": function(floorNum) { | |
var elevator = elevators[i]; | |
elevator.goToFloor(floorNum); | |
getMethod(i, "sort_destination_queue")(i); | |
}, | |
"passing_floor": function(floorNum) { | |
var floor = floors[floorNum]; | |
if(floor.buttonStates.up || floor.buttonStates.down) { | |
elevators[i].goToFloor(floorNum); | |
getMethod(i, "sort_destination_queue")(i); | |
} | |
}, | |
"up_button_pressed": function() { | |
for(var j = 0; j < elevators.length; j++) { | |
if(elevators[j].loadFactor() == 0) { | |
elevators[j].goToFloor(i); | |
getMethod(j, "sort_destination_queue")(j); | |
return; | |
} | |
} | |
var num = Math.floor(Math.random() * elevators.length); | |
elevators[num].goToFloor(i); | |
getMethod(num, "sort_destination_queue")(num); | |
}, | |
"down_button_pressed": function() { | |
for(var j = 0; j < elevators.length; j++) { | |
if(elevators[j].loadFactor() == 0) { | |
elevators[j].goToFloor(i); | |
getMethod(j, "sort_destination_queue")(j); | |
return; | |
} | |
} | |
var num = Math.floor(Math.random() * elevators.length); | |
elevators[num].goToFloor(i); | |
getMethod(num, "sort_destination_queue")(num); | |
}, | |
"sort_destination_queue": function(i) { | |
elevator = elevators[i]; | |
var ary = elevator.destinationQueue; | |
var upAry = []; | |
var downAry = []; | |
var sameAry = []; | |
for(var i = 0; i < ary.length; i++) { | |
if(ary[i] < elevator.currentFloor()) | |
downAry[downAry.length] = ary[i]; | |
else if(ary[i] > elevator.currentFloor()) | |
upAry[upAry.length] = ary[i]; | |
else | |
sameAry[0] = ary[i]; | |
} | |
upAry.sort(); | |
downAry.sort(); | |
downAry.reverse(); | |
if(upAry.length > downAry.length) { | |
ary = sameAry.concat(upAry, downAry); | |
} | |
else { | |
ary = sameAry.concat(downAry, upAry); | |
} | |
elevator.destinationQueue = ary; | |
elevator.checkDestinationQueue(); | |
}, | |
}; | |
return methods[method]; | |
} | |
for(var i = 0; i < elevators.length; i++) { | |
elevators[i].on("idle", getMethod(i, "idle")); | |
elevators[i].on("floor_button_pressed ", getMethod(i, "floor_button_pressed")); | |
elevators[i].on("passing_floor ", getMethod(i, "passing_floor")); | |
} | |
for(var i = 0; i < floors.length; i++) { | |
floors[i].on("up_button_pressed", getMethod(i, "up_button_pressed")); | |
floors[i].on("down_button_pressed ", getMethod(i, "down_button_pressed")); | |
} | |
}, | |
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