From 50b17d85c8c25afdba3c5af8b4bf0b8a0c242534 Mon Sep 17 00:00:00 2001
From: fheinrich <fheinrich@techfak.uni-bielefeld.de>
Date: Thu, 7 Dec 2023 10:59:52 +0100
Subject: [PATCH] Cleaner layout file reading: character to counter map,
 Cleaning up the gui

---
 overcooked_simulator/main.py                  |  5 ---
 .../overcooked_environment.py                 | 41 ++++++++-----------
 overcooked_simulator/pygame_gui/pygame_gui.py | 14 +++----
 3 files changed, 24 insertions(+), 36 deletions(-)

diff --git a/overcooked_simulator/main.py b/overcooked_simulator/main.py
index 3bdc5528..3c8bd6b1 100644
--- a/overcooked_simulator/main.py
+++ b/overcooked_simulator/main.py
@@ -16,11 +16,6 @@ def main():
     simulator.register_player(Player(player_one_name, np.array([200, 200])))
     simulator.register_player(Player(player_two_name, np.array([100, 200])))
 
-    # simulator.env.counters[3].occupied_by = Tomato()
-    # simulator.env.counters[4].occupied_by = Tomato()
-    # simulator.env.counters[6].occupied_by = Plate()
-    # simulator.env.counters[7].occupied_by = Plate()
-
     # TODO maybe read the player names and keyboard keys from config file?
     keys1 = [
         pygame.K_LEFT,
diff --git a/overcooked_simulator/overcooked_environment.py b/overcooked_simulator/overcooked_environment.py
index 2d677389..8245fafa 100644
--- a/overcooked_simulator/overcooked_environment.py
+++ b/overcooked_simulator/overcooked_environment.py
@@ -16,6 +16,16 @@ from overcooked_simulator.counters import (
     PlateReturn,
 )
 
+SYMBOL_TO_CHARACTER_MAP = {
+    "C": Counter,
+    "B": CuttingBoard,
+    "X": Trash,
+    "W": ServingWindow,
+    "T": TomatoDispenser,
+    "P": PlateReturn,
+    "E": None,
+}
+
 
 class Action:
     """Action class, specifies player, action type and action itself."""
@@ -70,27 +80,10 @@ class Environment:
             for character in line:
                 character = character.capitalize()
                 pos = np.array([current_x, current_y])
-                match character:
-                    case "C":
-                        counter = Counter(pos)
-                        counters.append(counter)
-                    case "B":
-                        counter = CuttingBoard(pos)
-                        counters.append(counter)
-                    case "X":
-                        counter = Trash(pos)
-                        counters.append(counter)
-                    case "W":
-                        counter = ServingWindow(pos)
-                        counters.append(counter)
-                    case "T":
-                        counter = TomatoDispenser(pos)
-                        counters.append(counter)
-                    case "P":
-                        counter = PlateReturn(pos)
-                        counters.append(counter)
-                    case "E":
-                        pass
+                counter_class = SYMBOL_TO_CHARACTER_MAP[character]
+                if counter_class is not None:
+                    counter = counter_class(pos)
+                    counters.append(counter)
                 current_x += self.counter_side_length
             current_y += self.counter_side_length
         return counters
@@ -203,9 +196,9 @@ class Environment:
         Returns: True if the player is intersecting with any object in the environment.
         """
         return (
-            self.detect_player_collision(player)
-            or self.detect_collision_counters(player)
-            or self.detect_collision_world_bounds(player)
+                self.detect_player_collision(player)
+                or self.detect_collision_counters(player)
+                or self.detect_collision_world_bounds(player)
         )
 
     def detect_player_collision(self, player: Player):
diff --git a/overcooked_simulator/pygame_gui/pygame_gui.py b/overcooked_simulator/pygame_gui/pygame_gui.py
index 2149535a..bf696e45 100644
--- a/overcooked_simulator/pygame_gui/pygame_gui.py
+++ b/overcooked_simulator/pygame_gui/pygame_gui.py
@@ -25,6 +25,8 @@ YELLOW = (255, 255, 0)
 BACKGROUND_COLOR = GREY
 BACKGROUND_LINES_COLOR = (200, 200, 200)
 KNIFE_COLOR = (120, 120, 120)
+PLATE_RETURN_COLOR = (170, 170, 240)
+BOARD_COLOR = (239, 193, 151)
 
 
 class PlayerKeyset:
@@ -57,10 +59,10 @@ class PyGameGUI:
     """Visualisation of the overcooked environmnent and reading keyboard inputs using pygame."""
 
     def __init__(
-        self,
-        simulator: Simulator,
-        player_names: list[str],
-        player_keys: list[pygame.key],
+            self,
+            simulator: Simulator,
+            player_names: list[str],
+            player_keys: list[pygame.key],
     ):
         self.FPS = 60
         self.simulator = simulator
@@ -232,14 +234,12 @@ class PyGameGUI:
                 board_size,
                 board_size,
             )
-            BOARD_COLOR = (239, 193, 151)
             pygame.draw.rect(self.screen, BOARD_COLOR, board_rect)
 
             knife_rect = pygame.Rect(counter.pos[0] + 6, counter.pos[1] - 8, 5, 20)
             pygame.draw.rect(self.screen, KNIFE_COLOR, knife_rect)
 
         if isinstance(counter, PlateReturn):
-            RETURN = (170, 170, 240)
             size = 38
             inner = pygame.Rect(
                 counter.pos[0] - (size / 2),
@@ -247,7 +247,7 @@ class PyGameGUI:
                 size,
                 size,
             )
-            pygame.draw.rect(self.screen, RETURN, inner)
+            pygame.draw.rect(self.screen, PLATE_RETURN_COLOR, inner)
 
         if isinstance(counter, Trash):
             pygame.draw.circle(
-- 
GitLab