-
Notifications
You must be signed in to change notification settings - Fork 1
/
elevator.py
100 lines (81 loc) · 3.09 KB
/
elevator.py
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
from random import randint
class Elevator:
def __init__(self, elevatorId, gameState):
self.gameState = gameState
self.id = elevatorId
self.currentFloor = 0.0
self.maxSpeed = 0.13
self.speed = 0.0
self.destinationFloor = 0
self.state = "loading" # up down unloading loading waiting
self.delay = 10
self.maxPassengers = 6
self.passengers = []
self.transportedPersons = 0
def getSpeed(self):
return self.speed
def getCurrentFloor(self):
return self.currentFloor
def getElevatorId(self):
return self.id
def getMaxPassengers(self):
return self.maxPassengers
def getDistanceToDestinationFloor(self):
return abs(self.currentFloor - self.destinationFloor)
def setDestinationFloor(self, floor):
self.destinationFloor = floor
self.state = "moving"
def checkArrival(self):
if self.getDistanceToDestinationFloor() < 0.03:
self.speed = 0
self.currentFloor = int(self.destinationFloor)
self.state = "unloading"
self.delay = 20
def getPassengers(self):
return self.passengers
def run(self):
if self.delay > 0:
self.delay -= 1
return
if self.state == "moving":
if self.getDistanceToDestinationFloor() > 0.7:
self.speed = min(self.speed + 0.005, self.maxSpeed)
elif self.getDistanceToDestinationFloor() < 0.6:
self.speed = max(self.speed - 0.01, 0.03)
if self.currentFloor < self.destinationFloor:
self.currentFloor += self.speed
elif self.currentFloor > self.destinationFloor:
self.currentFloor -= self.speed
self.checkArrival()
elif self.state == "unloading":
unloading = False
for person in self.passengers:
if not unloading and person.getEndFloor() == self.currentFloor:
# unload that person
self.passengers.remove(person)
person.unload()
self.transportedPersons += 1
self.delay = 15
unloading = True
if not unloading:
self.delay = 5
self.state = "loading"
elif self.state == "loading":
if len(self.passengers) < self.maxPassengers:
# load another person (if persons available)
person = self.gameState.getNextWaitingPerson(
int(self.currentFloor))
if person != None:
self.passengers.append(person)
person.setLocationElevator(self)
else:
self.state = "waitingForCommand"
else:
self.state = "waitingForCommand"
self.delay = 10
def getCurrentFloor(self):
return self.currentFloor
def getTransportedPersons(self):
return self.transportedPersons
def getPassengers(self):
return self.passengers