Created
February 1, 2015 12:55
-
-
Save garyjoy/94f4467b34268b9fe15a to your computer and use it in GitHub Desktop.
Elevator Game (01/01/2015)
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) { | |
// Extend the Elevators with Names and Ranges... | |
for (var i = 0; i < elevators.length; ++i) { | |
elevators[i].name = i; | |
elevators[i].bottomFloor = 0; | |
elevators[i].topFloor = 8; | |
} | |
// Override Elevator Ranges... | |
elevators[0].bottomFloor = 0; | |
elevators[0].topFloor = 3; | |
elevators[1].bottomFloor = 0; | |
elevators[1].topFloor = 3; | |
// Extend the Floors with Waiting Flags | |
floors.forEach(function(floor) { | |
floor.passengerWaitingToGoUp = false; | |
floor.passengerWaitingToGoDown = false; | |
}); | |
// Create Floor Events to update the Floor Waiting Flags | |
floors.forEach(function(floor) { | |
floor.on("up_button_pressed", function(event) { | |
floor.passengerWaitingToGoUp = true; | |
console.log("Floor #" + floor.floorNum() + ": Waiting for UP"); | |
}); | |
floor.on("down_button_pressed", function(event) { | |
floor.passengerWaitingToGoDown = true; | |
console.log("Floor #" + floor.floorNum() + ": Waiting for DOWN"); | |
}); | |
}); | |
// Create Elevator Events... | |
elevators.forEach(function(elevator) { | |
elevator.on("stopped_at_floor", function(floorNum) { | |
console.log("Elevator #" + this.name + ": Stopped at Floor " + floorNum); | |
// Update the Floor Waiting Flags | |
for (var i = 0; i < floors.length; ++i) { | |
var floor = floors[i]; | |
if (floor.floorNum() === floorNum) { | |
if (this.direction === "up") { | |
floor.passengerWaitingToGoUp = false; | |
console.log("Floor #" + floor.floorNum() + ": Collected UP"); | |
} | |
if (this.direction === "down") { | |
floor.passengerWaitingToGoDown = false; | |
console.log("Floor #" + floor.floorNum() + ": Collected DOWN"); | |
} | |
} | |
} | |
}); | |
elevator.on("idle", function() { | |
console.log("Elevator #" + this.name + ": Is IDLE"); | |
}); | |
elevator.on("passing_floor", function(floorNum, direction) { | |
for (var i = 0; i < floors.length; ++i) { | |
var floor = floors[i]; | |
if (floor.floorNum() === floorNum) { | |
if (direction === "up" && this.direction === "up" && floor.passengerWaitingToGoUp === true && this.loadFactor() < 0.7) { | |
this.goToFloor(floorNum, true); | |
floor.passengerWaitingToGoUp = false; | |
} | |
if (direction === "down" && this.direction === "down" && floor.passengerWaitingToGoDown === true && this.loadFactor() < 0.7) { | |
this.goToFloor(floorNum, true); | |
floor.passengerWaitingToGoDown = false; | |
} | |
} | |
} | |
}); | |
elevator.on("floor_button_pressed", function(floorNum) { | |
this.goToFloor(floorNum); | |
if (this.direction === "up") { | |
this.destinationQueue.sort(function(a, b) { | |
return a - b | |
}); | |
this.checkDestinationQueue(); | |
} | |
if (this.direction === "down") { | |
this.destinationQueue.sort(function(a, b) { | |
return b - a | |
}); | |
this.checkDestinationQueue(); | |
} | |
console.log("Elevator #" + this.name + ": Floor " + floorNum + " Added. Destination Queue: " + this.destinationQueue.toString()); | |
}); | |
}); | |
}, | |
update: function(dt, elevators, floors) { | |
elevators.forEach(function(elevator) { | |
var currentFloor = elevator.currentFloor(); | |
if (elevator.destinationQueue.length === 0) { | |
elevator.direction = ""; | |
console.log("Elevator #" + elevator.name + ": Empty Destination Queue"); | |
var currentFloor = elevator.currentFloor(); | |
if (currentFloor === elevator.bottomFloor) { | |
elevator.goToFloor(elevator.topFloor); | |
console.log("Elevator #" + elevator.name + ": Send to TOP"); | |
} else if (currentFloor === elevator.topFloor) { | |
elevator.goToFloor(elevator.bottomFloor); | |
console.log("Elevator #" + elevator.name + ": Send to BOTTOM"); | |
} else { | |
console.log("Elevator #" + elevator.name + ": Send to BOTTOM"); | |
elevator.goToFloor(elevator.bottomFloor); | |
} | |
} else { | |
var nextDestination = elevator.destinationQueue[0]; | |
if (nextDestination > currentFloor) { | |
elevator.direction = "up"; | |
elevator.goingUpIndicator(true); | |
elevator.goingDownIndicator(false); | |
} else if (nextDestination < currentFloor) { | |
elevator.direction = "down"; | |
elevator.goingUpIndicator(false); | |
elevator.goingDownIndicator(true); | |
} else { | |
elevator.direction = ""; | |
} | |
} | |
if (currentFloor === elevator.bottomFloor) { | |
elevator.direction = "up"; | |
elevator.goingUpIndicator(true); | |
elevator.goingDownIndicator(false); | |
} | |
if (currentFloor === elevator.topFloor) { | |
elevator.direction = "down"; | |
elevator.goingUpIndicator(false); | |
elevator.goingDownIndicator(true); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment