init commit, working save game
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
from classes.SavedState import SavedState
|
||||
import my_helpers.myPickle as my_pickle
|
||||
|
||||
import os
|
||||
|
||||
class GameBoard:
|
||||
def __init__(self, config:dict, save_state_file:dict, map_file:dict):
|
||||
self.config = config
|
||||
self.save_state_file = save_state_file
|
||||
self.map_file = map_file
|
||||
|
||||
def create_character(self, player_config:dict):
|
||||
name = input("Enter Character Name: ")
|
||||
|
||||
species = list(player_config["species"].keys())
|
||||
print(species)
|
||||
|
||||
types = [ x["name"] for x in player_config["types"] ]
|
||||
print(types)
|
||||
|
||||
species_selected = player_config["species"]["Gnoll"]
|
||||
species_selected["name"] = "Gnoll"
|
||||
|
||||
return {
|
||||
"name": name,
|
||||
"species": species_selected,
|
||||
"type": types
|
||||
}
|
||||
|
||||
def find_save_state(self):
|
||||
if os.path.exists(self.save_state_file['path']):
|
||||
saved_state:SavedState = self.load_from_file(self.save_state_file)
|
||||
else:
|
||||
saved_state = SavedState()
|
||||
player = self.create_character(self.config["player"])
|
||||
saved_state.add_player(player)
|
||||
self.save_to_file(saved_state, self.save_state_file)
|
||||
|
||||
self.map = self.load_from_file(self.map_file)
|
||||
self.saved_state = saved_state
|
||||
return saved_state
|
||||
|
||||
def load_from_file(self, file_options):
|
||||
if os.path.exists(file_options["path"]):
|
||||
with open(file_options["path"], "rb") as f:
|
||||
data = my_pickle.load(f, file_options['compression'], None)
|
||||
|
||||
return data
|
||||
else:
|
||||
raise FileNotFoundError(f"Could not load map file: {file_options['path']}")
|
||||
|
||||
def save_to_file(self, obj, file_options):
|
||||
os.makedirs(os.path.dirname(file_options["path"]), exist_ok=True)
|
||||
with open(file_options["path"], "wb") as f:
|
||||
my_pickle.dump(obj, f, file_options['compression'], None)
|
||||
|
||||
def get_world(self):
|
||||
return self.map[self.saved_state.story_position]["World"][self.saved_state.story_world_index]
|
||||
@@ -0,0 +1,22 @@
|
||||
from json import load as json_load
|
||||
import os
|
||||
|
||||
class Map:
|
||||
def __init__(self, path):
|
||||
self.path = path
|
||||
self.story = {}
|
||||
|
||||
def make_file(self, story:dict={}):
|
||||
for file in os.listdir(self.path):
|
||||
if file.endswith("json"):
|
||||
with open(f"{self.path}/{file}", "r") as f:
|
||||
filename = os.path.splitext(os.path.basename(file))[0]
|
||||
story[filename] = json_load(f)
|
||||
|
||||
self.story = story
|
||||
|
||||
def __str__(self):
|
||||
return f"{self.__class__.__name__} | Story: {self.story}"
|
||||
|
||||
def __getitem__(self, key):
|
||||
return self.story[key]
|
||||
@@ -0,0 +1,16 @@
|
||||
class Player:
|
||||
def __init__(self, name:str, type:dict, species:dict):
|
||||
self.name = name
|
||||
self.type = type
|
||||
self.species = species
|
||||
self.inventory = []
|
||||
self.inventory_size = species["inventory_size"]
|
||||
|
||||
def get_inventory(self):
|
||||
return self.inventory
|
||||
|
||||
def attack(self):
|
||||
pass
|
||||
|
||||
def __str__(self):
|
||||
return f"{self.__class__.__name__} | Name :{self.name}\nType :{self.type}\nSpecies :{self.species}\nInventory: {self.inventory}"
|
||||
@@ -0,0 +1,19 @@
|
||||
from classes.Player import Player
|
||||
|
||||
from classes.MapObject.Home import Home
|
||||
|
||||
class SavedState:
|
||||
def __init__(self, story_position:str="The Beginning"):
|
||||
self.home = Home()
|
||||
|
||||
self.story_position = story_position
|
||||
self.story_world_index = 0
|
||||
|
||||
def add_player(self, player:dict):
|
||||
self.player = Player(player["name"], player["type"], player["species"])
|
||||
|
||||
def get_home(self):
|
||||
return self.home
|
||||
|
||||
def get_player(self):
|
||||
return self.player
|
||||
Reference in New Issue
Block a user