@
kylebing 有意思,我让 gpt 帮我写了个。
{
init: function(elevators, floors) {
var elevator = elevators[0]; // Let's use the first elevator
// Whenever the elevator is idle (has no more queued destinations) ...
elevator.on("idle", function() {
// Check if there are any floors that have button pressed
for(var i=0; i < floors.length; i++) {
if(floors[i].buttonStates.up || floors[i].buttonStates.down) {
elevator.goToFloor(floors[i].floorNum());
break; // Exit loop once we find a floor with a button pressed
}
}
});
// Whenever a button is pressed in a floor
floors.forEach(function(floor) {
floor.on("up_button_pressed", function() {
// Maybe tell the elevator to go to this floor?
elevator.goToFloor(floor.floorNum());
});
floor.on("down_button_pressed", function() {
// Maybe tell the elevator to go to this floor?
elevator.goToFloor(floor.floorNum());
});
});
// Whenever a button is pressed inside the elevator
elevator.on("floor_button_pressed", function(floorNum) {
// Maybe tell the elevator to go to that floor?
elevator.goToFloor(floorNum);
});
},
update: function(dt, elevators, floors) {
// We normally don't need to do anything here
}
}