import json import os from datetime import datetime from settings import * class SaveSystem: """Handle game saving and loading""" SAVE_DIR = "saves" BACKUP_DIR = "saves/backups" def __init__(self): # Create directories if they don't exist os.makedirs(self.SAVE_DIR, exist_ok=True) os.makedirs(self.BACKUP_DIR, exist_ok=True) def save_game(self, game, world_name="Default"): """Save game state to file with world name""" try: # Create filename with world name and timestamp timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") filename = f"save_{world_name}_{timestamp}.json" save_data = { "world_name": world_name, "player": { "x": game.player.pos.x, "y": game.player.pos.y, "health": game.player.health, "velocity_x": game.player.velocity.x, "velocity_y": game.player.velocity.y, }, "world": { "grid": game.world.grid, "width": game.world.width, "height": game.world.height, }, "inventory": { "slots": game.inventory.slots, }, "timestamp": datetime.now().isoformat(), } filepath = os.path.join(self.SAVE_DIR, filename) with open(filepath, 'w') as f: json.dump(save_data, f, indent=4) print(f"Game '{world_name}' saved to {filepath}") return True, filename except Exception as e: print(f"Error saving game: {e}") return False, None def load_game(self, game, save_file): """Load game state from file""" try: filepath = os.path.join(self.SAVE_DIR, save_file) if not os.path.exists(filepath): print(f"Save file not found: {filepath}") return False with open(filepath, 'r') as f: save_data = json.load(f) # Store world name in game for future saves if "world_name" in save_data: game.world_name = save_data["world_name"] # Load player data game.player.pos.x = save_data["player"]["x"] game.player.pos.y = save_data["player"]["y"] game.player.rect.x = int(game.player.pos.x) game.player.rect.y = int(game.player.pos.y) game.player.health = save_data["player"]["health"] game.player.velocity.x = save_data["player"]["velocity_x"] game.player.velocity.y = save_data["player"]["velocity_y"] # Load world data game.world.grid = save_data["world"]["grid"] # Load inventory game.inventory.slots = save_data["inventory"]["slots"] print(f"Game '{game.world_name}' loaded from {filepath}") return True except Exception as e: print(f"Error loading game: {e}") return False def backup_game(self, game): """Create a backup of current save""" try: timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") backup_filename = f"backup_{game.world_name}_{timestamp}.json" save_data = { "world_name": game.world_name, "player": { "x": game.player.pos.x, "y": game.player.pos.y, "health": game.player.health, "velocity_x": game.player.velocity.x, "velocity_y": game.player.velocity.y, }, "world": { "grid": game.world.grid, "width": game.world.width, "height": game.world.height, }, "inventory": { "slots": game.inventory.slots, }, "timestamp": datetime.now().isoformat(), } filepath = os.path.join(self.BACKUP_DIR, backup_filename) with open(filepath, 'w') as f: json.dump(save_data, f, indent=4) print(f"Backup created: {filepath}") return True except Exception as e: print(f"Error creating backup: {e}") return False def get_save_files(self): """Get list of all save files""" try: files = [] for filename in os.listdir(self.SAVE_DIR): if filename.endswith(".json") and filename.startswith("save_"): filepath = os.path.join(self.SAVE_DIR, filename) files.append({ "name": filename, "path": filepath, "modified": os.path.getmtime(filepath) }) return sorted(files, key=lambda x: x["modified"], reverse=True) except Exception as e: print(f"Error reading save files: {e}") return [] def get_backups(self): """Get list of all backup files""" try: files = [] for filename in os.listdir(self.BACKUP_DIR): if filename.endswith(".json"): filepath = os.path.join(self.BACKUP_DIR, filename) files.append({ "name": filename, "path": filepath, "modified": os.path.getmtime(filepath) }) return sorted(files, key=lambda x: x["modified"], reverse=True) except Exception as e: print(f"Error reading backups: {e}") return [] def delete_save(self, save_file): """Delete a save file""" try: filepath = os.path.join(self.SAVE_DIR, save_file) if os.path.exists(filepath): os.remove(filepath) print(f"Save deleted: {filepath}") return True return False except Exception as e: print(f"Error deleting save: {e}") return False