Fix for loading game in progress, there remains to change all entities __init__ to allow being initialized by a dictionnary (work in progress, breaks the game)

This commit is contained in:
eichhornchen
2020-11-18 22:42:46 +01:00
parent 7ae4e47fc3
commit 657345e6f7
5 changed files with 139 additions and 48 deletions

View File

@ -13,16 +13,34 @@ class Player(FightingEntity):
inventory: list
paths: Dict[Tuple[int, int], Tuple[int, int]]
def __init__(self, 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) -> None:
super().__init__(name="player", maxhealth=maxhealth, strength=strength,
intelligence=intelligence, charisma=charisma,
dexterity=dexterity, constitution=constitution,
level=level, *args, **kwargs)
self.current_xp = current_xp
self.max_xp = max_xp
## def __init__(self, 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) -> None:
## super().__init__(name="player", maxhealth=maxhealth, strength=strength,
## intelligence=intelligence, charisma=charisma,
## dexterity=dexterity, constitution=constitution,
## level=level, *args, **kwargs)
## self.current_xp = current_xp
## self.max_xp = max_xp
## self.inventory = list()
## self.paths = dict()
def __init__(self, *args, **kwargs) -> None:
super().__init__(*args, **kwargs)
validkeys = {"current_xp" : 0,"max_xp" : 0}
for dictionary in args :
for key in validkeys :
if key in dictionary :
self.__setattr__(key, dictionary[key])
else :
self.__setattr__(key, validkeys[key])
for key in validkeys:
if key in kwargs :
self.__setattr__(key, kwargs[key])
else :
self.__setattr__(key, validkeys[key])
self.inventory = list()
self.paths = dict()
@ -104,3 +122,13 @@ class Player(FightingEntity):
distances[(new_y, new_x)] = distances[(y, x)] + 1
queue.append((new_y, new_x))
self.paths = predecessors
def save_state(self) -> dict:
"""
Saves the state of the entity into a dictionary
"""
d = super().save_state()
d["type"] = "Player"
d["current_xp"] = self.current_xp
d["max_xp"] = self.max_xp
return d