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:
parent
7ae4e47fc3
commit
657345e6f7
|
@ -37,6 +37,13 @@ class Item(Entity):
|
||||||
self.map.remove_entity(self)
|
self.map.remove_entity(self)
|
||||||
player.inventory.append(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):
|
class Heart(Item):
|
||||||
"""
|
"""
|
||||||
|
@ -55,6 +62,13 @@ class Heart(Item):
|
||||||
player.health = min(player.maxhealth, player.health + self.healing)
|
player.health = min(player.maxhealth, player.health + self.healing)
|
||||||
self.map.remove_entity(self)
|
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):
|
class Bomb(Item):
|
||||||
"""
|
"""
|
||||||
|
@ -82,3 +96,10 @@ class Bomb(Item):
|
||||||
if abs(e.x - self.x) + abs(e.y - self.y) <= 1 and \
|
if abs(e.x - self.x) + abs(e.y - self.y) <= 1 and \
|
||||||
isinstance(e, FightingEntity):
|
isinstance(e, FightingEntity):
|
||||||
e.take_damage(self, self.damage)
|
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"
|
||||||
|
|
|
@ -81,6 +81,13 @@ class Rabbit(Monster):
|
||||||
super().__init__(name="rabbit", strength=strength,
|
super().__init__(name="rabbit", strength=strength,
|
||||||
maxhealth=maxhealth, *args, **kwargs)
|
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):
|
class TeddyBear(Monster):
|
||||||
"""
|
"""
|
||||||
|
@ -90,3 +97,10 @@ class TeddyBear(Monster):
|
||||||
*args, **kwargs) -> None:
|
*args, **kwargs) -> None:
|
||||||
super().__init__(name="teddy_bear", strength=strength,
|
super().__init__(name="teddy_bear", strength=strength,
|
||||||
maxhealth=maxhealth, *args, **kwargs)
|
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"
|
||||||
|
|
|
@ -13,16 +13,34 @@ class Player(FightingEntity):
|
||||||
inventory: list
|
inventory: list
|
||||||
paths: Dict[Tuple[int, int], Tuple[int, int]]
|
paths: Dict[Tuple[int, int], Tuple[int, int]]
|
||||||
|
|
||||||
def __init__(self, maxhealth: int = 20, strength: int = 5,
|
## def __init__(self, maxhealth: int = 20, strength: int = 5,
|
||||||
intelligence: int = 1, charisma: int = 1, dexterity: int = 1,
|
## intelligence: int = 1, charisma: int = 1, dexterity: int = 1,
|
||||||
constitution: int = 1, level: int = 1, current_xp: int = 0,
|
## constitution: int = 1, level: int = 1, current_xp: int = 0,
|
||||||
max_xp: int = 10, *args, **kwargs) -> None:
|
## max_xp: int = 10, *args, **kwargs) -> None:
|
||||||
super().__init__(name="player", maxhealth=maxhealth, strength=strength,
|
## super().__init__(name="player", maxhealth=maxhealth, strength=strength,
|
||||||
intelligence=intelligence, charisma=charisma,
|
## intelligence=intelligence, charisma=charisma,
|
||||||
dexterity=dexterity, constitution=constitution,
|
## dexterity=dexterity, constitution=constitution,
|
||||||
level=level, *args, **kwargs)
|
## level=level, *args, **kwargs)
|
||||||
self.current_xp = current_xp
|
## self.current_xp = current_xp
|
||||||
self.max_xp = max_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.inventory = list()
|
||||||
self.paths = dict()
|
self.paths = dict()
|
||||||
|
|
||||||
|
@ -104,3 +122,13 @@ class Player(FightingEntity):
|
||||||
distances[(new_y, new_x)] = distances[(y, x)] + 1
|
distances[(new_y, new_x)] = distances[(y, x)] + 1
|
||||||
queue.append((new_y, new_x))
|
queue.append((new_y, new_x))
|
||||||
self.paths = predecessors
|
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
|
||||||
|
|
|
@ -130,8 +130,9 @@ class Map:
|
||||||
d["start_x"] = self.start_x
|
d["start_x"] = self.start_x
|
||||||
d["currentx"] = self.currentx
|
d["currentx"] = self.currentx
|
||||||
d["currenty"] = self.currenty
|
d["currenty"] = self.currenty
|
||||||
|
d["entities"] = []
|
||||||
for enti in self.entities:
|
for enti in self.entities:
|
||||||
d.update(enti.save_state())
|
d.append(enti.save_state())
|
||||||
d["map"] = self.draw_string(TexturePack.ASCII_PACK)
|
d["map"] = self.draw_string(TexturePack.ASCII_PACK)
|
||||||
return d
|
return d
|
||||||
|
|
||||||
|
@ -146,7 +147,10 @@ class Map:
|
||||||
self.currentx = d["currentx"]
|
self.currentx = d["currentx"]
|
||||||
self.currenty = d["currenty"]
|
self.currenty = d["currenty"]
|
||||||
self.tiles = self.load_dungeon_from_string(d["map"])
|
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):
|
class Tile(Enum):
|
||||||
|
@ -196,13 +200,27 @@ class Entity:
|
||||||
name: str
|
name: str
|
||||||
map: Map
|
map: Map
|
||||||
|
|
||||||
# noinspection PyShadowingBuiltins
|
## # noinspection PyShadowingBuiltins
|
||||||
def __init__(self, y: int = 0, x: int = 0, name: Optional[str] = None,
|
## def __init__(self, y: int = 0, x: int = 0, name: Optional[str] = None,
|
||||||
map: Optional[Map] = None):
|
## map: Optional[Map] = None):
|
||||||
self.y = y
|
## self.y = y
|
||||||
self.x = x
|
## self.x = x
|
||||||
self.name = name
|
## self.name = name
|
||||||
self.map = map
|
## 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)\
|
def check_move(self, y: int, x: int, move_if_possible: bool = False)\
|
||||||
-> bool:
|
-> bool:
|
||||||
|
@ -293,6 +311,14 @@ class Entity:
|
||||||
Rabbit, TeddyBear
|
Rabbit, TeddyBear
|
||||||
return [Beaver, Bomb, Heart, Hedgehog, 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:
|
def save_state(self) -> dict:
|
||||||
"""
|
"""
|
||||||
Saves the coordinates of the entity
|
Saves the coordinates of the entity
|
||||||
|
@ -302,13 +328,6 @@ class Entity:
|
||||||
d["y"] = self.y
|
d["y"] = self.y
|
||||||
return d
|
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):
|
class FightingEntity(Entity):
|
||||||
"""
|
"""
|
||||||
|
@ -324,19 +343,36 @@ class FightingEntity(Entity):
|
||||||
constitution: int
|
constitution: int
|
||||||
level: int
|
level: int
|
||||||
|
|
||||||
def __init__(self, maxhealth: int = 0, health: Optional[int] = None,
|
## def __init__(self, maxhealth: int = 0, health: Optional[int] = None,
|
||||||
strength: int = 0, intelligence: int = 0, charisma: int = 0,
|
## strength: int = 0, intelligence: int = 0, charisma: int = 0,
|
||||||
dexterity: int = 0, constitution: int = 0, level: int = 0,
|
## dexterity: int = 0, constitution: int = 0, level: int = 0,
|
||||||
*args, **kwargs) -> None:
|
## *args, **kwargs) -> None:
|
||||||
super().__init__(*args, **kwargs)
|
## super().__init__(*args, **kwargs)
|
||||||
self.maxhealth = maxhealth
|
## self.maxhealth = maxhealth
|
||||||
self.health = maxhealth if health is None else health
|
## self.health = maxhealth if health is None else health
|
||||||
self.strength = strength
|
## self.strength = strength
|
||||||
self.intelligence = intelligence
|
## self.intelligence = intelligence
|
||||||
self.charisma = charisma
|
## self.charisma = charisma
|
||||||
self.dexterity = dexterity
|
## self.dexterity = dexterity
|
||||||
self.constitution = constitution
|
## self.constitution = constitution
|
||||||
self.level = level
|
## 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
|
@property
|
||||||
def dead(self) -> bool:
|
def dead(self) -> bool:
|
||||||
|
@ -377,11 +413,3 @@ class FightingEntity(Entity):
|
||||||
for name in self.keys():
|
for name in self.keys():
|
||||||
d[name] = self.__getattribute__(name)
|
d[name] = self.__getattribute__(name)
|
||||||
return d
|
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])
|
|
||||||
|
|
|
@ -1 +1 @@
|
||||||
{"width": 80, "height": 40, "start_y": 1, "start_x": 17, "currentx": 29, "currenty": 25, "x": 74, "y": 22, "maxhealth": 50, "health": 0, "level": 1, "dead": false, "strength": 0, "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": 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 ################ ############ "}
|
||||||
|
|
Loading…
Reference in New Issue