From eab599a579095f1efcf66b376c104cb65704c17e Mon Sep 17 00:00:00 2001 From: mar77i Date: Sun, 25 Aug 2024 11:56:59 +0200 Subject: [PATCH] get rid of pop_destination, bundle open_if_there --- elevator.py | 63 ++++++++++++++++++++++++----------------------------- 1 file changed, 29 insertions(+), 34 deletions(-) diff --git a/elevator.py b/elevator.py index 8c91f40..a6c8f83 100755 --- a/elevator.py +++ b/elevator.py @@ -206,7 +206,7 @@ class Door: self.button_rect.center, self.button_rect.height // 2, ) - if self.level in {*self.elevator.call_queue, self.elevator.destination}: + if self.elevator.is_dest_or_queued(self.level): pygame.draw.circle( surf, self.BUTTON_GLOW_COLOR, @@ -234,11 +234,6 @@ class Elevator: self.destination = None self.call_queue = [] - def pop_destination(self, destination): - while destination in self.call_queue: - self.call_queue.remove(destination) - self.destination = destination - def pick_destination(self): """ If not all doors are closed, bail. @@ -246,10 +241,13 @@ class Elevator: """ if not all(door.state == door.CLOSED for door in self.doors): return False - if not len(self.call_queue) > 0: - self.pop_destination(self.call_queue.pop(0)) - return True - return False + if len(self.call_queue) == 0: + return False + destination = self.call_queue.pop(0) + while destination in self.call_queue: + self.call_queue.remove(destination) + self.destination = destination + return True def update(self): """ @@ -303,36 +301,33 @@ class Elevator: ) self.panel.draw(surf) - def goto(self, level): - if self.destination is None and not self.panel.pressed and not ( - self.get_whole_level() == level and self.doors[level].state == Door.OPEN - ): - self.call_queue.insert(0, level) - self.panel.pressed = True - def get_whole_level(self): level, mod = divmod(self.current_level, self.LEVEL_STEPS) if mod == 0 and self.destination in (None, level): return level return None - def call_from(self, call_level): - """ - If we are at the destination and the door is open, disregard - If level is not already a destination, add it to the call_queue - """ - current = self.get_whole_level() - if call_level == current and self.doors[call_level].state == Door.OPEN: - return - level_queue = { - current, - *self.call_queue, - self.destination, - } - if call_level not in level_queue: - self.call_queue.append(call_level) - elif call_level == current: - self.doors[call_level].open() + def open_if_there(self, level): + if self.get_whole_level() == level and self.doors[level].state != Door.OPEN: + self.doors[level].open() + return True + return False + + def goto(self, level): + if ( + not self.open_if_there(level) + and self.destination is None + and not self.panel.pressed + ): + self.call_queue.insert(0, level) + self.panel.pressed = True + + def is_dest_or_queued(self, level): + return level in {self.destination, *self.call_queue} + + def call_from(self, level): + if not self.open_if_there(level) and not self.is_dest_or_queued(level): + self.call_queue.append(level) def open(self): """ -- 2.47.0