Save entities

This commit is contained in:
Yohann D'ANELLO
2020-11-19 00:10:37 +01:00
parent 657345e6f7
commit 81b20b72bc
5 changed files with 56 additions and 119 deletions

View File

@ -37,12 +37,13 @@ class Item(Entity):
self.map.remove_entity(self)
player.inventory.append(self)
def save_state(self) -> None:
def save_state(self) -> dict:
"""
Saves the state of the entity into a dictionary
"""
d = super().save_state()
d["held"] = self.held
return d
class Heart(Item):
@ -62,13 +63,6 @@ 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):
"""
@ -96,10 +90,3 @@ 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,13 +81,6 @@ 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):
"""
@ -97,10 +90,3 @@ 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,34 +13,16 @@ 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
## 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])
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()
@ -122,13 +104,12 @@ 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