Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- init: function(elevators, floors) {
- var elevator = elevators[0]; // Let's use the first elevator
- var up = [];
- var down = [];
- floors.forEach(function(floor) {
- floor.on("up_button_pressed", function() {
- up.push(floor.floorNum());
- });
- floor.on("down_button_pressed", function() {
- down.push(floor.floorNum());
- });
- });
- elevator.on("idle stopped_at_floor", function() {
- var floorNum = 0;
- if (elevator.getPressedFloors().length > 0) {
- if (elevator.goingUpIndicator()) {
- floorNum = Math.max.apply(Math, elevator.getPressedFloors());
- }
- else {
- floorNum = Math.min.apply(Math, elevator.getPressedFloors());
- }
- }
- else if (up.length > 0)
- {
- if (down.length > 0)
- {
- if (elevator.currentFloor() >= floors.length / 2)
- {
- floorNum = Math.max.apply(Math, down);
- }
- else
- {
- floorNum = Math.min.apply(Math, up);
- }
- }
- else
- {
- floorNum = Math.min.apply(Math, up);
- }
- }
- else if (down.length > 0)
- {
- floorNum = Math.max.apply(Math, down);
- }
- elevator.goToFloor(floorNum);
- if (floorNum < elevator.currentFloor()) {
- elevator.goingUpIndicator(false);
- elevator.goingDownIndicator(true);
- if (down.indexOf(elevator.currentFloor()) != -1) {
- down.splice(down.indexOf(elevator.currentFloor()), 1);
- }
- }
- else if (floorNum > elevator.currentFloor()) {
- elevator.goingUpIndicator(true);
- elevator.goingDownIndicator(false);
- if (up.indexOf(elevator.currentFloor()) != -1) {
- up.splice(up.indexOf(elevator.currentFloor()), 1);
- }
- }
- else {
- elevator.goingUpIndicator(true);
- elevator.goingDownIndicator(true);
- }
- });
- elevator.on("floor_button_pressed", function(floorNum) {
- if (elevator.destinationQueue.length == 0) {
- elevator.goToFloor(floorNum);
- if (floorNum < elevator.currentFloor()) {
- elevator.goingUpIndicator(false);
- elevator.goingDownIndicator(true);
- if (down.indexOf(elevator.currentFloor()) != -1) {
- down.splice(down.indexOf(elevator.currentFloor()), 1);
- }
- }
- else if (floorNum > elevator.currentFloor()) {
- elevator.goingUpIndicator(true);
- elevator.goingDownIndicator(false);
- if (up.indexOf(elevator.currentFloor()) != -1) {
- up.splice(up.indexOf(elevator.currentFloor()), 1);
- }
- }
- else {
- elevator.goingUpIndicator(true);
- elevator.goingDownIndicator(true);
- }
- }
- });
- elevator.on("passing_floor", function(floorNum, direction) {
- var bShouldStop = false;
- if (elevator.getPressedFloors().indexOf(floorNum) != -1) {
- bShouldStop = true;
- } else if (elevator.loadFactor() < 0.6) {
- if (direction == "up" && up.indexOf(floorNum) != -1) {
- bShouldStop = true;
- } else if (direction == "down" && down.indexOf(floorNum) != -1) {
- bShouldStop = true;
- }
- }
- if (bShouldStop) {
- elevator.stop();
- elevator.goToFloor(floorNum);
- }
- });
- },
- update: function(dt, elevators, floors) {
- // We normally don't need to do anything here
- }
- }
Add Comment
Please, Sign In to add comment