Created
January 22, 2015 23:47
-
-
Save johnywith1n/44c628d7914f2afc52dd to your computer and use it in GitHub Desktop.
elevatorsaga works until level 6
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
{ | |
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