Save the inventory of the player when saving the game, fixes #33

This commit is contained in:
Yohann D'ANELLO
2020-12-05 14:35:59 +01:00
parent f887a1f0aa
commit c38f8cdc53
2 changed files with 15 additions and 2 deletions

View File

@ -19,7 +19,8 @@ class Player(FightingEntity):
def __init__(self, name: str = "player", maxhealth: int = 20,
strength: int = 5, intelligence: int = 1, charisma: int = 1,
dexterity: int = 1, constitution: int = 1, level: int = 1,
current_xp: int = 0, max_xp: int = 10, *args, **kwargs) \
current_xp: int = 0, max_xp: int = 10, inventory: list = None,
*args, **kwargs) \
-> None:
super().__init__(name=name, maxhealth=maxhealth, strength=strength,
intelligence=intelligence, charisma=charisma,
@ -27,7 +28,12 @@ class Player(FightingEntity):
level=level, *args, **kwargs)
self.current_xp = current_xp
self.max_xp = max_xp
self.inventory = list()
self.inventory = inventory if inventory else list()
for i in range(len(self.inventory)):
if isinstance(self.inventory[i], dict):
entity_classes = self.get_all_entity_classes_in_a_dict()
item_class = entity_classes[self.inventory[i]["type"]]
self.inventory[i] = item_class(**self.inventory[i])
self.paths = dict()
def move(self, y: int, x: int) -> None:
@ -118,4 +124,5 @@ class Player(FightingEntity):
d = super().save_state()
d["current_xp"] = self.current_xp
d["max_xp"] = self.max_xp
d["inventory"] = [item.save_state() for item in self.inventory]
return d