Skip to content

Instantly share code, notes, and snippets.

@tdooner
Last active August 29, 2015 14:14
Show Gist options
  • Select an option

  • Save tdooner/2533cbba47acf2425d65 to your computer and use it in GitHub Desktop.

Select an option

Save tdooner/2533cbba47acf2425d65 to your computer and use it in GitHub Desktop.
elevator saga WIP
{
elevators: [],
floorsWaitingUp: [],
floorsWaitingDown: [],
registerElevator: function(elevator) {
elevator.i = this.elevators.length;
elevator.log = function(msg) {
if (this.i !== 0) { return; }
console.log("e#" + this.i + "(" + (this.goingUpIndicator() ? "up" : "") + "/" + (this.goingDownIndicator() ? "down" : "") + "/" + this.destinationQueue.join(" -> ") + ") " + msg);
};
elevator.beginTripUp = function(context) {
var firstFloor = context.floorsWaitingUp.indexOf(true);
this.goingUpIndicator(true);
this.goingDownIndicator(false);
this.goToFloor(firstFloor);
};
elevator.beginTripDown = function(context) {
var lastFloor = context.floorsWaitingDown.lastIndexOf(true);
context.floorsWaitingDown[lastFloor] = false;
this.goingUpIndicator(false);
this.goingDownIndicator(true);
this.goToFloor(lastFloor);
};
elevator.goingUpIndicator(false);
elevator.goingDownIndicator(false);
if (elevator.i % 2 === 1) {
elevator.goToFloor(this.topFloor);
elevator.goToFloor(0);
}
elevator.on("idle", (function(context) {
return function() {
elevator.log("idle");
if (this.goingUpIndicator() && this.currentFloor() !== context.topFloor) {
this.goToFloor(context.topFloor);
} else if (this.goingDownIndicator() && this.currentFloor() !== 0) {
this.goToFloor(0);
} else if (this.currentFloor() === 0) {
this.beginTripUp(context);
} else if (this.currentFloor() === context.topFloor) {
this.beginTripDown(context);
}
}
})(this));
elevator.on("floor_button_pressed", function(floorNum) {
this.destinationQueue.push(floorNum);
this.destinationQueue.sort();
this.destinationQueue = this.destinationQueue.filter(function(i, pos, self) { return self.indexOf(i) == pos });
if (this.goingDownIndicator()) {
this.destinationQueue.reverse();
}
this.log("updated queue");
this.checkDestinationQueue();
});
elevator.on("passing_floor", (function(context) {
return function(floorNum, direction) {
if (this.loadFactor() > 0.75) {
this.log("skipping floor due fullness");
return;
}
var isSomeoneThere = direction === "up" ? context.floorsWaitingUp[floorNum] : context.floorsWaitingDown[floorNum];
if (!isSomeoneThere) {
// this.log("skipping floor " + floorNum + " because nobody's there");
return;
}
if (Math.random() > 0.4) {
this.log("skipping floor randomly");
return;
}
this.log("stopping at floor " + floorNum);
this.goToFloor(floorNum, true);
};
})(this));
elevator.on("stopped_at_floor", (function(context) {
return function(floorNum) {
if (this.goingUpIndicator()) {
context.floorsWaitingUp[floorNum] = false;
} else if (this.goingDownIndicator()) {
context.floorsWaitingDown[floorNum] = false;
}
}
})(this));
this.elevators.push(elevator);
},
init: function(elevators, floors) {
this.totalFloors = floors.length;
this.topFloor = this.totalFloors - 1;
for (var i in elevators) {
this.registerElevator(elevators[i]);
}
for (var i in floors) {
floors[i].on("up_button_pressed", (function (context) {
var mainObject = context;
return function() {
var floorNum = this.floorNum();
mainObject.floorsWaitingUp[floorNum] = true;
}
})(this));
floors[i].on("down_button_pressed", (function (context) {
var mainObject = context;
return function() {
var floorNum = this.floorNum();
mainObject.floorsWaitingDown[floorNum] = true;
}
})(this));
}
},
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