Skip to content

Instantly share code, notes, and snippets.

@swizzlevixen
Last active September 15, 2018 01:18
Show Gist options
  • Save swizzlevixen/42fbef2822151157fda2ced96b3df23d to your computer and use it in GitHub Desktop.
Save swizzlevixen/42fbef2822151157fda2ced96b3df23d to your computer and use it in GitHub Desktop.
Elevator saga WIP http://play.elevatorsaga.com/
{
// init will be called when the challenge starts
// Normally you will put most of your code in the init function,
// to set up event listeners and logic.
// TODO: If we're just picking up, only stop if we're going in the right direction
// TODO: Deal with multiple elevators
// TODO: Don't stop if the current car is full, until we've let off some passengers
init: function(elevators, floors) {
elevators.forEach(function(elevator) {
elevator.on("floor_button_pressed", function(floorNum) {
// if we're not already going to this floor,
// add it to the destination queue
if (elevator.destinationQueue.indexOf(floorNum) == -1) {
elevator.goToFloor(floorNum);
}
});
elevator.on("passing_floor", function(floorNum, direction) {
// we're about to pass the floor. Should we stop here?
// if floor exists in queue:
if (elevator.destinationQueue.indexOf(floorNum) != -1) {
// Splice the current floor out of the destination array
// (so we don't go to it twice)
elevator.destinationQueue.splice(elevator.destinationQueue.indexOf(floorNum), 1);
elevator.checkDestinationQueue();
// Stop at this floor NOW (true) instead of later
elevator.goToFloor(floorNum, true);
}
});
// Whenever the elevator is idle (has no more queued destinations) ...
elevator.on("idle", function() {
// let's go to the bottom floor
// Since most people will come in there
elevator.goToFloor(0);
});
}); //end of elevators
floors.forEach(function(floor) {
// TODO: assign elevators round-robin to accept new floors?
floor.on("up_button_pressed", function() {
if (elevators[0].destinationQueue.indexOf(floor.floorNum()) != -1) {
; // already going there
} else {
elevators[0].goToFloor(floor.floorNum());
}
});
floor.on("down_button_pressed", function() {
if (elevators[0].destinationQueue.indexOf(floor.floorNum()) != -1) {
; // already going there
} else {
elevators[0].goToFloor(floor.floorNum());
}
});
}); // end of floors
}, // end of init
update: function(dt, elevators, floors) {
// update will be called repeatedly during the challenge.
// Do more stuff with the elevators and floors
// dt is the number of game seconds that passed since the last time update was called
// probably don't need to do anything here unless it's *~.FANCY.~*
// IDEAS:
// - set going up indicator
// - go up to first floor on up button pressed
// - check if I should continue going up, even if "down" is next in queue
} // end of update
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment