Return to repo list

tzed

Simple story-driven open world 2D CRPG.
Return to HMagellan.com

commit 4cc566979026349edfb3739b739dcdcde02f1b7a
parent 6c2acc116a4d7186e8d56bca3c3a24e227aaa6f5
Author: Erik Letson <hmagellan@hmagellan.com>
Date:   Sat, 28 Aug 2021 01:20:56 -0500

saving checkpoint

Diffstat:
Mdata/boards/testloc1/entities.json | 6------
Mdata/boards/testloc2/entities.json | 8++++----
Msrc/board.py | 4+---
Msrc/game.py | 52++++++++++++++++++++++++++++++++++++++++------------
Msrc/gamelib.py | 4++--
5 files changed, 47 insertions(+), 27 deletions(-)

diff --git a/data/boards/testloc1/entities.json b/data/boards/testloc1/entities.json @@ -1,11 +1,5 @@ { "dynamic" : [ - { - "type" : "playerspawn", - "cell" : [0, 0], - "tile" : [1, 1], - "data" : {} - } ], "static" : [ { diff --git a/data/boards/testloc2/entities.json b/data/boards/testloc2/entities.json @@ -1,6 +1,6 @@ { - "static" : { - }, - "dynamic" : { - } + "static" : [ + ], + "dynamic" : [ + ] } diff --git a/src/board.py b/src/board.py @@ -161,9 +161,7 @@ class Board(object): switched to. """ for e in self.entity_definition["dynamic"]: - # NOTE: The player entity is not added to the board's entities group! - if e["type"] == "playerspawn": - self.game.spawn_player(tuple(e["cell"]), tuple(e["tile"])) + pass def cell_shift_dynamic_entities(self): """ diff --git a/src/game.py b/src/game.py @@ -41,8 +41,10 @@ class Game(object): self.interface = interface.Interface(self) self.message_board = message.MessageBoard(self, (313, 620), (696, 72)) # TODO: Hardcoded. Need to make the whole UI resolution-aware... - # Pygame sprite objects + # Gameplay objects self.player = None + self.player_name = "" + self.party = {} # UI elements self.ui_font = pygame.font.Font(os.path.join(FONT_PATH, MESSAGE_FONT), MESSAGE_FONT_SIZE) # TODO: Should there be a UI font option??? @@ -58,6 +60,7 @@ class Game(object): self.clock_element_pos = (0, 0) # Other values + self.active_story = "Tzed 1" # TMP! self.sheets = {} self.loaded_boards = {} self.current_board = None @@ -81,14 +84,6 @@ class Game(object): self.load_boards("boards.json") self.build_ui() - # TMP! - self.switch_board("testloc1", (0, 0)) - self.message_board.post("hi there", {}) - self.message_board.post("bye there", {}) - - # Switch to game control - self.switch_mode(STATE_MODES.Overworld_Mode) - def load_sheets(self, imagefile): """ Load all of the images as sheets. @@ -145,7 +140,7 @@ class Game(object): else: return 1 - def spawn_player(self, cellpos, tilepos): + def spawn_player(self, playername, cellpos, tilepos): """ Spawn the player in the current board at the given cell and tile coordinates. Called @@ -156,6 +151,7 @@ class Game(object): """ if self.current_board != None: self.player = entity.PlayerEntity(self.sheets["chars" + str(self.current_board.scale_factor)].sprites[(0, 0)], cellpos, tilepos) + self.player_name = playername self.player.rect.topleft = ((SCREEN_WIDTH // 3) - 6, (SCREEN_HEIGHT // 3) - 6)# TODO: Weirdly derived self.current_board.position_offset = self.player.rect.topleft self.current_board.position_to_tile(tilepos) @@ -238,7 +234,7 @@ class Game(object): # Factor this time into the appropriate categories # TODO: Time should be defined externally to allow for custom time systems - self.seconds = (10 * self.gametime) + 12999993970 + self.seconds = (10 * self.gametime) + 15000000070 self.minutes = self.seconds // 60 self.hours = self.seconds // (60 * 60) self.days = self.seconds // (60 * 60 * 25) @@ -322,9 +318,41 @@ class Game(object): be safely called multiple times per frame. """ # TODO: This will see regular expansion - self.gamedata["$PLAYERNAME"] = "Joe" + self.gamedata["$PLAYERNAME"] = self.player_name self.gamedata["$CURRENTBOARDNAME"] = self.current_board.display_name + def save_game(self, savename): + """ + Save the current game state as a JSON file. + """ + savestate = { + "story" : self.active_story, + "player" : self.player_name, + "board" : self.current_board.name, + "cell" : self.player.cellpos, + "tile" : self.player.tilepos, + "party" : self.party + } + with open(os.path.join(SAVE_PATH, savename + ".json")) as sj: json.dump(savestate) + + def load_game(self, savename): + """ + Load a save from JSON. + """ + if os.path.exists(os.path.join(SAVE_PATH, savename + ".json")): + with open(os.path.join(SAVE_PATH, savename + ".json")) as lj: saveraw = json.load(lj) + self.active_story = saveraw["story"] + self.player_name = saveraw["player"] + self.party = saveraw["party"] + self.switch_board(saveraw["board"], tuple(saveraw["cell"])) + self.spawn + + # Switch to game control + # TODO: Saves will eventually take note of what mode they were saved in + self.switch_mode(STATE_MODES.Overworld_Mode) + else: + return 1 + def shift_frames(self, framerate = FRAMERATE): """ Shift to the next frame of the game at the specified diff --git a/src/gamelib.py b/src/gamelib.py @@ -29,7 +29,7 @@ FONT_PATH = os.path.join(DATA_PATH, "fonts") BOARD_PATH = os.path.join(DATA_PATH, "boards") OVERWORLD_PATH = os.path.join(BOARD_PATH, "overworlds") LOCATION_PATH = os.path.join(BOARD_PATH, "locations") -SAVE_PATH = os.path.join(USERLOCAL_PATH, "savegames") +SAVE_PATH = os.path.join(USERLOCAL_PATH, "saves") # Game settings with open(SETTINGS_PATH) as sf: SETTINGS_RAW = json.load(sf) @@ -38,7 +38,7 @@ SCREEN_HEIGHT = SETTINGS_RAW["screen_height"] SCREEN_MARGINS = (SETTINGS_RAW["screen_margin_x"], SETTINGS_RAW["screen_margin_y"]) FRAMERATE = SETTINGS_RAW["framerate"] CONTROLS = SETTINGS_RAW["controls"] -MESSAGE_FONT = SETTINGS_RAW["message_font"] +MESSAGE_FONT = SETTINGS_RAW["message_font"]t MESSAGE_FONT_SIZE = SETTINGS_RAW["message_font_size"] COLOR_PALETTE = tuple(tuple(color) for color in SETTINGS_RAW["color_palette"]) OVERLAY_SHEET = SETTINGS_RAW["overlay_sheet"]