subsystem.py (3184B)
1 import pygame, os, json, pathlib 2 from .constants import * 3 4 ################ 5 # subsystem.py # 6 ################ 7 8 # This file contains: 9 # 1. The generic GameSubsystem class, which abstract subsystems (like GameInterface) are children of, as well as all Managers 10 # 2. The ObjectOracle class, which keeps track of all Entity objects and can refer to them, but does not manage or draw them 11 # 3. The SaveSystem class, which handles saving and loading games 12 13 ####################################### 14 # Section 1 - The GameSubsystem class # 15 ####################################### 16 17 class GameSubsystem(object): 18 """ 19 Abstract class subordinate to game which 20 Managers and other subsystems are specialized 21 versions of. GameSubsystem is pretty much the 22 most abstract possible parent for these kinds 23 of objects and is very simple. 24 """ 25 # What SHOULD be a Subsystem: 26 # * Any object that CONTROLS A CRITICAL 27 # GAME FUNCTION, IS DIRECTLY SUBORDINATE 28 # TO THE Game OBJECT, and DOES NOT 29 # MANAGE ANY ENTITIES OR RESOURCES BY 30 # ITSELF. Game Subsystems can and often 31 # do need ManagerBus access, but they don't 32 # manage any objects themselves, nor do 33 # they expose their own data to the 34 # ManagerBus. 35 36 def __init__(self, game): 37 38 self.game = game 39 40 ###################################### 41 # Section 2 - The ObjectOracle class # 42 ###################################### 43 44 class ObjectOracle(GameSubsystem): 45 """ 46 ObjectOracle has references to all VGO and VGO-child 47 objects that are created. All managers are supposed to 48 report to the ObjectOracle whenever they make a new 49 VGO-type object. ObjectOracle can fetch info about 50 these objects by ID, name, or other attributes, but 51 does not manage them in any way, nor change their 52 values ever (with the possible exception of ID). 53 """ 54 55 def __init__(self, game): 56 57 # Parent init 58 super().__init__(game) 59 60 #################################### 61 # Section 3 - The SaveSystem class # 62 #################################### 63 64 class SaveSystem(GameSubsystem): 65 """ 66 The SaveSystem object handles saving to and loading 67 from a game save JSON file. This large JSON file is 68 subdivided into smaller objects called definitions 69 that are transformed at load time to Python dicts. 70 These definitions are passed to Game, managers, and 71 other subsystems in order to generate the gameplay 72 environment. 73 """ 74 75 def __init__(self, game): 76 77 # Parent init 78 super().__init__(game) 79 80 # Environment 81 self.active_save_env = {} 82 83 def save_to_file(self, filename): 84 # TODO: Check if it exists and prompt to overwrite (maybe in a another object???) 85 # TODO: Make cross-platform compatible 86 with open(os.path.join(SAVE_PATH, filename)) as sf: json.dump(self.active_save_env, sf) 87 88 def load_from_file(self, filename): 89 with open(os.path.join(SAVE_PATH, filename)) as lf: self.active_save_env = json.load(lf) 90 91 def apply_environment(self): 92 pass 93 94 def update_environment(self, key, value): 95 pass