Skip to content

Commit

Permalink
Station (#9)
Browse files Browse the repository at this point in the history
* base of generic station

more work required

* basics of occupied_station

* Station: minor fixes

* Station: major fix

occupied_station didn't have .py... yeah

* fixed station classes

* station / avatar fixes

occupied_by changed to GameObject, isinstance added to setter methods
  • Loading branch information
MoodyMan04 authored Mar 2, 2023
1 parent 9518a3f commit 7236b50
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 7 deletions.
12 changes: 6 additions & 6 deletions game/common/avatar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 3 additions & 1 deletion game/common/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class ObjectType(Enum):
GAMEBOARD = auto()
TILE = auto()
ITEM = auto()
STATION = auto()
OCCUPIABLE_STATION = auto()


class ActionType(Enum):
Expand All @@ -28,4 +30,4 @@ class ActionType(Enum):
INTERACT_DOWN = auto()
INTERACT_LEFT = auto()
INTERACT_RIGHT = auto()
INTERACT_CENTER = auto()
INTERACT_CENTER = auto()
6 changes: 6 additions & 0 deletions game/common/map/tile.py
Original file line number Diff line number Diff line change
@@ -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"""
Expand Down Expand Up @@ -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
Empty file.
49 changes: 49 additions & 0 deletions game/common/stations/occupiable_station.py
Original file line number Diff line number Diff line change
@@ -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


49 changes: 49 additions & 0 deletions game/common/stations/station.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 7236b50

Please sign in to comment.