-
Notifications
You must be signed in to change notification settings - Fork 0
/
elevator.js
54 lines (52 loc) · 2.13 KB
/
elevator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
{
init: function(elevators, floors) {
// define recentlyPressed
var recentlyPressed = [];
// recently pressed
for (var j=0; j<floors.length; j++) {
floors[j].on("up_button_pressed", function() {
if (this.floorNum() != recentlyPressed[recentlyPressed.length - 1]) {
var closest = 0;
for (var k=1; k<elevators.length; k++) {
if (elevators[k].currentFloor() - this.floorNum() <
elevators[k-1].currentFloor() - this.floorNum()) {
closest = k;
}
}
console.log(closest);
elevators[closest].destinationQueue.unshift(this.floorNum());
console.log(elevators[closest].destinationQueue);
}
});
floors[j].on("down_button_pressed", function() {
if (this.floorNum() != recentlyPressed[recentlyPressed.length - 1]) {
recentlyPressed.push(this.floorNum());
}
});
}
// elevator's default autonomous behavior
for (var i=0; i<elevators.length; i++) {
// when idle
elevators[i].on("idle", function() {
if (recentlyPressed.length > 0) {
this.goToFloor(recentlyPressed.pop());
} else {
this.goToFloor(0);
}
});
// when somebody presses a button in the elevator
elevators[i].on("floor_button_pressed", function(floorNum) {
console.log("dQ: "+this.destinationQueue+" rP: "+recentlyPressed);
if (Math.abs(this.currentFloor() - this.destinationQueue[0]) <
Math.abs(this.currentFloor() - floorNum)) {
this.goToFloor(this.destinationQueue[0]);
this.goToFloor(floorNum);
} else {
this.goToFloor(floorNum);
}
});
}
},
update: function(dt, elevators, floors) {
}
}