forked from magwo/elevatorsaga
-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.js
72 lines (60 loc) · 2.39 KB
/
user.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
var asUser = function(user, weight, floorCount, floorHeight) {
user.weight = weight;
user.currentFloor = 0;
user.destinationFloor = 0;
user.done = false;
user.removeMe = false;
user.appearOnFloor = function(floor, destinationFloorNum) {
var floorPosY = (floorCount - 1) * floorHeight - floor.level * floorHeight + 30;
user.currentFloor = floor.level;
user.destinationFloor = destinationFloorNum;
user.moveTo(null, floorPosY);
user.pressFloorButton(floor);
};
user.pressFloorButton = function(floor) {
if(user.destinationFloor < user.currentFloor) {
floor.pressDownButton();
} else {
floor.pressUpButton();
}
};
user.elevatorAvailable = function(elevator, floor) {
if(user.done || user.parent !== null || user.isBusy()) {
return;
}
if(!elevator.isSuitableForTravelBetween(user.currentFloor, user.destinationFloor)) {
// Not suitable for travel - don't use this elevator
return;
}
var pos = elevator.userEntering(user);
if(pos) {
// Success
user.setParent(elevator);
user.trigger("entered_elevator", elevator);
user.moveToOverTime(pos[0], pos[1], 1, undefined, function() {
elevator.pressFloorButton(user.destinationFloor);
});
var exitAvailableHandler = function(floorNum) {
if(elevator.currentFloor === user.destinationFloor) {
elevator.userExiting(user);
user.currentFloor = elevator.currentFloor;
user.setParent(null);
var destination = user.x + 100;
user.done = true;
user.trigger("exited_elevator", elevator);
user.trigger("new_state");
user.moveToOverTime(destination, null, 1 + Math.random()*0.5, linearInterpolate, function() {
user.removeMe = true;
user.trigger("removed");
user.off("*");
});
elevator.off("exit_available", exitAvailableHandler);
}
};
elevator.on("exit_available", exitAvailableHandler);
} else {
user.pressFloorButton(floor);
}
};
return user;
};