-
Notifications
You must be signed in to change notification settings - Fork 322
Developer testing solution
Magnus Wolffelt edited this page Jan 24, 2015
·
1 revision
This is the code I'm currently using for testing when developing elevatorsaga.
{
init: function(elevators, floors) {
var rotator = 0;
_.each(floors, function(floor) {
floor.on("up_button_pressed down_button_pressed", function() {
var elevator = elevators[(rotator++) % elevators.length];
elevator.goToFloor(floor.level);
});
});
_.each(elevators, function(elevator) {
elevator.on("floor_button_pressed", function(floorNum) {
elevator.goToFloor(floorNum);
});
elevator.on("idle", function() {
elevator.goToFloor(0);
});
});
},
update: function(dt, elevators, floors) {
}
}
I introduced a rotator variable to use all the elevators in a round-robin fashion, which sort of distributes the workload, although in a pretty inefficient manner. This rotator variable is only used when travellers press buttons on the floors, obviously.
The rest of the code is pretty obvious - go to the floor that each traveller wants to go to, and go to floor 0 when idle - floor 0 is where most travellers will appear.
Also, instead of for loops, I'm using Lo-Dash to iterate over all the elevators and floors. It has a more convenient syntax and results in more expressive code in my opinion.
Play it yourself at play.elevatorsaga.com