Created
January 13, 2016 08:06
-
-
Save fosterbrereton/6634745beeb421d49532 to your computer and use it in GitHub Desktop.
Elevator Saga (play.elevatorsaga.com)
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
{ | |
init: function(elevators, floors) { | |
var closestToFloor = function (floor) { | |
var elevator = null; | |
var distance = 1000; | |
console.log("Call to floor " + floor + "..."); | |
elevators.forEach(function (cur_elevator) { | |
var cur_distance = Math.abs(cur_elevator.currentFloor() - floor); | |
if (cur_distance >= distance) { | |
return; | |
} | |
distance = cur_distance; | |
elevator = cur_elevator; | |
}); | |
return elevator; | |
}; | |
var travel = function(elevator, floor) { | |
elevator.goToFloor(floor); | |
var direction = elevator.destinationDirection(); | |
elevator.goingUpIndicator(direction != "down"); | |
elevator.goingDownIndicator(direction != "up"); | |
} | |
elevators.forEach(function (elevator) { | |
elevator.on("idle", function () { | |
travel(elevator, 0); | |
}); | |
elevator.on("floor_button_pressed", function (floor) { | |
travel(elevator, floor); | |
}); | |
}); | |
floors.forEach(function(floor) { | |
floor.on("up_button_pressed", function () { | |
var curFloor = floor.floorNum(); | |
var elevator = closestToFloor(curFloor); | |
if (elevator !== null) { | |
travel(elevator, curFloor); | |
} | |
}); | |
floor.on("down_button_pressed", function () { | |
var curFloor = floor.floorNum(); | |
var elevator = closestToFloor(curFloor); | |
if (elevator !== null) { | |
travel(elevator, curFloor); | |
} | |
}); | |
}); | |
}, | |
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