Skip to content

Instantly share code, notes, and snippets.

@johnywith1n
Created January 22, 2015 23:47
Show Gist options
  • Save johnywith1n/44c628d7914f2afc52dd to your computer and use it in GitHub Desktop.
Save johnywith1n/44c628d7914f2afc52dd to your computer and use it in GitHub Desktop.
elevatorsaga works until level 6
{
go: function (elevator) {
var self = this;
elevator.on("idle", function() {
self.queue.push(elevator);
});
elevator.on('floor_button_pressed', function (floor) {
elevator.goToFloor(floor);
});
},
callElevator: function (floorNum) {
var self = this;
var elevator = self.queue.shift();
if (elevator) {
elevator.goToFloor(floorNum);
} else {
setTimeout(function () {
self.callElevator(floorNum);
}, 0);
}
},
up: function (floor) {
var self = this;
floor.on("up_button_pressed", function(event) {
self.callElevator(floor.floorNum());
});
floor.on("down_button_pressed", function(event) {
self.callElevator(floor.floorNum());
});
},
queue: [],
init: function(elevators, floors) {
for(var i = 0; i < floors.length; i++) {
this.up(floors[i])
}
for (var i = 0; i < elevators.length; i++) {
this.go(elevators[i]);
}
},
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