Added functionnal save system and broken load system

This commit is contained in:
eichhornchen
2020-11-16 01:01:18 +01:00
committed by Yohann D'ANELLO
parent d95747159f
commit 41d1696c9b
8 changed files with 202 additions and 9 deletions

View File

@ -78,6 +78,17 @@ class Map:
return Map(width, height, tiles, start_y, start_x)
@staticmethod
def load_dungeon_from_string(content: str) -> "Map":
"""
Transforms a string into the list of corresponding tiles
"""
lines = content.split("\n")
lines = [line for line in lines[1:] if line]
tiles = [[Tile.from_ascii_char(c)
for x, c in enumerate(line)] for y, line in enumerate(lines)]
return tiles
def draw_string(self, pack: TexturePack) -> str:
"""
Draw the current map as a string object that can be rendered
@ -108,14 +119,39 @@ class Map:
for entity in self.entities:
entity.act(self)
def save_state(self) -> dict:
d = dict()
d["width"] = self.width
d["height"] = self.height
d["start_y"] = self.start_y
d["start_x"] = self.start_x
d["currentx"] = self.currentx
d["currenty"] = self.currenty
for enti in self.entities:
d.update(enti.save_state())
d["map"] = self.draw_string(TexturePack.ASCII_PACK)
return d
def load_state(self, d: dict) -> None:
self.width = d["width"]
self.height = d["height"]
self.start_y = d["start_y"]
self.start_x = d["start_x"]
self.currentx = d["currentx"]
self.currenty = d["currenty"]
self.map = self.load_dungeon_from_string(d["map"])
#add entities
class Tile(Enum):
EMPTY = auto()
WALL = auto()
FLOOR = auto()
@classmethod
def from_ascii_char(cls, ch: str) -> "Tile":
@staticmethod
def from_ascii_char(ch: str) -> "Tile":
"""
Maps an ascii character to its equivalent in the texture pack
"""
for tile in Tile:
if tile.char(TexturePack.ASCII_PACK) == ch:
return tile
@ -206,6 +242,22 @@ class Entity:
Rabbit, TeddyBear
return [Beaver, Bomb, Heart, Hedgehog, Rabbit, TeddyBear]
def save_state(self) -> dict:
"""
Saves the coordinates of the entity
"""
d = dict()
d["x"] = self.x
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):
maxhealth: int
@ -222,6 +274,15 @@ class FightingEntity(Entity):
super().__init__()
self.health = self.maxhealth
self.dead = False
self.health = 0
self.strength = 0
self.dead = False
self.intelligence = 0
self.charisma = 0
self.dexterity = 0
self.constitution = 0
self.level = 1
def hit(self, opponent: "FightingEntity") -> None:
opponent.take_damage(self, self.strength)
@ -234,3 +295,17 @@ class FightingEntity(Entity):
def die(self) -> None:
self.dead = True
self.map.remove_entity(self)
def keys(self) -> list:
return ["maxhealth", "health", "level", "dead", "strength", "intelligence", "charisma", "dexterity", "constitution"]
def save_state(self) -> dict:
d = super().save_state()
for name in self.keys():
d[name] = self.__getattribute__(name)
return d
def recover_state(self, d : dict) -> None:
super().recover_state(d)
for name in d.keys():
self.__setattribute__(name, d[name])