diff --git a/game/common/avatar.py b/game/common/avatar.py index 9d7a380..47709dc 100644 --- a/game/common/avatar.py +++ b/game/common/avatar.py @@ -28,20 +28,20 @@ def position(self) -> tuple[int, int]: @held_item.setter def held_item(self, item: Item): # If it's not an item, and it's not None, raise the error - if not isinstance(item, Item) and item: - raise ValueError("avatar.held_item must be an Item or None.") + if item and not isinstance(item, Item): + raise ValueError(f"{self.__class__.__name__}.held_item must be an Item or None.") self.__held_item = item @score.setter def score(self, score: int): - if not isinstance(score, int): - raise ValueError("avatar.score must be an int.") + if score and not isinstance(score, int): + raise ValueError(f"{self.__class__.__name__}.score must be an int.") self.__score = score @position.setter def position(self, position: tuple[int, int]): - if not(isinstance(position, tuple) and list(map(type, position)) == [int, int]) and position: - raise ValueError("avatar.position must be a tuple of two ints.") + if position and not(isinstance(position, tuple) and list(map(type, position)) == [int, int]): + raise ValueError(f"{self.__class__.__name__}.position must be a tuple of two ints.") self.__position = position def to_json(self) -> dict: diff --git a/game/common/enums.py b/game/common/enums.py index 987da58..43f8be2 100644 --- a/game/common/enums.py +++ b/game/common/enums.py @@ -16,6 +16,8 @@ class ObjectType(Enum): GAMEBOARD = auto() TILE = auto() ITEM = auto() + STATION = auto() + OCCUPIABLE_STATION = auto() class ActionType(Enum): @@ -28,4 +30,4 @@ class ActionType(Enum): INTERACT_DOWN = auto() INTERACT_LEFT = auto() INTERACT_RIGHT = auto() - INTERACT_CENTER = auto() + INTERACT_CENTER = auto() \ No newline at end of file diff --git a/game/common/map/tile.py b/game/common/map/tile.py index 98eea7c..88a5fb4 100644 --- a/game/common/map/tile.py +++ b/game/common/map/tile.py @@ -1,6 +1,8 @@ from game.common.enums import ObjectType from game.common.game_object import GameObject from game.common.avatar import Avatar +from game.common.stations.occupiable_station import Occupiable_Station +from game.common.stations.station import Station from typing import Self """This object exists to encapsulate all objects that could be placed on the gameboard""" @@ -37,6 +39,10 @@ def from_json(self, data: dict) -> Self: match occupied_by["object_type"]: case ObjectType.AVATAR: self.occupied_by = Avatar().from_json(data['occupied_by']) + case ObjectType.OCCUPIABLE_STATION: + self.occupied_by = Occupiable_Station().from_json(data['occupied_by']) + case ObjectType.STATION: + self.occupied_by = Station().from_json(data['occupied_by']) case _: raise Exception(f'Could not parse occupied_by: {self.occupied_by}') return self \ No newline at end of file diff --git a/game/common/stations/__init__.py b/game/common/stations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/game/common/stations/occupiable_station.py b/game/common/stations/occupiable_station.py new file mode 100644 index 0000000..8a9276f --- /dev/null +++ b/game/common/stations/occupiable_station.py @@ -0,0 +1,49 @@ +from game.common.avatar import Avatar +from game.common.enums import ObjectType +from game.common.items.item import Item +from game.common.stations.station import Station +from game.common.game_object import GameObject +from typing import Self + +# create station object that contains occupied_by +class Occupiable_Station(Station): + def __init__(self, item: Item = None, occupied_by: GameObject = None): + super().__init__(self, item) + self.object_type = ObjectType.OCCUPIABLE_STATION + self.occupied_by = occupied_by + + # occupied_by getter and setter methods + @property + def occupied_by(self) -> GameObject: + return self.__occupied_by + + @occupied_by.setter + def occupied_by(self, occupied_by: GameObject): + if occupied_by and not isinstance(occupied_by, GameObject): + raise ValueError(f"{self.__class__.__name__}.occupied_by must be a GameObject.") + self.__occupied_by = occupied_by + + # take action method + def take_action(self, avatar: Avatar = None): + return + + # json methods + def to_json(self) -> dict: + dict_data = super().to_json() + dict_data['occupied_by'] = self.occupied_by + return dict_data + + def from_json(self, data: dict) -> Self: + super().from_json(data) + self.occupied_by = data['occupied_by'] + + # framework match case for from json, can add more cases if they can occupy station + match self.occupied_by["object_type"]: + case ObjectType.AVATAR: + self.occupied_by = Avatar().from_json(data['occupied_by']) + case _: + raise Exception(f'Could not parse occupied_by: {self.occupied_by}') + + return self + + \ No newline at end of file diff --git a/game/common/stations/station.py b/game/common/stations/station.py new file mode 100644 index 0000000..6b350c7 --- /dev/null +++ b/game/common/stations/station.py @@ -0,0 +1,49 @@ +from game.common.avatar import Avatar +from game.common.game_object import GameObject +from game.common.enums import ObjectType +from game.common.items.item import Item +from typing import Self + +# create Station object from GameObject that allows item to be contained in it +class Station(GameObject): + def __init__(self, item: Item = None): + super().__init__() + self.object_type = ObjectType.station + self.item: Item = item + + # item getter and setter methods + @property + def item(self) -> Item: + return self.__item + + @item.setter + def item(self, item: Item): + if item and not isinstance(item, Item): + raise ValueError(f"{self.__class__.__name__}.item must be an Item.") + self.__item = item + + # take action method + def take_action(self, avatar: Avatar = None): + return + + # json methods + def to_json(self) -> dict: + dict_data = super().to_json() + dict_data['item'] = self.item.to_json() if self.item else None + + return dict_data + + def from_json(self, data: dict) -> Self: + super().from_json(data) + if not data['item']: + self.item = None + + # framework match case for from json, can add more object types that can be item + match self.item["object_type"]: + case ObjectType.ITEM: + self.item = Item().from_json(data['item']) + case _: + raise Exception(f'Could not parse item: {self.item}') + + return self + \ No newline at end of file