Save entities

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

View File

@ -132,7 +132,9 @@ class Map:
d["currenty"] = self.currenty
d["entities"] = []
for enti in self.entities:
d.append(enti.save_state())
if enti.save_state() is None:
raise Exception(enti)
d["entities"].append(enti.save_state())
d["map"] = self.draw_string(TexturePack.ASCII_PACK)
return d
@ -148,9 +150,9 @@ class Map:
self.currenty = d["currenty"]
self.tiles = self.load_dungeon_from_string(d["map"])
self.entities = []
dictclasses = get_all_entity_classes_in_a_dict()
for entisave in d["entities"] :
self.add_entity(dictclasses[entisave["type"]](entisave))
dictclasses = Entity.get_all_entity_classes_in_a_dict()
for entisave in d["entities"]:
self.add_entity(dictclasses[entisave["type"]](**entisave))
class Tile(Enum):
@ -200,27 +202,13 @@ 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
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"]
# noinspection PyShadowingBuiltins
def __init__(self, y: int = 0, x: int = 0, name: Optional[str] = None,
map: Optional[Map] = None, *ignored, **ignored2):
self.y = y
self.x = x
self.name = name
self.map = map
def check_move(self, y: int, x: int, move_if_possible: bool = False)\
-> bool:
@ -314,10 +302,21 @@ class Entity:
@staticmethod
def get_all_entity_classes_in_a_dict() -> dict:
"""
Returns all entities subclasses in a dictionnary
Returns all entities subclasses in a dictionary
"""
from dungeonbattle.entities.player import Player
return {"Beaver" : Beaver, "Bomb" : Bomb, "Heart" : Heart, "Hedgehog" : Hedgehog, "Rabbit" : Rabbit, "Teddy" : TeddyBear, "Player" : Player}
from dungeonbattle.entities.monsters import Beaver, Hedgehog, Rabbit, \
TeddyBear
from dungeonbattle.entities.items import Bomb, Heart
return {
"Beaver": Beaver,
"Bomb": Bomb,
"Heart": Heart,
"Hedgehog": Hedgehog,
"Rabbit": Rabbit,
"TeddyBear": TeddyBear,
"Player": Player,
}
def save_state(self) -> dict:
"""
@ -326,6 +325,7 @@ class Entity:
d = dict()
d["x"] = self.x
d["y"] = self.y
d["type"] = self.__class__.__name__
return d
@ -343,36 +343,19 @@ 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, *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])
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
@property
def dead(self) -> bool:
@ -402,7 +385,7 @@ class FightingEntity(Entity):
"""
Returns a fighting entities specific attributes
"""
return ["maxhealth", "health", "level", "dead", "strength",
return ["maxhealth", "health", "level", "strength",
"intelligence", "charisma", "dexterity", "constitution"]
def save_state(self) -> dict:
@ -411,5 +394,5 @@ class FightingEntity(Entity):
"""
d = super().save_state()
for name in self.keys():
d[name] = self.__getattribute__(name)
d[name] = getattr(self, name)
return d