Skip to content
Snippets Groups Projects
Commit 06c33962 authored by Florian Schröder's avatar Florian Schröder
Browse files

changed pick_up method to return the item in the players hand. Plates are now...

changed pick_up method to return the item in the players hand. Plates are now stackable on the PlateReturn. occupied by is now a list in the plate return
parent fa429c24
No related branches found
No related tags found
1 merge request!8Resolve "Can put tomatos on returned plate"
...@@ -30,7 +30,7 @@ class Counter: ...@@ -30,7 +30,7 @@ class Counter:
self.occupied_by = None self.occupied_by = None
return give_player return give_player
def can_drop_off(self, item: HoldableItem): def can_drop_off(self, item: HoldableItem) -> bool:
"""Checks whether an item by the player can be dropped of. More relevant for example with """Checks whether an item by the player can be dropped of. More relevant for example with
ingredient dispensers, which should always be occupied and cannot take an item. ingredient dispensers, which should always be occupied and cannot take an item.
...@@ -42,7 +42,7 @@ class Counter: ...@@ -42,7 +42,7 @@ class Counter:
""" """
return self.occupied_by is None or self.occupied_by.can_combine(item) return self.occupied_by is None or self.occupied_by.can_combine(item)
def drop_off(self, item: HoldableItem): def drop_off(self, item: HoldableItem) -> HoldableItem | None:
"""Takes the thing dropped of by the player. """Takes the thing dropped of by the player.
Args: Args:
...@@ -55,6 +55,7 @@ class Counter: ...@@ -55,6 +55,7 @@ class Counter:
self.occupied_by = item self.occupied_by = item
elif self.occupied_by.can_combine(item): elif self.occupied_by.can_combine(item):
self.occupied_by.combine(item) self.occupied_by.combine(item)
return None
def interact_start(self): def interact_start(self):
"""Starts an interaction by the player. Nothing happens for the standard counter.""" """Starts an interaction by the player. Nothing happens for the standard counter."""
...@@ -100,14 +101,14 @@ class CuttingBoard(Counter): ...@@ -100,14 +101,14 @@ class CuttingBoard(Counter):
class ServingWindow(Counter): class ServingWindow(Counter):
def drop_off(self, item): def drop_off(self, item) -> HoldableItem | None:
return 5 return None
def can_score(self, item): def can_score(self, item):
if isinstance(item, Plate) and isinstance(item.holds, ProgressibleItem): if isinstance(item, Plate) and isinstance(item.holds, ProgressibleItem):
return item.holds.finished return item.holds.finished
def can_drop_off(self, item: HoldableItem): def can_drop_off(self, item: HoldableItem) -> bool:
return self.can_score(item) return self.can_score(item)
def pick_up(self): def pick_up(self):
...@@ -120,7 +121,7 @@ class ServingWindow(Counter): ...@@ -120,7 +121,7 @@ class ServingWindow(Counter):
class PlateReturn(Counter): class PlateReturn(Counter):
def __init__(self, pos): def __init__(self, pos):
super().__init__(pos) super().__init__(pos)
self.occupied_by = Plate() self.occupied_by = [Plate()]
def pick_up(self): def pick_up(self):
"""Gets called upon a player performing the pickup action. Gives back a plate (possibly with ingredient. """Gets called upon a player performing the pickup action. Gives back a plate (possibly with ingredient.
...@@ -128,20 +129,28 @@ class PlateReturn(Counter): ...@@ -128,20 +129,28 @@ class PlateReturn(Counter):
Returns: A plate possibly with an ingredient on it. Returns: A plate possibly with an ingredient on it.
""" """
give_player = self.occupied_by give_player = self.occupied_by.pop()
self.occupied_by = Plate() if not self.occupied_by:
self.occupied_by.append(Plate())
return give_player return give_player
def drop_off(self, item: HoldableItem): def drop_off(self, item: HoldableItem) -> HoldableItem | None:
"""Takes the ingredient dropped of by the player. """Takes the ingredient dropped of by the player.
Args: Args:
item: The ingredient to be placed on the counter. item: The ingredient to be placed on the counter.
""" """
if item is Plate() and self.occupied_by is Plate(): if isinstance(item, Plate):
self.occupied_by = None if self.occupied_by[-1].holds:
return item
def can_drop_off(self, item: HoldableItem): self.occupied_by.append(item)
return None
if self.occupied_by[-1].can_combine(item):
self.occupied_by[-1].combine(item)
return None
return item
def can_drop_off(self, item: HoldableItem) -> bool:
"""Checks whether an ingredient by the player can be dropped of. """Checks whether an ingredient by the player can be dropped of.
Args: Args:
...@@ -152,8 +161,8 @@ class PlateReturn(Counter): ...@@ -152,8 +161,8 @@ class PlateReturn(Counter):
""" """
# possibility to drop off empty plate on empty plate return # possibility to drop off empty plate on empty plate return
return ( return (
isinstance(self.occupied_by, Plate) and isinstance(item, Plate) isinstance(self.occupied_by[-1], Plate) and isinstance(item, Plate)
) or self.occupied_by.can_combine(item) ) or self.occupied_by[-1].can_combine(item)
def __repr__(self): def __repr__(self):
return "PlateReturn" return "PlateReturn"
...@@ -166,10 +175,10 @@ class TomatoDispenser(Counter): ...@@ -166,10 +175,10 @@ class TomatoDispenser(Counter):
def pick_up(self): def pick_up(self):
return Tomato() return Tomato()
def drop_off(self, item: HoldableItem): def drop_off(self, item: HoldableItem) -> HoldableItem | None:
return 0 return None
def can_drop_off(self, item: HoldableItem): def can_drop_off(self, item: HoldableItem) -> bool:
return False return False
def __repr__(self): def __repr__(self):
...@@ -180,12 +189,13 @@ class Trash(Counter): ...@@ -180,12 +189,13 @@ class Trash(Counter):
def pick_up(self): def pick_up(self):
pass pass
def drop_off(self, item: HoldableItem): def drop_off(self, item: HoldableItem) -> HoldableItem | None:
if isinstance(item, Plate): if isinstance(item, Plate):
item.holds = None item.holds = None
return -1 return item
return None
def can_drop_off(self, item: HoldableItem): def can_drop_off(self, item: HoldableItem) -> bool:
return True return True
def __repr__(self): def __repr__(self):
......
...@@ -9,9 +9,9 @@ class HoldableItem: ...@@ -9,9 +9,9 @@ class HoldableItem:
class Plate(HoldableItem): class Plate(HoldableItem):
def __init__(self): def __init__(self, holds: HoldableItem = None):
self.clean = True self.clean = True
self.holds = None self.holds = holds
super().__init__() super().__init__()
......
...@@ -13,20 +13,22 @@ from overcooked_simulator.counters import ( ...@@ -13,20 +13,22 @@ from overcooked_simulator.counters import (
Counter, Counter,
CuttingBoard, CuttingBoard,
Trash, Trash,
ServingWindow,
TomatoDispenser, TomatoDispenser,
ServingWindow,
PlateReturn, PlateReturn,
) )
SYMBOL_TO_CHARACTER_MAP = {
"C": Counter, class GameScore:
"B": CuttingBoard, def __init__(self):
"X": Trash, self.score = 0
"W": ServingWindow,
"T": TomatoDispenser, def increment_score(self, score: int):
"P": PlateReturn, self.score += score
"E": None, print(self.score)
}
def read_score(self):
return self.score
class Action: class Action:
...@@ -57,6 +59,18 @@ class Environment: ...@@ -57,6 +59,18 @@ class Environment:
self.players: dict[str, Player] = {} self.players: dict[str, Player] = {}
self.counter_side_length: int = 40 self.counter_side_length: int = 40
self.layout_path: Path = layout_path self.layout_path: Path = layout_path
self.game_score = GameScore()
self.SYMBOL_TO_CHARACTER_MAP = {
"C": Counter,
"B": CuttingBoard,
"X": Trash,
"W": lambda pos: ServingWindow(pos, self.game_score),
"T": TomatoDispenser,
"P": PlateReturn,
"E": None,
}
self.counters: list[Counter] = self.create_counters(self.layout_path) self.counters: list[Counter] = self.create_counters(self.layout_path)
self.score: int = 0 self.score: int = 0
self.world_width: int = 800 self.world_width: int = 800
...@@ -82,7 +96,7 @@ class Environment: ...@@ -82,7 +96,7 @@ class Environment:
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])
counter_class = SYMBOL_TO_CHARACTER_MAP[character] counter_class = self.SYMBOL_TO_CHARACTER_MAP[character]
if counter_class is not None: if counter_class is not None:
counter = counter_class(pos) counter = counter_class(pos)
counters.append(counter) counters.append(counter)
......
...@@ -3,8 +3,8 @@ from typing import Optional ...@@ -3,8 +3,8 @@ from typing import Optional
import numpy as np import numpy as np
import numpy.typing as npt import numpy.typing as npt
from overcooked_simulator.counters import Counter, Trash from overcooked_simulator.counters import Counter
from overcooked_simulator.game_items import HoldableItem, Plate from overcooked_simulator.game_items import HoldableItem
class Player: class Player:
...@@ -78,11 +78,7 @@ class Player: ...@@ -78,11 +78,7 @@ class Player:
self.holding = counter.pick_up() self.holding = counter.pick_up()
elif counter.can_drop_off(self.holding): elif counter.can_drop_off(self.holding):
if isinstance(counter, Trash) and isinstance(self.holding, Plate): self.holding = counter.drop_off(self.holding)
self.holding.holds = None
else:
counter.drop_off(self.holding)
self.holding = None
elif self.holding.can_combine(counter.occupied_by): elif self.holding.can_combine(counter.occupied_by):
returned_by_counter = counter.pick_up() returned_by_counter = counter.pick_up()
......
...@@ -284,7 +284,13 @@ class PyGameGUI: ...@@ -284,7 +284,13 @@ class PyGameGUI:
pygame.draw.rect(self.screen, YELLOW, board_rect) pygame.draw.rect(self.screen, YELLOW, board_rect)
if counter.occupied_by is not None: if counter.occupied_by is not None:
self.draw_item(counter.pos, counter.occupied_by) if isinstance(counter.occupied_by, list):
for i, o in enumerate(counter.occupied_by):
self.draw_item(
np.abs([counter.pos[0], counter.pos[1] - (i * 3)]), o
)
else:
self.draw_item(counter.pos, counter.occupied_by)
def draw_counters(self, state): def draw_counters(self, state):
"""Visualizes the counters in the environment. """Visualizes the counters in the environment.
......
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