From 8b23120cfce1a0c96556790674deb2483acf0e23 Mon Sep 17 00:00:00 2001 From: mar77i Date: Sun, 25 Aug 2024 10:57:57 +0200 Subject: [PATCH] simplify call_queue and queued_dest and panel_prio into call_queue and panel.pressed --- elevator.py | 42 ++++++++++++++++-------------------------- 1 file changed, 16 insertions(+), 26 deletions(-) diff --git a/elevator.py b/elevator.py index 2ffc1de..8c91f40 100755 --- a/elevator.py +++ b/elevator.py @@ -155,7 +155,6 @@ class Door: elif self.state >= self.CLOSED: self.state = self.CLOSED self.direction = 0 - self.elevator.panel_prio = False return True def scale(self, topleft, size): @@ -230,15 +229,12 @@ class Elevator: def __init__(self, surf_size, num_levels): self.doors = [Door(self, surf_size, i, num_levels) for i in range(num_levels)] + self.panel = ElevatorPanel(self, surf_size, num_levels) self.current_level = 0 self.destination = None - self.queued_dest = None self.call_queue = [] - self.panel_prio = False def pop_destination(self, destination): - if destination == self.queued_dest: - self.queued_dest = None while destination in self.call_queue: self.call_queue.remove(destination) self.destination = destination @@ -246,17 +242,12 @@ class Elevator: def pick_destination(self): """ If not all doors are closed, bail. - If we have a queued_dest, go there If we have something on the call_queue, go there """ if not all(door.state == door.CLOSED for door in self.doors): return False - if self.queued_dest is not None: - self.pop_destination(self.queued_dest) - return True - elif not self.panel_prio and len(self.call_queue) > 0: + if not len(self.call_queue) > 0: self.pop_destination(self.call_queue.pop(0)) - self.panel_prio = True return True return False @@ -279,6 +270,7 @@ class Elevator: self.doors[self.destination].open() elif state == Door.OPEN: self.destination = None + self.panel.pressed = False return True def draw(self, surf): @@ -309,20 +301,14 @@ class Elevator: pygame.draw.line( surf, self.CABLE_COLOR, rect.midtop, (rect.centerx, cabin_rect.top) ) - - def get_current_dest(self): - if self.queued_dest is not None: - return self.queued_dest - return self.destination + self.panel.draw(surf) def goto(self, level): - """ - Set a queued_dest if no destination or queued_dest is set. - """ - if self.get_current_dest() is None and not ( + 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.queued_dest = level + self.call_queue.insert(0, level) + self.panel.pressed = True def get_whole_level(self): level, mod = divmod(self.current_level, self.LEVEL_STEPS) @@ -341,7 +327,6 @@ class Elevator: level_queue = { current, *self.call_queue, - self.queued_dest, self.destination, } if call_level not in level_queue: @@ -382,6 +367,7 @@ class ElevatorPanel: def __init__(self, elevator, surf_size, num_levels): self.elevator = elevator + self.elevator.panel = self self.outer_rect = Rect(*(v * surf_size for v in self.OUTER_RECT)) self.num_levels = num_levels inner_margin = ((1, 1) - self.INNER_SIZE) / 2 @@ -420,6 +406,7 @@ class ElevatorPanel: ), ) ) + self.pressed = False def draw(self, surf): pygame.draw.rect(surf, self.OUTER_COLOR, self.outer_rect) @@ -431,7 +418,12 @@ class ElevatorPanel: fs = self.font.render(str(i - 1), True, self.LABEL_COLOR) fs_size = Vec(fs.get_size()) surf.blit(fs, button.center - fs_size // 2) - if self.elevator.get_current_dest() == i - 1: + if self.elevator.destination == i - 1 or ( + self.elevator.destination is None + and self.pressed + and len(self.elevator.call_queue) > 0 + and self.elevator.call_queue[0] == i - 1 + ): pygame.draw.rect(surf, self.GLOW_COLOR, button, 2) for shape in BELL: @@ -460,7 +452,6 @@ class ElevatorApp: self.num_levels = 5 surf_size = surf.get_size() self.elevator = Elevator(surf_size, self.num_levels) - self.elevator_panel = ElevatorPanel(self.elevator, surf_size, self.num_levels) self.last_pos = None def left_click(self, pos): @@ -469,7 +460,7 @@ class ElevatorApp: self.elevator.call_from(i) self.dirty = True return - for i, button in enumerate(self.elevator_panel.buttons): + for i, button in enumerate(self.elevator.panel.buttons): if button.collidepoint(pos): if i == 0: beep() @@ -510,7 +501,6 @@ class ElevatorApp: for door in self.elevator.doors: door.draw(surf) self.elevator.draw(surf) - self.elevator_panel.draw(surf) pygame.display.update() def run(self): -- 2.47.0