Skip to content

Instantly share code, notes, and snippets.

@rkJun
Created June 23, 2015 04:19
Show Gist options
  • Save rkJun/5cff35df80a6c3123261 to your computer and use it in GitHub Desktop.
Save rkJun/5cff35df80a6c3123261 to your computer and use it in GitHub Desktop.
{
init: function(elevators, floors) {
var elevator_zero = elevators[0];
var elevator_active = elevators[0];
var idle_elevators = [];
var idle_elevator = {};
var getIdleElevator = function(floorNum, direction) {
if (idle_elevators.length > 0) {
elevator_active = idle_elevators.pop().instance;
} else {
elevator_active = elevators[0];
}
return elevator_active;
};
_.each(elevators, function(elevator, index){
elevator.on("idle", function() {
idle_elevator = {
index: index,
instance: elevator,
currentFloor: elevator.currentFloor,
loadFactor: elevator.loadFactor,
maxPassengerCount: elevator.maxPassengerCount
};
idle_elevators.push(idle_elevator);
});
elevator.on("floor_button_pressed", function(floorNum) {
var pressed_floors = elevator.getPressedFloors();
// destinationQueue
elevator.goToFloor(floorNum);
});
});
_.each(floors, function(floor, index) {
var floorNum = floor.floorNum();
floor.on("up_button_pressed", function() {
getIdleElevator(floorNum, "up").goToFloor(floorNum);
});
floor.on("down_button_pressed", function() {
getIdleElevator(floorNum, "down").goToFloor(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