Skip to content

Instantly share code, notes, and snippets.

@mikefrey
Created January 30, 2015 21:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikefrey/58066d39337fc8a87bd6 to your computer and use it in GitHub Desktop.
Save mikefrey/58066d39337fc8a87bd6 to your computer and use it in GitHub Desktop.
Elevator Saga
{
init: function(elevators, floors) {
var pickup = {up:[], down:[]}
var topFloor = floors.length - 1
var bottomFloor = 0
console.log(floors[0])
// setup floors
floors.forEach(function(floor, i) {
pickup.up[i] = false
pickup.down[i] = false
floor.on('up_button_pressed', function() {
pickup.up[floor.floorNum()] = true
})
floor.on('down_button_pressed', function() {
pickup.down[floor.floorNum()] = true
})
})
elevators.forEach(function(elevator) {
var floorStatus = []
for (var i = 0; i < floors.length; i++) {
floorStatus[i] = false
}
var currentDir = 1
elevator.goingUpIndicator(true)
elevator.goingDownIndicator(false)
function setDir(dir) {
currentDir = dir
}
function setFloors(currentFloor) {
var hadFloor = false
for (var i = currentFloor; i >= 0 && i <= topFloor; i += currentDir) {
if (currentFloor == i) {
floorStatus[i] = false
continue
}
if (floorStatus[i]) {
hadFloor = true
floorStatus[i] = false
elevator.goToFloor(i)
}
else if (pickup.up[i]) {
hadFloor = true
pickup.up[i] = false
elevator.goToFloor(i)
}
else if (pickup.down[i]) {
hadFloor = true
pickup.down[i] = false
elevator.goToFloor(i)
}
}
return hadFloor
}
function idle() {
var currentFloor = elevator.currentFloor()
if (currentFloor == topFloor) setDir(-1)
if (currentFloor == bottomFloor) setDir(1)
if (!setFloors(currentFloor)) {
setDir(currentDir * -1)
if (!setFloors(currentFloor)) {
setTimeout(idle, 200)
}
}
}
elevator.on('stopped_at_floor', function(floorNum) {
if (currentDir == 1) pickup.up[floorNum] = false
if (currentDir == -1) pickup.down[floorNum] = false
var next = elevator.destinationQueue[0]
if (next > elevator.currentFloor() || elevator.currentFloor() == 0) {
elevator.goingUpIndicator(true)
elevator.goingDownIndicator(false)
} else if (next < elevator.currentFloor() || elevator.currentFloor() == floors.length -1) {
elevator.goingUpIndicator(false)
elevator.goingDownIndicator(true)
} else if (elevator.currentFloor() >= floors.length/2) {
elevator.goingUpIndicator(false)
elevator.goingDownIndicator(true)
} else {
elevator.goingUpIndicator(true)
elevator.goingDownIndicator(false)
}
})
elevator.on('passing_floor', function(floorNum) {
if (elevator.goingUpIndicator() && pickup.up[floorNum]) {
console.log('FORCE UP', floorNum)
elevator.goToFloor(floorNum, true)
} else if (elevator.goingDownIndicator() && pickup.down[floorNum]) {
console.log('FORCE DOWN', floorNum)
elevator.goToFloor(floorNum, true)
}
})
elevator.on('floor_button_pressed', function(floorNum) {
floorStatus[floorNum] = true
})
elevator.on('idle', idle)
})
},
update: function(dt, elevators, floors) {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment