diff --git a/squirrelbattle/entities/friendly.py b/squirrelbattle/entities/friendly.py index 221707a..52d2cd2 100644 --- a/squirrelbattle/entities/friendly.py +++ b/squirrelbattle/entities/friendly.py @@ -23,8 +23,17 @@ class Merchant(FriendlyEntity): super().__init__(name=name, *args, **kwargs) self.inventory = inventory or [] self.hazel = hazel - for i in range(5): - self.inventory.append(choice(Item.get_all_items())()) + + entity_classes = self.get_all_entity_classes_in_a_dict() + + for i in range(len(self.inventory)): + if isinstance(self.inventory[i], dict): + item_class = entity_classes[self.inventory[i]["type"]] + self.inventory[i] = item_class(**self.inventory[i]) + + if not self.inventory: + for i in range(5): + self.inventory.append(choice(Item.get_all_items())()) def talk_to(self, player: Player) -> str: """ @@ -34,6 +43,14 @@ class Merchant(FriendlyEntity): # TODO return _("I don't sell any squirrel") + def save_state(self) -> dict: + """ + We save the inventory of the merchant formatted as JSON + """ + d = super().save_state() + d["inventory"] = [item.save_state() for item in self.inventory] + return d + class Sunflower(FriendlyEntity): """ diff --git a/squirrelbattle/entities/player.py b/squirrelbattle/entities/player.py index dce6839..48a5b7f 100644 --- a/squirrelbattle/entities/player.py +++ b/squirrelbattle/entities/player.py @@ -30,9 +30,9 @@ class Player(FightingEntity): self.current_xp = current_xp self.max_xp = max_xp self.inventory = inventory if inventory else list() + entity_classes = self.get_all_entity_classes_in_a_dict() 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() diff --git a/squirrelbattle/interfaces.py b/squirrelbattle/interfaces.py index 1984465..15f6852 100644 --- a/squirrelbattle/interfaces.py +++ b/squirrelbattle/interfaces.py @@ -362,7 +362,8 @@ class Entity: from squirrelbattle.entities.monsters import Tiger, Hedgehog, Rabbit, \ TeddyBear from squirrelbattle.entities.friendly import Merchant, Sunflower - from squirrelbattle.entities.items import BodySnatchPotion, Bomb, Heart + from squirrelbattle.entities.items import BodySnatchPotion, Bomb, \ + Heart, Sword return { "Tiger": Tiger, "Bomb": Bomb, @@ -374,6 +375,7 @@ class Entity: "Player": Player, "Merchant": Merchant, "Sunflower": Sunflower, + "Sword": Sword, } def save_state(self) -> dict: