From 0d0805bdac921f20317b8fa77b2469466b5a524e Mon Sep 17 00:00:00 2001
From: fheinrich <fheinrich@techfak.uni-bielefeld.de>
Date: Tue, 30 Jan 2024 13:28:39 +0100
Subject: [PATCH] Clean up positions in drawing

---
 overcooked_simulator/gui_2d_vis/drawing.py    | 38 ++++++++++---------
 .../gui_2d_vis/overcooked_gui.py              |  1 -
 .../gui_2d_vis/visualization.yaml             |  1 -
 3 files changed, 20 insertions(+), 20 deletions(-)

diff --git a/overcooked_simulator/gui_2d_vis/drawing.py b/overcooked_simulator/gui_2d_vis/drawing.py
index f074f654..d5caba6d 100644
--- a/overcooked_simulator/gui_2d_vis/drawing.py
+++ b/overcooked_simulator/gui_2d_vis/drawing.py
@@ -78,13 +78,13 @@ class Visualizer:
         )
         self.draw_counters(
             screen,
-            state,
+            state["counters"],
             grid_size,
         )
 
         self.draw_players(
             screen,
-            state,
+            state["players"],
             grid_size,
         )
 
@@ -130,14 +130,14 @@ class Visualizer:
     def draw_players(
         self,
         screen: pygame.Surface,
-        state_dict: dict,
+        players: dict,
         grid_size: float,
     ):
         """Visualizes the players as circles with a triangle for the facing direction.
         If the player holds something in their hands, it is displayed
         Args:            state: The game state returned by the environment.
         """
-        for p_idx, player_dict in enumerate(state_dict["players"]):
+        for p_idx, player_dict in enumerate(players):
             player_dict: PlayerState
             pos = np.array(player_dict["pos"]) * grid_size
             pos += grid_size / 2  # correct for grid offset
@@ -196,7 +196,7 @@ class Visualizer:
                 )
 
             if player_dict["holding"] is not None:
-                holding_item_pos = pos + (20 * facing) - (grid_size / 2)
+                holding_item_pos = pos + (20 * facing)
                 self.draw_item(
                     pos=holding_item_pos,
                     grid_size=grid_size,
@@ -244,7 +244,6 @@ class Visualizer:
             if "center_offset" in part:
                 draw_pos += np.array(part["center_offset"]) * grid_size
 
-            draw_pos += grid_size / 2  # Correction for grid offset
             match part_type:
                 case "image":
                     self.draw_image(
@@ -257,10 +256,9 @@ class Visualizer:
                     height = part["height"] * grid_size
                     width = part["width"] * grid_size
                     color = part["color"]
-                    draw_pos = pos.copy()
                     rect = pygame.Rect(
-                        draw_pos[0] - (height / 2) + (grid_size / 2),
-                        draw_pos[1] - (width / 2) + (grid_size / 2),
+                        draw_pos[0] - (height / 2),
+                        draw_pos[1] - (width / 2),
                         height,
                         width,
                     )
@@ -268,7 +266,6 @@ class Visualizer:
                 case "circle":
                     radius = part["radius"] * grid_size
                     color = colors[part["color"]]
-                    # draw_pos = pos.copy()
                     pygame.draw.circle(screen, color, draw_pos, radius)
 
     def draw_item(
@@ -343,6 +340,8 @@ class Visualizer:
         grid_size: float,
     ):
         """Visualize progress of progressing item as a green bar under the item."""
+        pos -= grid_size / 2
+
         bar_height = grid_size * 0.2
         progress_width = percent * grid_size
         progress_bar = pygame.Rect(
@@ -361,8 +360,11 @@ class Visualizer:
         different parts to be drawn.
         Args:            counter: The counter to visualize.
         """
-        pos = np.array(counter_dict["pos"]) * grid_size
+        pos = np.array(counter_dict["pos"], dtype=float) * grid_size
         counter_type = counter_dict["type"]
+
+        pos += grid_size // 2  # correct for grid offset
+
         self.draw_thing(screen, pos, grid_size, self.config["Counter"]["parts"])
         if counter_type in self.config:
             self.draw_thing(screen, pos, grid_size, self.config[counter_type]["parts"])
@@ -405,21 +407,21 @@ class Visualizer:
                 screen=screen,
             )
 
-    def draw_counters(self, screen: pygame, state, grid_size):
+    def draw_counters(self, screen: pygame, counters, grid_size):
         """Visualizes the counters in the environment.
 
         Args:            state: The game state returned by the environment.
         """
-        for counter in state["counters"]:
+        for counter in counters:
             self.draw_counter(screen, counter, grid_size)
 
-        for counter in state["counters"]:
+        for counter in counters:
             if counter["occupied_by"]:
                 self.draw_counter_occupier(
                     screen,
                     counter["occupied_by"],
                     grid_size,
-                    np.array(counter["pos"]) * grid_size,
+                    np.array(counter["pos"]) * grid_size + (grid_size / 2),
                 )
             if SHOW_COUNTER_CENTERS:
                 pygame.draw.circle(
@@ -460,13 +462,13 @@ class Visualizer:
             )
             center = np.array(order_upper_left)
             self.draw_thing(
-                pos=center,
+                pos=center + (grid_size / 2),
                 parts=config["Plate"]["parts"],
                 screen=order_screen,
                 grid_size=grid_size,
             )
             self.draw_item(
-                pos=center,
+                pos=center + (grid_size / 2),
                 item={"type": order["meal"]},
                 plate=True,
                 screen=order_screen,
@@ -482,7 +484,7 @@ class Visualizer:
 
             percentage = order_done_seconds / order["max_duration"]
             self.draw_progress_bar(
-                pos=center,
+                pos=center + (grid_size / 2),
                 percent=percentage,
                 screen=order_screen,
                 grid_size=grid_size,
diff --git a/overcooked_simulator/gui_2d_vis/overcooked_gui.py b/overcooked_simulator/gui_2d_vis/overcooked_gui.py
index c9ae367d..7922bc2f 100644
--- a/overcooked_simulator/gui_2d_vis/overcooked_gui.py
+++ b/overcooked_simulator/gui_2d_vis/overcooked_gui.py
@@ -95,7 +95,6 @@ class PyGameGUI:
         self.request_url = f"http://{url}:{port}"
         self.manager_id = random.choice(manager_ids)
 
-        # TODO cache loaded images?
         with open(ROOT_DIR / "gui_2d_vis" / "visualization.yaml", "r") as file:
             self.visualization_config = yaml.safe_load(file)
 
diff --git a/overcooked_simulator/gui_2d_vis/visualization.yaml b/overcooked_simulator/gui_2d_vis/visualization.yaml
index 21005007..87415a0e 100644
--- a/overcooked_simulator/gui_2d_vis/visualization.yaml
+++ b/overcooked_simulator/gui_2d_vis/visualization.yaml
@@ -275,7 +275,6 @@ Oven:
       color: black
       height: 0.8
       width: 0.3
-      center_offset: [ -0.4, -0.1 ]
 
 Basket:
   parts:
-- 
GitLab