Last active
May 15, 2019 15:05
-
-
Save codyfet/379ae651498f23682bb6cf820694996f 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 elevator = elevators[0]; // 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); | |
} | |
}); | |
} | |
// Когда в лифте нажали на цифру - едем туда | |
elevator.on("floor_button_pressed", function(floorNum){ | |
elevator.goToFloor(floorNum); | |
}); | |
// Когда лифт останавливается на этаже удаляем номер этого этажа из массива ожидающих | |
elevator.on("stopped_at_floor", function (floorNum) { | |
var index = pressedFloorNumbers.indexOf(floorNum); | |
if (index !== -1) { | |
pressedFloorNumbers.splice(index, 1); | |
} | |
}); | |
// Когда нет задач лифт идет на первый из очереди нажатых | |
elevator.on("idle", function() { | |
if (pressedFloorNumbers.length > 0) { | |
elevator.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