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
committed by Yohann D'ANELLO
parent 57a20c53f3
commit a4482849ae
5 changed files with 139 additions and 48 deletions

View File

@ -37,6 +37,13 @@ class Item(Entity):
self.map.remove_entity(self)
player.inventory.append(self)
def save_state(self) -> None:
"""
Saves the state of the entity into a dictionary
"""
d = super().save_state()
d["held"] = self.held
class Heart(Item):
"""
@ -55,6 +62,13 @@ class Heart(Item):
player.health = min(player.maxhealth, player.health + self.healing)
self.map.remove_entity(self)
def save_state(self) -> None:
"""
Saves the state of the entity into a dictionary
"""
d = super().save_state()
d["type"] = "Heart"
class Bomb(Item):
"""
@ -82,3 +96,10 @@ class Bomb(Item):
if abs(e.x - self.x) + abs(e.y - self.y) <= 1 and \
isinstance(e, FightingEntity):
e.take_damage(self, self.damage)
def save_state(self) -> None:
"""
Saves the state of the entity into a dictionary
"""
d = super().save_state()
d["type"] = "Bomb"

View File

@ -81,6 +81,13 @@ class Rabbit(Monster):
super().__init__(name="rabbit", strength=strength,
maxhealth=maxhealth, *args, **kwargs)
def save_state(self) -> None:
"""
Saves the state of the entity into a dictionary
"""
d = super().save_state()
d["type"] = "Rabbit"
class TeddyBear(Monster):
"""
@ -90,3 +97,10 @@ class TeddyBear(Monster):
*args, **kwargs) -> None:
super().__init__(name="teddy_bear", strength=strength,
maxhealth=maxhealth, *args, **kwargs)
def save_state(self) -> None:
"""
Saves the state of the entity into a dictionary
"""
d = super().save_state()
d["type"] = "Teddy"

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