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

@ -130,8 +130,9 @@ class Map:
d["start_x"] = self.start_x
d["currentx"] = self.currentx
d["currenty"] = self.currenty
d["entities"] = []
for enti in self.entities:
d.update(enti.save_state())
d.append(enti.save_state())
d["map"] = self.draw_string(TexturePack.ASCII_PACK)
return d
@ -146,7 +147,10 @@ class Map:
self.currentx = d["currentx"]
self.currenty = d["currenty"]
self.tiles = self.load_dungeon_from_string(d["map"])
# add entities
self.entities = []
dictclasses = get_all_entity_classes_in_a_dict()
for entisave in d["entities"] :
self.add_entity(dictclasses[entisave["type"]](entisave))
class Tile(Enum):
@ -196,13 +200,27 @@ class Entity:
name: str
map: Map
# noinspection PyShadowingBuiltins
def __init__(self, y: int = 0, x: int = 0, name: Optional[str] = None,
map: Optional[Map] = None):
self.y = y
self.x = x
self.name = name
self.map = map
## # noinspection PyShadowingBuiltins
## def __init__(self, y: int = 0, x: int = 0, name: Optional[str] = None,
## map: Optional[Map] = None):
## self.y = y
## self.x = x
## self.name = name
## self.map = map
def __init__(self, dictionary, **kwargs) -> None:
validkeys = self.attributes()
for key in validkeys :
self.__setattr__(key, dictionary[key])
for key in validkeys:
self.__setattr__(key, kwargs[key])
@staticmethod
def attributes(self) -> list:
"""
Returns the list of attributes
"""
return ["x", "y", "name"]
def check_move(self, y: int, x: int, move_if_possible: bool = False)\
-> bool:
@ -293,6 +311,14 @@ class Entity:
Rabbit, TeddyBear
return [Beaver, Bomb, Heart, Hedgehog, Rabbit, TeddyBear]
@staticmethod
def get_all_entity_classes_in_a_dict() -> dict:
"""
Returns all entities subclasses in a dictionnary
"""
from dungeonbattle.entities.player import Player
return {"Beaver" : Beaver, "Bomb" : Bomb, "Heart" : Heart, "Hedgehog" : Hedgehog, "Rabbit" : Rabbit, "Teddy" : TeddyBear, "Player" : Player}
def save_state(self) -> dict:
"""
Saves the coordinates of the entity
@ -302,13 +328,6 @@ class Entity:
d["y"] = self.y
return d
def recover_state(self, d: dict) -> None:
"""
Loads the coordinates of the entity from a dictionnary
"""
self.x = d["x"]
self.y = d["y"]
class FightingEntity(Entity):
"""
@ -324,19 +343,36 @@ class FightingEntity(Entity):
constitution: int
level: int
def __init__(self, maxhealth: int = 0, health: Optional[int] = None,
strength: int = 0, intelligence: int = 0, charisma: int = 0,
dexterity: int = 0, constitution: int = 0, level: int = 0,
*args, **kwargs) -> None:
super().__init__(*args, **kwargs)
self.maxhealth = maxhealth
self.health = maxhealth if health is None else health
self.strength = strength
self.intelligence = intelligence
self.charisma = charisma
self.dexterity = dexterity
self.constitution = constitution
self.level = level
## def __init__(self, maxhealth: int = 0, health: Optional[int] = None,
## strength: int = 0, intelligence: int = 0, charisma: int = 0,
## dexterity: int = 0, constitution: int = 0, level: int = 0,
## *args, **kwargs) -> None:
## super().__init__(*args, **kwargs)
## self.maxhealth = maxhealth
## self.health = maxhealth if health is None else health
## self.strength = strength
## self.intelligence = intelligence
## self.charisma = charisma
## self.dexterity = dexterity
## self.constitution = constitution
## self.level = level
def __init__(self, *args, **kwargs) -> None:
validkeys = {"maxhealth" : 0,"health" : 0,"strength" : 0 \
,"intelligence" : 0,"charisma" : 0,"dexterity" : 0\
,"constitution" : 0,"level" : 0}
#All the keys we wan to set in this init, with their default value
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])
@property
def dead(self) -> bool:
@ -377,11 +413,3 @@ class FightingEntity(Entity):
for name in self.keys():
d[name] = self.__getattribute__(name)
return d
def recover_state(self, d: dict) -> None:
"""
Loads the state of an entity from a dictionary
"""
super().recover_state(d)
for name in d.keys():
setattr(self, name, d[name])