diff --git a/dungeonbattle/entities/items.py b/dungeonbattle/entities/items.py index 442e00b..217514d 100644 --- a/dungeonbattle/entities/items.py +++ b/dungeonbattle/entities/items.py @@ -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" diff --git a/dungeonbattle/entities/monsters.py b/dungeonbattle/entities/monsters.py index 76fdb7a..1f04372 100644 --- a/dungeonbattle/entities/monsters.py +++ b/dungeonbattle/entities/monsters.py @@ -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" diff --git a/dungeonbattle/entities/player.py b/dungeonbattle/entities/player.py index 0b84135..873da32 100644 --- a/dungeonbattle/entities/player.py +++ b/dungeonbattle/entities/player.py @@ -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 diff --git a/dungeonbattle/interfaces.py b/dungeonbattle/interfaces.py index ae87700..20cedde 100644 --- a/dungeonbattle/interfaces.py +++ b/dungeonbattle/interfaces.py @@ -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 diff --git a/save.json b/save.json index 067eac1..e0ac667 100644 --- a/save.json +++ b/save.json @@ -1 +1 @@ -{"width": 80, "height": 40, "start_y": 1, "start_x": 17, "currentx": 54, "currenty": 38, "x": 56, "y": 19, "maxhealth": 15, "health": 15, "level": 0, "dead": false, "strength": 1, "intelligence": 0, "charisma": 0, "dexterity": 0, "constitution": 0, "map": " ########### ######### \n #.........# #.......# \n #.........# ############.......# \n #.........###############..........#.......############## \n #.........#........................#....................# \n #.........#.............#..........#.......#............# \n ########.########.............#..................#............# \n #.........# #.............####.#######.......#............# \n #.........# #.............##.........###################### \n #.........# #####.##########.........# ########### \n #.........# #......# #.........# #.........# \n ########.##########......# #.........# #.........# \n #...........##......# #.........# #.........# \n #...........##......# #.........# #.........# \n #...........##......# #.........# ################.###### \n #...........##......# #.........# #.................############\n #...........##......# ########.########.......#.........#..........#\n #...........##......# #...............#.......#.........#..........#\n #...........######### #...............#.......#.........#..........#\n #...........# #...............#.......#....................#\n #####.####### #.......................#.........#..........#\n #.........# #...............###################..........#\n #.........############ #...............# #..........#\n #.........#..........# #...............# ############\n #....................#####.###########.############# \n ########.#########...................# #.............# \n #........# #..........#........# #.............######### \n #........# ######.##########........# #.............#.......# \n #........# #..........# #........# #.....................# \n #........# #..........# #........# #.............#.......# \n #........# #..........# #........# #.............#.......# \n #........# #..........# #........# #.............#.......# \n #........# #..........#########.##### #.............#.......# \n #........# #..........#.........# ##########.############.####### \n #........# #..........#.........# #..............# #..........# \n ########## #..........#.........# #..............# #..........# \n ############.........# #..............# #..........# \n #.........# #..............# #..........# \n ########### #..............# #..........# \n ################ ############ "} +{"width": 80, "height": 40, "start_y": 1, "start_x": 17, "currentx": 14, "currenty": 11, "entities": [{"x": 14, "y": 11, "type": "Player", "maxhealth": 20, "health": 20, "level": 1, "strength": 5, "intelligence": 1, "charisma": 1, "dexterity": 1, "constitution": 1, "current_xp": 0, "max_xp": 10}, {"x": 50, "y": 37, "type": "Rabbit", "maxhealth": 15, "health": 15, "level": 0, "strength": 1, "intelligence": 0, "charisma": 0, "dexterity": 0, "constitution": 0}, {"x": 16, "y": 22, "type": "Rabbit", "maxhealth": 15, "health": 15, "level": 0, "strength": 1, "intelligence": 0, "charisma": 0, "dexterity": 0, "constitution": 0}, {"x": 12, "y": 7, "type": "Bomb", "held": false}, {"x": 69, "y": 38, "type": "Hedgehog", "maxhealth": 10, "health": 10, "level": 0, "strength": 3, "intelligence": 0, "charisma": 0, "dexterity": 0, "constitution": 0}, {"x": 64, "y": 28, "type": "Hedgehog", "maxhealth": 10, "health": 10, "level": 0, "strength": 3, "intelligence": 0, "charisma": 0, "dexterity": 0, "constitution": 0}, {"x": 37, "y": 29, "type": "TeddyBear", "maxhealth": 50, "health": 50, "level": 0, "strength": 0, "intelligence": 0, "charisma": 0, "dexterity": 0, "constitution": 0}, {"x": 16, "y": 17, "type": "Rabbit", "maxhealth": 15, "health": 15, "level": 0, "strength": 1, "intelligence": 0, "charisma": 0, "dexterity": 0, "constitution": 0}, {"x": 39, "y": 22, "type": "Rabbit", "maxhealth": 15, "health": 15, "level": 0, "strength": 1, "intelligence": 0, "charisma": 0, "dexterity": 0, "constitution": 0}, {"x": 35, "y": 28, "type": "Heart", "held": false}], "map": " ########### ######### \n #.........# #.......# \n #.........# ############.......# \n #.........###############..........#.......############## \n #.........#........................#....................# \n #.........#.............#..........#.......#............# \n ########.########.............#..................#............# \n #.........# #.............####.#######.......#............# \n #.........# #.............##.........###################### \n #.........# #####.##########.........# ########### \n #.........# #......# #.........# #.........# \n ########.##########......# #.........# #.........# \n #...........##......# #.........# #.........# \n #...........##......# #.........# #.........# \n #...........##......# #.........# ################.###### \n #...........##......# #.........# #.................############\n #...........##......# ########.########.......#.........#..........#\n #...........##......# #...............#.......#.........#..........#\n #...........######### #...............#.......#.........#..........#\n #...........# #...............#.......#....................#\n #####.####### #.......................#.........#..........#\n #.........# #...............###################..........#\n #.........############ #...............# #..........#\n #.........#..........# #...............# ############\n #....................#####.###########.############# \n ########.#########...................# #.............# \n #........# #..........#........# #.............######### \n #........# ######.##########........# #.............#.......# \n #........# #..........# #........# #.....................# \n #........# #..........# #........# #.............#.......# \n #........# #..........# #........# #.............#.......# \n #........# #..........# #........# #.............#.......# \n #........# #..........#########.##### #.............#.......# \n #........# #..........#.........# ##########.############.####### \n #........# #..........#.........# #..............# #..........# \n ########## #..........#.........# #..............# #..........# \n ############.........# #..............# #..........# \n #.........# #..............# #..........# \n ########### #..............# #..........# \n ################ ############ "} \ No newline at end of file