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

new cooking equipment class for pot, pan and frier. Should now work. Next...

new cooking equipment class for pot, pan and frier. Should now work. Next steps: based on recipes and not only hard coded tomato
parent c3c4c786
No related branches found
No related tags found
1 merge request!10Resolve "Pot and stove and cooking tomato soup"
Pipeline #41581 passed
...@@ -15,7 +15,7 @@ from overcooked_simulator.game_items import ( ...@@ -15,7 +15,7 @@ from overcooked_simulator.game_items import (
Plate, Plate,
Tomato, Tomato,
Pot, Pot,
Pan, CookingEquipment,
) )
...@@ -33,9 +33,17 @@ class Counter: ...@@ -33,9 +33,17 @@ class Counter:
Returns: The item which the counter is occupied by. None if nothing is there. Returns: The item which the counter is occupied by. None if nothing is there.
""" """
give_player = self.occupied_by if on_hands:
if self.occupied_by:
occupied_by = self.occupied_by
self.occupied_by = None
return occupied_by
return None
if self.occupied_by and isinstance(self.occupied_by, CookingEquipment):
return self.occupied_by.release()
occupied_by = self.occupied_by
self.occupied_by = None self.occupied_by = None
return give_player return occupied_by
def can_drop_off(self, item: Item) -> bool: def can_drop_off(self, item: Item) -> 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
...@@ -61,7 +69,7 @@ class Counter: ...@@ -61,7 +69,7 @@ class Counter:
if self.occupied_by is None: if self.occupied_by is None:
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) return self.occupied_by.combine(item)
return None return None
def interact_start(self): def interact_start(self):
...@@ -160,8 +168,7 @@ class PlateReturn(Counter): ...@@ -160,8 +168,7 @@ class PlateReturn(Counter):
self.occupied_by.append(item) self.occupied_by.append(item)
return None return None
if self.occupied_by[-1].can_combine(item): if self.occupied_by[-1].can_combine(item):
self.occupied_by[-1].combine(item) return self.occupied_by[-1].combine(item)
return None
return item return item
def can_drop_off(self, item: Item) -> bool: def can_drop_off(self, item: Item) -> bool:
...@@ -201,6 +208,9 @@ class Trash(Counter): ...@@ -201,6 +208,9 @@ class Trash(Counter):
if isinstance(item, Plate): if isinstance(item, Plate):
item.holds = None item.holds = None
return item return item
if isinstance(item, CookingEquipment):
item.content = None
return item
return None return None
def can_drop_off(self, item: Item) -> bool: def can_drop_off(self, item: Item) -> bool:
...@@ -213,32 +223,25 @@ class Stove(Counter): ...@@ -213,32 +223,25 @@ class Stove(Counter):
occupied_by = Pot() occupied_by = Pot()
super().__init__(pos, occupied_by) super().__init__(pos, occupied_by)
def pick_up(self, on_hands: bool = True): # def drop_off(self, item) -> Item | None:
if on_hands: # if isinstance(item, (Pot, Pan)):
if self.occupied_by: # self.occupied_by = item
occupied_by = self.occupied_by # else:
self.occupied_by = None # self.occupied_by.combine(item)
return occupied_by # return None
return None #
if self.occupied_by and self.occupied_by.holds: # def can_drop_off(self, item) -> bool:
soup = self.occupied_by.holds.pop(0) # if isinstance(item, (Pot, Pan)):
return soup # return not self.occupied_by
# if self.occupied_by:
def drop_off(self, item) -> Item | None: # return self.occupied_by.can_combine(item)
if isinstance(item, (Pot, Pan)): # return False
self.occupied_by = item
else:
self.occupied_by.combine(item)
return None
def can_drop_off(self, item) -> bool:
if isinstance(item, (Pot, Pan)):
return not self.occupied_by
if self.occupied_by:
return self.occupied_by.can_combine(item)
return False
def progress(self): def progress(self):
"""Called by environment step function for time progression""" """Called by environment step function for time progression"""
if self.occupied_by and self.occupied_by.can_cook(): if (
self.occupied_by
and isinstance(self.occupied_by, CookingEquipment)
and self.occupied_by.can_progress()
):
self.occupied_by.progress() self.occupied_by.progress()
from __future__ import annotations
class Item: class Item:
"""Base class for game items which can be held by a player.""" """Base class for game items which can be held by a player."""
def __init__(self, name: str = None, *args, **kwargs):
super().__init__(*args, **kwargs)
self.name = self.__class__.__name__ if name is None else name
def can_combine(self, other): def can_combine(self, other):
return False return False
def combine(self, other): def combine(self, other) -> Item | None:
pass pass
def __repr__(self): def __repr__(self):
return self.__class__.__name__ return f"{self.name}({self.extra_repr})"
@property
def extra_repr(self):
return ""
class Plate(Item): class Plate(Item):
def __init__(self, holds: Item = None): def __init__(self, holds: Item = None):
super().__init__()
self.clean = True self.clean = True
self.holds = holds self.holds = holds
super().__init__()
def can_combine(self, other: Item) -> bool: def can_combine(self, other: Item) -> bool:
if self.holds is None: if self.holds is None:
if isinstance(other, (Pot, Pan)): if isinstance(other, CookingEquipment):
return other.finished return other.can_release_content()
return not isinstance(other, Plate) return not isinstance(other, Plate)
return False return False
def combine(self, other): def combine(self, other):
if isinstance(other, CookingEquipment):
self.holds = other.release()
return other
self.holds = other self.holds = other
def __repr__(self): @property
return f"Plate({self.holds})" def extra_repr(self):
return self.holds
class ProgressibleItem(Item): class ProgressibleItem:
"""Class for items which need to be processed (cut, cooked, ...)""" """Class for items which need to be processed (cut, cooked, ...)"""
def __init__(self, finished: bool = False, steps_needed: int = 1500): def __init__(
self,
finished: bool = False,
steps_needed: int = 1500,
finished_name: str = None,
*args,
**kwargs,
):
super().__init__(*args, **kwargs)
self.progressed_steps = steps_needed if finished else 0 self.progressed_steps = steps_needed if finished else 0
self.steps_needed = steps_needed self.steps_needed = steps_needed
self.finished = finished self.finished = finished
self.finished_name = "Cutted" self.finished_name = (
super().__init__() f"Cutted{self.name}" if finished_name is None else finished_name
)
def progress(self): def progress(self):
"""Progresses the item process as long as it is not finished.""" """Progresses the item process as long as it is not finished."""
...@@ -50,14 +73,21 @@ class ProgressibleItem(Item): ...@@ -50,14 +73,21 @@ class ProgressibleItem(Item):
if not self.finished: if not self.finished:
self.progressed_steps += 1 self.progressed_steps += 1
def can_progress(self) -> bool:
return True
def finished_call(self): def finished_call(self):
pass self.name = self.finished_name
def reset(self):
self.finished = False
self.progressed_steps = 0
def __repr__(self): def __repr__(self):
if self.finished: if self.finished:
return f"{self.finished_name}{self.__class__.__name__}()" return f"{self.name}({self.extra_repr})"
else: else:
return f"{self.__class__.__name__}(progress={int(self.progressed_steps / self.steps_needed * 100)}%)" return f"{self.name}(progress={int(self.progressed_steps / self.steps_needed * 100)}%,{self.extra_repr})"
class CuttableItem(ProgressibleItem): class CuttableItem(ProgressibleItem):
...@@ -66,7 +96,7 @@ class CuttableItem(ProgressibleItem): ...@@ -66,7 +96,7 @@ class CuttableItem(ProgressibleItem):
pass pass
class Tomato(CuttableItem): class Tomato(CuttableItem, Item):
"""Item class representing a tomato. Can be cut on the cutting board""" """Item class representing a tomato. Can be cut on the cutting board"""
def can_combine(self, other): def can_combine(self, other):
...@@ -76,42 +106,82 @@ class Tomato(CuttableItem): ...@@ -76,42 +106,82 @@ class Tomato(CuttableItem):
super().__init__(steps_needed=1500) super().__init__(steps_needed=1500)
class Pot(ProgressibleItem): class CookingEquipment(Item):
def __init__(self, holds: Item = None): def __init__(self, content: Meal = None, *args, **kwargs):
self.holds = [] if holds is None else holds super().__init__(*args, **kwargs)
super().__init__(steps_needed=1500) self.content = content
self.finished_name = "Cooked"
def can_combine(self, other): def can_combine(self, other):
# TODO based on recipes if self.content is None:
# TODO check other is start of a meal, create meal
return True
return self.content.can_combine(other)
def combine(self, other):
if not self.content:
# find starting meal for other
self.content = Soup()
self.content.combine(other)
def can_progress(self, counter_type="Stove") -> bool:
return ( return (
isinstance(other, Tomato) self.content
# and other.finished and isinstance(self.content, ProgressibleItem)
and len(self.holds) < 3 and self.content.can_progress()
and all([isinstance(h, Tomato) for h in self.holds])
) )
def finished_call(self): def progress(self):
self.holds = [TomatoSoup()] self.content.progress()
def combine(self, other): def can_release_content(self) -> bool:
self.holds.append(other) return (
self.content
and isinstance(self.content, ProgressibleItem)
and self.content.finished
)
def can_cook(self) -> bool: def release(self):
return self.holds and len(self.holds) == 3 content = self.content
self.content = None
return content
def __repr__(self): @property
if self.finished: def extra_repr(self):
return f"{self.finished_name}{self.__class__.__name__}({self.holds})" return self.content
else:
return f"{self.__class__.__name__}({self.holds},progress={int(self.progressed_steps / self.steps_needed * 100)}%)"
class Pot(CookingEquipment):
def __init__(self, holds: Meal = None):
super().__init__()
class Meal(Item):
def __init__(self, parts=None, *args, **kwargs):
super().__init__(*args, **kwargs)
self.parts = [] if parts is None else parts
# self.rules ...
def can_combine(self, other) -> bool:
return (
isinstance(other, Tomato)
and all([isinstance(o, other.__class__) for o in self.parts])
and len(self.parts) < 3
) # rules
def combine(self, other):
self.parts.append(other)
@property
def extra_repr(self):
return self.parts
class TomatoSoup(Item): class Soup(ProgressibleItem, Meal):
... def can_progress(self) -> bool:
return len(self.parts) == 3
class Pan(ProgressibleItem): class Pan(CookingEquipment):
def __init__(self): def __init__(self):
super().__init__(steps_needed=1500) super().__init__(steps_needed=1500)
......
...@@ -18,7 +18,7 @@ from overcooked_simulator.game_items import ( ...@@ -18,7 +18,7 @@ from overcooked_simulator.game_items import (
Plate, Plate,
Item, Item,
Pot, Pot,
TomatoSoup, Soup,
) )
from overcooked_simulator.game_items import Tomato from overcooked_simulator.game_items import Tomato
from overcooked_simulator.overcooked_environment import Action from overcooked_simulator.overcooked_environment import Action
...@@ -231,8 +231,11 @@ class PyGameGUI: ...@@ -231,8 +231,11 @@ class PyGameGUI:
if isinstance(item, Pot): if isinstance(item, Pot):
pot_size = 15 pot_size = 15
pygame.draw.circle(self.screen, GREY, pos, pot_size) pygame.draw.circle(self.screen, GREY, pos, pot_size)
if item.content:
self.draw_item(pos, item.content)
if isinstance(item, Soup):
if not item.finished: if not item.finished:
match len(item.holds): match len(item.parts):
case 1: case 1:
pygame.draw.circle(self.screen, RED, pos, 4) pygame.draw.circle(self.screen, RED, pos, 4)
case 2: case 2:
...@@ -240,17 +243,14 @@ class PyGameGUI: ...@@ -240,17 +243,14 @@ class PyGameGUI:
case 3: case 3:
pygame.draw.circle(self.screen, RED, pos, 12) pygame.draw.circle(self.screen, RED, pos, 12)
else: else:
print(item.holds) """https://www.readersdigest.ca/wp-content/uploads/2020/11/The-Best-Ever-Tomato-Soup_EXPS_THSO18_222724_D03_06_5b-4.jpg"""
self.draw_item(pos, item.holds[0]) image = pygame.image.load(
if isinstance(item, TomatoSoup): self.images_path / "tomato_soup.png"
"""https://www.readersdigest.ca/wp-content/uploads/2020/11/The-Best-Ever-Tomato-Soup_EXPS_THSO18_222724_D03_06_5b-4.jpg""" ).convert_alpha()
image = pygame.image.load( image = pygame.transform.scale(image, (24, 24))
self.images_path / "tomato_soup.png" rect = image.get_rect()
).convert_alpha() rect.center = pos
image = pygame.transform.scale(image, (24, 24)) self.screen.blit(image, rect)
rect = image.get_rect()
rect.center = pos
self.screen.blit(image, rect)
if isinstance(item, ProgressibleItem) and not item.finished: if isinstance(item, ProgressibleItem) and not item.finished:
self.draw_progress_bar(pos, item.progressed_steps, item.steps_needed) self.draw_progress_bar(pos, item.progressed_steps, item.steps_needed)
......
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