tzed

Simple story-driven open world 2D CRPG.
Log | Files | Refs | README | LICENSE

commit 374816c7695e2d8c6952cff38733f100e416dac3
parent 93af07d6f2bb3da035efe00edd954bc7c35b1747
Author: Erik Letson <hmagellan@hmagellan.com>
Date:   Thu, 17 Jun 2021 09:23:31 -0500

settings and redo controls

Diffstat:
Adata/etc/defaults.json | 12++++++++++++
Msrc/envvars.py | 37+++++++++++++++++++++++--------------
Msrc/interface.py | 40+++++++++++++++++++++++++---------------
3 files changed, 60 insertions(+), 29 deletions(-)

diff --git a/data/etc/defaults.json b/data/etc/defaults.json @@ -0,0 +1,12 @@ +{ + "screen_width" : 1024, + "screen_height" : 768, + "framerate" : 30, + "controls" : { + "up" : [119, 1073741906], + "down" : [115, 1073741905], + "left" : [97, 1073741904], + "right": [100, 1073741903] + }, + "overworld_board" : "test_ow" +} diff --git a/src/envvars.py b/src/envvars.py @@ -13,11 +13,30 @@ import os, enum, json, pathlib # Section 1 - Constants # ######################### +# Conditional/initial paths +DATA_PATH = os.path.join(os.getcwd(), "data") +USERLOCAL_PATH = os.path.join(str(pathlib.Path.home), ".local", "share", "tzed") +if os.path.exists(os.path.join(USERLOCAL_PATH, "settings.json")): + SETTINGS_PATH = os.path.join(USERLOCAL_PATH, "settings.json") +else: + SETTINGS_PATH = os.path.join(DATA_PATH, "etc", "defaults.json") + +# Additional file paths +IMAGE_PATH = os.path.join(DATA_PATH, "img") +SOUND_PATH = os.path.join(DATA_PATH, "snd") +FONT_PATH = os.path.join(DATA_PATH, "font") +TILE_PATH = os.path.join(DATA_PATH, "tsx") +BOARD_PATH = os.path.join(DATA_PATH, "boards") +JSON_PATH = os.path.join(DATA_PATH, "json") +SAVE_PATH = os.path.join(USERLOCAL_PATH, "savegames") + # Game settings -SCREEN_WIDTH = 1024 -SCREEN_HEIGHT = 768 -FRAMERATE = 30 -OVERWORLD_BOARD = "test_ow" +with open(SETTINGS_PATH) as sf: SETTINGS_RAW = json.load(sf) +SCREEN_WIDTH = SETTINGS_RAW["screen_width"] +SCREEN_HEIGHT = SETTINGS_RAW["screen_height"] +FRAMERATE = SETTINGS_RAW["framerate"] +CONTROLS = SETTINGS_RAW["controls"] +OVERWORLD_BOARD = SETTINGS_RAW["overworld_board"] # Tile settings BASE_TILE_WIDTH = 16 @@ -27,16 +46,6 @@ OVERWORLD_SCALE_FACTOR = 2 VIEWPORT_SIZE = (640, 480) COLORKEY = (255, 0, 255) -# File paths -DATA_PATH = os.path.join(os.getcwd(), "data") -IMAGE_PATH = os.path.join(DATA_PATH, "img") -SOUND_PATH = os.path.join(DATA_PATH, "snd") -FONT_PATH = os.path.join(DATA_PATH, "font") -TILE_PATH = os.path.join(DATA_PATH, "tsx") -BOARD_PATH = os.path.join(DATA_PATH, "boards") -JSON_PATH = os.path.join(DATA_PATH, "json") -SAVE_PATH = os.path.join(str(pathlib.Path.home), ".local", "share", "tzed", "savegames") - # Stats constants # TODO: Could be totally changed going forward CORE_STATS = ["STR", "FIN", "MAG", "LUK"] diff --git a/src/interface.py b/src/interface.py @@ -39,7 +39,7 @@ class GameInterface(subsystem.GameSubsystem): self.right_double_click_timer = 0 self.right_double_click_mousepos = None self.old_mousepos = None - self.key_bools = [False for k in range(0, 350)] + self.key_bools = { n:False for k in CONTROLS for n in CONTROLS[k] } # Recording if self.bus != None: @@ -65,14 +65,14 @@ class GameInterface(subsystem.GameSubsystem): """ React to a key being pressed. """ - if event.key < len(self.key_bools): + if event.key in self.key_bools.keys(): self.key_bools[event.key] = True def handle_key_release(self, event): """ React to a key being released. """ - if event.key < len(self.key_bools): + if event.key in self.key_bools.keys(): self.key_bools[event.key] = False def react_to_keys(self): @@ -82,18 +82,28 @@ class GameInterface(subsystem.GameSubsystem): update. """ if self.game.state_mode in OVERHEAD_MODES: - if self.key_bools[pygame.K_w]: - self.manager_bus.perform_entity_manager_move_player((0, -1)) - self.key_bools[pygame.K_w] = False - elif self.key_bools[pygame.K_s]: - self.manager_bus.perform_entity_manager_move_player((0, 1)) - self.key_bools[pygame.K_s] = False - elif self.key_bools[pygame.K_a]: - self.manager_bus.perform_entity_manager_move_player((-1, 0)) - self.key_bools[pygame.K_a] = False - elif self.key_bools[pygame.K_d]: - self.manager_bus.perform_entity_manager_move_player((1, 0)) - self.key_bools[pygame.K_d] = False + for k in self.key_bools: + if self.key_bools[k]: + if k in CONTROLS["up"]: + self.manager_bus.perform_entity_manager_move_player((0, -1)) + for j in CONTROLS["up"]: + self.key_bools[j] = False + return + elif k in CONTROLS["down"]: + self.manager_bus.perform_entity_manager_move_player((0, 1)) + for j in CONTROLS["down"]: + self.key_bools[j] = False + return + elif k in CONTROLS["left"]: + self.manager_bus.perform_entity_manager_move_player((-1, 0)) + for j in CONTROLS["left"]: + self.key_bools[j] = False + return + elif k in CONTROLS["right"]: + self.manager_bus.perform_entity_manager_move_player((1, 0)) + for j in CONTROLS["right"]: + self.key_bools[j] = False + return def handle_mouse_click(self, event): """