Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
IWP Elevator Saga Mob Programming
{
init: (elevators, floors) => {
const up_queue = [];
const down_queue = [];
const getIdleElevator = () => {
elevators.forEach((elevator) => {
if(elevator.destinationQueue.length == 0)
return elevator;
});
return null;
}
const sendIdleElevator = (floor_num) => {
const elevator = getIdleElevator();
if(elevator != null)
elevator.goToFloor(floor_num);
};
floors.forEach((floor) => {
floor.on("up_button_pressed", () => {
up_queue.push(floor.floorNum());
sendIdleElevator(floor.floorNum());
});
floor.on("down_button_pressed", () => {
down_queue.push(floor.floorNum());
sendIdleElevator(floor.floorNum());
});
});
elevators.forEach((elevator) => {
elevator.on("floor_button_pressed", (floor_num) => {
elevator.goToFloor(floor_num);
});
elevator.on("passing_floor", (floor_num, direction) => {
const destinationIndex = elevator.destinationQueue.findIndex((f) => f == floor_num);
if(destinationIndex > -1) {
elevator.destinationQueue.splice(destinationIndex, 1);
elevator.checkDestinationQueue();
elevator.goToFloor(floor_num, true);
}
if(direction == "up") {
const up_queue_index = up_queue.findIndex((f) => f == floor_num);
if(up_queue_index > -1) {
up_queue.splice(up_queue_index, 1);
elevator.goToFloor(floor_num, true);
}
} else {
const down_queue_index = down_queue.findIndex((f) => f == floor_num);
if(down_queue_index > -1) {
down_queue.splice(down_queue_index, 1);
elevator.goToFloor(floor_num, true);
}
}
if(floor_num == 0 || elevator.destinationQueue[1] > floor_num) {
elevator.goingUpIndicator(true);
elevator.goingDownIndicator(false);
} else {
elevator.goingUpIndicator(false);
elevator.goingDownIndicator(true);
}
});
elevator.on("idle", () => {
elevator.goToFloor(0);
if(elevator.currentFloor() > 0) {
elevator.goingUpIndicator(false);
elevator.goingDownIndicator(true);
} else {
elevator.goingUpIndicator(true);
elevator.goingDownIndicator(false);
}
})
});
},
update: (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
You can’t perform that action at this time.