]> git.mar77i.info Git - elevator/commitdiff
get rid of pop_destination, bundle open_if_there
authormar77i <mar77i@protonmail.ch>
Sun, 25 Aug 2024 09:56:59 +0000 (11:56 +0200)
committermar77i <mar77i@protonmail.ch>
Sun, 25 Aug 2024 09:56:59 +0000 (11:56 +0200)
elevator.py

index 8c91f40cf442dcb69e83c499299ed20e6f0a22d3..a6c8f83207a0b1ff8e8575eb85b06a47dd075b29 100755 (executable)
@@ -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):
         """