Skip to content
Snippets Groups Projects
Commit 2d262255 authored by Fabian Heinrich's avatar Fabian Heinrich
Browse files

Counter centers are now at even grid points. Drawing adjusted to that.

parent 000c29b0
No related branches found
No related tags found
1 merge request!38Resolve "Finalize coordinate system"
Pipeline #44971 passed
...@@ -17,8 +17,8 @@ from overcooked_simulator.state_representation import ( ...@@ -17,8 +17,8 @@ from overcooked_simulator.state_representation import (
) )
USE_PLAYER_COOK_SPRITES = True USE_PLAYER_COOK_SPRITES = True
SHOW_INTERACTION_RANGE = False SHOW_INTERACTION_RANGE = True
SHOW_COUNTER_CENTERS = False SHOW_COUNTER_CENTERS = True
def create_polygon(n, length): def create_polygon(n, length):
...@@ -66,10 +66,10 @@ class Visualizer: ...@@ -66,10 +66,10 @@ class Visualizer:
self, self,
screen, screen,
state, state,
width,
height,
grid_size, grid_size,
): ):
width = int(np.ceil(state["kitchen"]["width"] * grid_size))
height = int(np.ceil(state["kitchen"]["height"] * grid_size))
self.draw_background( self.draw_background(
surface=screen, surface=screen,
width=width, width=width,
...@@ -140,6 +140,7 @@ class Visualizer: ...@@ -140,6 +140,7 @@ class Visualizer:
for p_idx, player_dict in enumerate(state_dict["players"]): for p_idx, player_dict in enumerate(state_dict["players"]):
player_dict: PlayerState player_dict: PlayerState
pos = np.array(player_dict["pos"]) * grid_size pos = np.array(player_dict["pos"]) * grid_size
pos += grid_size / 2 # correct for grid offset
facing = np.array(player_dict["facing_direction"]) facing = np.array(player_dict["facing_direction"])
...@@ -195,7 +196,7 @@ class Visualizer: ...@@ -195,7 +196,7 @@ class Visualizer:
) )
if player_dict["holding"] is not None: if player_dict["holding"] is not None:
holding_item_pos = pos + (20 * facing) holding_item_pos = pos + (20 * facing) - (grid_size / 2)
self.draw_item( self.draw_item(
pos=holding_item_pos, pos=holding_item_pos,
grid_size=grid_size, grid_size=grid_size,
...@@ -204,13 +205,15 @@ class Visualizer: ...@@ -204,13 +205,15 @@ class Visualizer:
) )
if player_dict["current_nearest_counter_pos"]: if player_dict["current_nearest_counter_pos"]:
pos = player_dict["current_nearest_counter_pos"] nearest_pos = (
np.array(player_dict["current_nearest_counter_pos"]) * grid_size
)
pygame.draw.rect( pygame.draw.rect(
screen, screen,
colors[self.player_colors[p_idx]], colors[self.player_colors[p_idx]],
rect=pygame.Rect( rect=pygame.Rect(
pos[0] * grid_size - (grid_size // 2), *nearest_pos,
pos[1] * grid_size - (grid_size // 2),
grid_size, grid_size,
grid_size, grid_size,
), ),
...@@ -236,48 +239,37 @@ class Visualizer: ...@@ -236,48 +239,37 @@ class Visualizer:
""" """
for part in parts: for part in parts:
part_type = part["type"] part_type = part["type"]
draw_pos = pos.copy()
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: match part_type:
case "image": case "image":
if "center_offset" in part:
d = np.array(part["center_offset"]) * grid_size
pos = np.array(pos)
pos += d
self.draw_image( self.draw_image(
screen, screen,
part["path"], part["path"],
part["size"] * scale * grid_size, part["size"] * scale * grid_size,
pos, draw_pos,
) )
case "rect": case "rect":
height = part["height"] * grid_size height = part["height"] * grid_size
width = part["width"] * grid_size width = part["width"] * grid_size
color = part["color"] color = part["color"]
if "center_offset" in part: draw_pos = pos.copy()
dx, dy = np.array(part["center_offset"]) * grid_size rect = pygame.Rect(
rect = pygame.Rect(pos[0] + dx, pos[1] + dy, height, width) draw_pos[0] - (height / 2) + (grid_size / 2),
pygame.draw.rect(screen, color, rect) draw_pos[1] - (width / 2) + (grid_size / 2),
else: height,
rect = pygame.Rect( width,
pos[0] - (height / 2), )
pos[1] - (width / 2),
height,
width,
)
pygame.draw.rect(screen, color, rect) pygame.draw.rect(screen, color, rect)
case "circle": case "circle":
radius = part["radius"] * grid_size radius = part["radius"] * grid_size
color = colors[part["color"]] color = colors[part["color"]]
if "center_offset" in part: # draw_pos = pos.copy()
pygame.draw.circle( pygame.draw.circle(screen, color, draw_pos, radius)
screen,
color,
np.array(pos)
+ (np.array(part["center_offset"]) * grid_size),
radius,
)
else:
pygame.draw.circle(screen, color, pos, radius)
def draw_item( def draw_item(
self, self,
...@@ -354,8 +346,8 @@ class Visualizer: ...@@ -354,8 +346,8 @@ class Visualizer:
bar_height = grid_size * 0.2 bar_height = grid_size * 0.2
progress_width = percent * grid_size progress_width = percent * grid_size
progress_bar = pygame.Rect( progress_bar = pygame.Rect(
pos[0] - (grid_size / 2), pos[0],
pos[1] - (grid_size / 2) + grid_size - bar_height, pos[1] + grid_size - bar_height,
progress_width, progress_width,
bar_height, bar_height,
) )
...@@ -431,7 +423,10 @@ class Visualizer: ...@@ -431,7 +423,10 @@ class Visualizer:
) )
if SHOW_COUNTER_CENTERS: if SHOW_COUNTER_CENTERS:
pygame.draw.circle( pygame.draw.circle(
screen, colors["green1"], np.array(counter["pos"]) * grid_size, 3 screen,
colors["green1"],
np.array(counter["pos"]) * grid_size + (grid_size / 2),
3,
) )
def draw_orders( def draw_orders(
...@@ -463,9 +458,7 @@ class Visualizer: ...@@ -463,9 +458,7 @@ class Visualizer:
), ),
width=2, width=2,
) )
center = np.array(order_upper_left) + np.array( center = np.array(order_upper_left)
[grid_size / 2, grid_size / 2]
)
self.draw_thing( self.draw_thing(
pos=center, pos=center,
parts=config["Plate"]["parts"], parts=config["Plate"]["parts"],
......
...@@ -128,9 +128,8 @@ class PyGameGUI: ...@@ -128,9 +128,8 @@ class PyGameGUI:
self.vis.create_player_colors(len(self.player_names)) self.vis.create_player_colors(len(self.player_names))
def get_window_sizes(self, state: dict): def get_window_sizes(self, state: dict):
counter_positions = np.array([c["pos"] for c in state["counters"]]) kitchen_width = state["kitchen"]["width"]
kitchen_width = counter_positions[:, 0].max() + 0.5 kitchen_height = state["kitchen"]["height"]
kitchen_height = counter_positions[:, 1].max() + 0.5
if self.visualization_config["GameWindow"]["WhatIsFixed"] == "window_width": if self.visualization_config["GameWindow"]["WhatIsFixed"] == "window_width":
game_width = self.visualization_config["GameWindow"]["size"] game_width = self.visualization_config["GameWindow"]["size"]
kitchen_aspect_ratio = kitchen_height / kitchen_width kitchen_aspect_ratio = kitchen_height / kitchen_width
...@@ -349,8 +348,6 @@ class PyGameGUI: ...@@ -349,8 +348,6 @@ class PyGameGUI:
self.vis.draw_gamescreen( self.vis.draw_gamescreen(
self.game_screen, self.game_screen,
state, state,
self.game_width,
self.game_height,
self.grid_size, self.grid_size,
) )
......
...@@ -281,23 +281,22 @@ class Environment: ...@@ -281,23 +281,22 @@ class Environment:
[counter_size/2, counter_size/2], counters are directly next to each other (of no empty space is specified [counter_size/2, counter_size/2], counters are directly next to each other (of no empty space is specified
in layout). in layout).
""" """
current_y: float = 0.5
starting_at: float = 0.0
current_y: float = starting_at
counters: list[Counter] = [] counters: list[Counter] = []
designated_player_positions: list[npt.NDArray] = [] designated_player_positions: list[npt.NDArray] = []
free_positions: list[npt.NDArray] = [] free_positions: list[npt.NDArray] = []
self.kitchen_width = 0
if self.as_files: if self.as_files:
with open(self.layout_config, "r") as layout_file: with open(self.layout_config, "r") as layout_file:
lines = layout_file.readlines() lines = layout_file.readlines()
else: else:
lines = self.layout_config.split("\n") lines = self.layout_config.split("\n")
self.kitchen_height = len(lines)
for line in lines: for line in lines:
line = line.replace("\n", "").replace(" ", "") # remove newline char line = line.replace("\n", "").replace(" ", "") # remove newline char
current_x = 0.5 current_x: float = starting_at
for character in line: for character in line:
character = character.capitalize() character = character.capitalize()
pos = np.array([current_x, current_y]) pos = np.array([current_x, current_y])
...@@ -316,11 +315,13 @@ class Environment: ...@@ -316,11 +315,13 @@ class Environment:
free_positions.append(np.array([current_x, current_y])) free_positions.append(np.array([current_x, current_y]))
current_x += 1 current_x += 1
if current_x > self.kitchen_width: # if current_x > self.kitchen_width:
self.kitchen_width = current_x # self.kitchen_width = current_x
current_y += 1 current_y += 1
self.kitchen_width -= 0.5 # self.kitchen_width -= 0.5
self.kitchen_width: float = len(lines[0])
self.kitchen_height = len(lines)
self.counter_factory.post_counter_setup(counters) self.counter_factory.post_counter_setup(counters)
...@@ -585,26 +586,27 @@ class Environment: ...@@ -585,26 +586,27 @@ class Environment:
counter.progress(passed_time=passed_time, now=self.env_time) counter.progress(passed_time=passed_time, now=self.env_time)
self.order_and_score.progress(passed_time=passed_time, now=self.env_time) self.order_and_score.progress(passed_time=passed_time, now=self.env_time)
def get_state(self): # def get_state(self):
"""Get the current state of the game environment. The state here is accessible by the current python objects. # """Get the current state of the game environment. The state here is accessible by the current python objects.
#
Returns: Dict of lists of the current relevant game objects. # Returns: Dict of lists of the current relevant game objects.
#
""" # """
return { # return {
"players": self.players, # "players": self.players,
"counters": self.counters, # "counters": self.counters,
"score": self.order_and_score.score, # "score": self.order_and_score.score,
"orders": self.order_and_score.open_orders, # "orders": self.order_and_score.open_orders,
"ended": self.game_ended, # "ended": self.game_ended,
"env_time": self.env_time, # "env_time": self.env_time,
"remaining_time": max(self.env_time_end - self.env_time, timedelta(0)), # "remaining_time": max(self.env_time_end - self.env_time, timedelta(0)),
} # }
def get_json_state(self, player_id: str = None): def get_json_state(self, player_id: str = None):
state = { state = {
"players": [p.to_dict() for p in self.players.values()], "players": [p.to_dict() for p in self.players.values()],
"counters": [c.to_dict() for c in self.counters], "counters": [c.to_dict() for c in self.counters],
"kitchen": {"width": self.kitchen_width, "height": self.kitchen_height},
"score": self.order_and_score.score, "score": self.order_and_score.score,
"orders": self.order_and_score.order_state(), "orders": self.order_and_score.order_state(),
"ended": self.game_ended, "ended": self.game_ended,
......
...@@ -50,9 +50,15 @@ class PlayerState(TypedDict): ...@@ -50,9 +50,15 @@ class PlayerState(TypedDict):
current_nearest_counter_id: str | None current_nearest_counter_id: str | None
class KitchenInfo(BaseModel):
width: float
height: float
class StateRepresentation(BaseModel): class StateRepresentation(BaseModel):
players: list[PlayerState] players: list[PlayerState]
counters: list[CounterState] counters: list[CounterState]
kitchen: KitchenInfo
score: float | int score: float | int
orders: list[OrderState] orders: list[OrderState]
ended: bool ended: bool
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment