Last active
December 10, 2021 21:08
-
-
Save codyfet/614a52b33ac26f4d7dee5d60379cc7f7 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) { | |
//var elevator1 = elevators[0]; // Let's use the first elevator | |
//var elevator2 = elevators[1]; // Let's use the first elevator | |
var pressedFloorNumbers = []; | |
for (var i = 0; i < floors.length; i++) { | |
// Накапливаем информацию об этажах, на которых нажаты кнопки вверх или вниз | |
floors[i].on("up_button_pressed", function(floor) { | |
if (pressedFloorNumbers.indexOf(floor.floorNum()) === -1) { | |
pressedFloorNumbers.push(floor.floorNum()); | |
console.log(pressedFloorNumbers); | |
} | |
}); | |
floors[i].on("down_button_pressed", function(floor) { | |
if (pressedFloorNumbers.indexOf(floor.floorNum()) === -1) { | |
pressedFloorNumbers.push(floor.floorNum()); | |
console.log(pressedFloorNumbers); | |
} | |
}); | |
} | |
for (var k = 0; k < elevators.length; k++) { | |
elevators[k].on("floor_button_pressed", function(floorNum){ | |
var currentFloor = this.currentFloor(); | |
var floorsCount = floors.length; | |
this.goToFloor(floorNum); | |
console.log("elevators[k].destinationQueue before sort") | |
console.log(this.destinationQueue) | |
this.destinationQueue.sort(function(a, b){ | |
if (currentFloor + 1 > (floorsCount / 2)){ | |
return b-a; | |
} else { | |
return a-b; | |
} | |
}); | |
this.checkDestinationQueue(); | |
console.log("elevators[k].destinationQueue after sort") | |
console.log(this.destinationQueue) | |
}); | |
// Когда лифт останавливается на этаже удаляем номер этого этажа из массива ожидающих | |
elevators[k].on("stopped_at_floor", function (floorNum) { | |
var index = pressedFloorNumbers.indexOf(floorNum); | |
if (index !== -1) { | |
pressedFloorNumbers.splice(index, 1); | |
} | |
}); | |
// Когда нет задач лифт идет на первый из очереди нажатых | |
elevators[k].on("idle", function() { | |
if (pressedFloorNumbers.length > 0) { | |
this.goToFloor(pressedFloorNumbers[0]); | |
} | |
}); | |
} | |
}, | |
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