Merchant inventory is well-saved
This commit is contained in:
parent
099508d4c0
commit
2bb99a707e
|
@ -23,8 +23,17 @@ class Merchant(FriendlyEntity):
|
||||||
super().__init__(name=name, *args, **kwargs)
|
super().__init__(name=name, *args, **kwargs)
|
||||||
self.inventory = inventory or []
|
self.inventory = inventory or []
|
||||||
self.hazel = hazel
|
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:
|
def talk_to(self, player: Player) -> str:
|
||||||
"""
|
"""
|
||||||
|
@ -34,6 +43,14 @@ class Merchant(FriendlyEntity):
|
||||||
# TODO
|
# TODO
|
||||||
return _("I don't sell any squirrel")
|
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):
|
class Sunflower(FriendlyEntity):
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -30,9 +30,9 @@ class Player(FightingEntity):
|
||||||
self.current_xp = current_xp
|
self.current_xp = current_xp
|
||||||
self.max_xp = max_xp
|
self.max_xp = max_xp
|
||||||
self.inventory = inventory if inventory else list()
|
self.inventory = inventory if inventory else list()
|
||||||
|
entity_classes = self.get_all_entity_classes_in_a_dict()
|
||||||
for i in range(len(self.inventory)):
|
for i in range(len(self.inventory)):
|
||||||
if isinstance(self.inventory[i], dict):
|
if isinstance(self.inventory[i], dict):
|
||||||
entity_classes = self.get_all_entity_classes_in_a_dict()
|
|
||||||
item_class = entity_classes[self.inventory[i]["type"]]
|
item_class = entity_classes[self.inventory[i]["type"]]
|
||||||
self.inventory[i] = item_class(**self.inventory[i])
|
self.inventory[i] = item_class(**self.inventory[i])
|
||||||
self.paths = dict()
|
self.paths = dict()
|
||||||
|
|
|
@ -362,7 +362,8 @@ class Entity:
|
||||||
from squirrelbattle.entities.monsters import Tiger, Hedgehog, Rabbit, \
|
from squirrelbattle.entities.monsters import Tiger, Hedgehog, Rabbit, \
|
||||||
TeddyBear
|
TeddyBear
|
||||||
from squirrelbattle.entities.friendly import Merchant, Sunflower
|
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 {
|
return {
|
||||||
"Tiger": Tiger,
|
"Tiger": Tiger,
|
||||||
"Bomb": Bomb,
|
"Bomb": Bomb,
|
||||||
|
@ -374,6 +375,7 @@ class Entity:
|
||||||
"Player": Player,
|
"Player": Player,
|
||||||
"Merchant": Merchant,
|
"Merchant": Merchant,
|
||||||
"Sunflower": Sunflower,
|
"Sunflower": Sunflower,
|
||||||
|
"Sword": Sword,
|
||||||
}
|
}
|
||||||
|
|
||||||
def save_state(self) -> dict:
|
def save_state(self) -> dict:
|
||||||
|
|
Loading…
Reference in New Issue