Save floors and visibility, fixes #61

This commit is contained in:
Yohann D'ANELLO 2021-01-08 14:23:57 +01:00
parent 073e3d3740
commit f48377e055
Signed by: ynerant
GPG Key ID: 3A75C55819C8CF85
2 changed files with 19 additions and 9 deletions

View File

@ -335,14 +335,16 @@ class Game:
""" """
Saves the game to a dictionary. Saves the game to a dictionary.
""" """
return self.map.save_state() return dict(map_index=self.map_index,
maps=[m.save_state() for m in self.maps])
def load_state(self, d: dict) -> None: def load_state(self, d: dict) -> None:
""" """
Loads the game from a dictionary. Loads the game from a dictionary.
""" """
try: try:
self.map.load_state(d) self.map_index = d["map_index"]
self.maps = [Map().load_state(map_dict) for map_dict in d["maps"]]
except KeyError: except KeyError:
self.message = _("Some keys are missing in your save file.\n" self.message = _("Some keys are missing in your save file.\n"
"Your save seems to be corrupt. It got deleted.") "Your save seems to be corrupt. It got deleted.")
@ -359,6 +361,8 @@ class Game:
return return
self.player = players[0] self.player = players[0]
self.map.compute_visibility(self.player.y, self.player.x,
self.player.vision)
self.display_actions(DisplayActions.UPDATE) self.display_actions(DisplayActions.UPDATE)
def load_game(self) -> None: def load_game(self) -> None:

View File

@ -80,18 +80,18 @@ class Map:
currentx: int currentx: int
currenty: int currenty: int
def __init__(self, width: int, height: int, tiles: list, def __init__(self, width: int = 0, height: int = 0, tiles: list = None,
start_y: int, start_x: int): start_y: int = 0, start_x: int = 0):
self.floor = 0 self.floor = 0
self.width = width self.width = width
self.height = height self.height = height
self.start_y = start_y self.start_y = start_y
self.start_x = start_x self.start_x = start_x
self.tiles = tiles self.tiles = tiles or []
self.visibility = [[False for _ in range(len(tiles[0]))] self.visibility = [[False for _ in range(len(self.tiles[0]))]
for _ in range(len(tiles))] for _ in range(len(self.tiles))]
self.seen_tiles = [[False for _ in range(len(tiles[0]))] self.seen_tiles = [[False for _ in range(len(tiles[0]))]
for _ in range(len(tiles))] for _ in range(len(self.tiles))]
self.entities = [] self.entities = []
self.logs = Logs() self.logs = Logs()
@ -338,9 +338,10 @@ class Map:
for enti in self.entities: for enti in self.entities:
d["entities"].append(enti.save_state()) d["entities"].append(enti.save_state())
d["map"] = self.draw_string(TexturePack.ASCII_PACK) d["map"] = self.draw_string(TexturePack.ASCII_PACK)
d["seen_tiles"] = self.seen_tiles
return d return d
def load_state(self, d: dict) -> None: def load_state(self, d: dict) -> "Map":
""" """
Loads the map's attributes from a dictionary. Loads the map's attributes from a dictionary.
""" """
@ -351,11 +352,16 @@ 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"])
self.seen_tiles = d["seen_tiles"]
self.visibility = [[False for _ in range(len(self.tiles[0]))]
for _ in range(len(self.tiles))]
self.entities = [] self.entities = []
dictclasses = Entity.get_all_entity_classes_in_a_dict() dictclasses = Entity.get_all_entity_classes_in_a_dict()
for entisave in d["entities"]: for entisave in d["entities"]:
self.add_entity(dictclasses[entisave["type"]](**entisave)) self.add_entity(dictclasses[entisave["type"]](**entisave))
return self
class Tile(Enum): class Tile(Enum):
""" """