Return to repo list

tzed

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

commit 888393ab70d86ad8564a0d3542bb727b131cb4fe
parent 374816c7695e2d8c6952cff38733f100e416dac3
Author: Erik Letson <hmagellan@hmagellan.com>
Date:   Thu, 17 Jun 2021 18:38:33 -0500

added color mapping

Diffstat:
Adata/boards/test_ow/colors.json | 3+++
Adata/boards/test_town/colors.json | 4++++
Mdata/etc/defaults.json | 20+++++++++++++++++++-
Msrc/board.py | 40+++++++++++++++++++++++++++++++++-------
Msrc/entity.py | 15+++++++++++++--
Msrc/envvars.py | 7+++++--
6 files changed, 77 insertions(+), 12 deletions(-)

diff --git a/data/boards/test_ow/colors.json b/data/boards/test_ow/colors.json @@ -0,0 +1,3 @@ +{ + "50" : 5 +} diff --git a/data/boards/test_town/colors.json b/data/boards/test_town/colors.json @@ -0,0 +1,4 @@ +{ + "2" : [50], + "5" : [4, 2, 13, 27] +} diff --git a/data/etc/defaults.json b/data/etc/defaults.json @@ -8,5 +8,23 @@ "left" : [97, 1073741904], "right": [100, 1073741903] }, - "overworld_board" : "test_ow" + "overworld_board" : "test_ow", + "color_palette" : [ + [0, 0, 0], + [0, 0, 128], + [128, 128, 0], + [128, 128, 128], + [128, 0, 0], + [170, 0, 170], + [255, 128, 0], + [255, 128, 128], + [0, 128, 0], + [0, 170, 170], + [128, 255, 0], + [128, 255, 128], + [170, 170, 170], + [128, 128, 255], + [255, 255, 128], + [255, 255, 255] + ] } diff --git a/src/board.py b/src/board.py @@ -148,19 +148,33 @@ class Board(object): # Saved values self.manager = manager self.boardname = boardname + + # File values self.filename = boardname + ".tmx" + self.location_path = os.path.join(BOARD_PATH, self.boardname, "location.json") + self.location_data = None + self.color_path = os.path.join(BOARD_PATH, self.boardname, "colors.json") + self.color_data = None # Pytmx values self.tmx_data = pytmx.load_pygame(os.path.join(BOARD_PATH, self.boardname, self.filename)) self.board_dimensions = (self.tmx_data.width, self.tmx_data.height) self.pixel_dimensions = (self.tmx_data.width * BASE_TILE_WIDTH, self.tmx_data.height * BASE_TILE_HEIGHT) - # Optional values + # Overworld values self.overworld_tile_pos = None - if os.path.exists(os.path.join(BOARD_PATH, self.boardname, "location.json")): - with open(os.path.join(BOARD_PATH, self.boardname, "location.json")) as f: locdat = json.load(f) - if "overworld_tile_pos" in locdat.keys(): - self.overworld_tile_pos = tuple(locdat["overworld_tile_pos"]) + if os.path.exists(self.location_path): + with open(self.location_path) as f: self.location_data = json.load(f) + if "overworld_tile_pos" in self.location_data.keys(): + self.overworld_tile_pos = tuple(self.location_data["overworld_tile_pos"]) + + # Tile color values + self.tile_color_map = [ [] for i in range(len(COLOR_PALETTE) + 1) ] + if os.path.exists(self.color_path): + with open(self.color_path) as f: self.color_data = json.load(f) + for c in self.color_data: + if int(c) <= len(self.tile_color_map): + self.tile_color_map[int(c)] = self.color_data[c] # Generate map self.tilesprites = pygame.sprite.Group() @@ -171,7 +185,15 @@ class Board(object): if t: r = t.get_rect() r.topleft = (x * BASE_TILE_WIDTH, y * BASE_TILE_HEIGHT) - self.tilesprites.add(TileSprite(t, r)) + tid = self.tmx_data.get_tile_properties_by_gid(gid)["id"] + colo = 15 + i = 0 + for co in self.tile_color_map: + if tid in co: + colo = i + else: + i += 1 + self.tilesprites.add(TileSprite(t, r, colo)) def draw_board(self, surface = None, camrect = None): """ @@ -189,7 +211,7 @@ class TileSprite(pygame.sprite.Sprite): Simplified sprite subclass for drawing the board's tiles. """ - def __init__(self, image, rect): + def __init__(self, image, rect, pixcolor = 15): # Parent init pygame.sprite.Sprite.__init__(self) @@ -198,6 +220,10 @@ class TileSprite(pygame.sprite.Sprite): self.image = image self.rect = rect + # Coloring + self.palette_color = pixcolor + pygame.PixelArray(self.image).replace(BASE_COLOR, COLOR_PALETTE[self.palette_color]) + def update(self, surface = None, camrect = None): """ Draw the tile image if it is on the rect. diff --git a/src/entity.py b/src/entity.py @@ -52,7 +52,7 @@ class EntityManager(manager.Manager): # Then, load the entities for e in ent_def: if ent_def[e]["type"] == "playerspawn": - self.player = PlayerEntity(sh["chars1"], (0, 0)) + self.player = PlayerEntity(sh["chars1"], (0, 0), None, False, 9) self.player.assign_tile(tuple(ent_def[e]["location"]), self.bus.perform_board_manager_get_tile_at_tile_pos(tuple(ent_def[e]["location"]))[2]) self.player.snap_to_tile() self.system_bus.perform_camera_follow_entity(self.player, False) @@ -307,6 +307,13 @@ class Entity(pygame.sprite.DirtySprite): self.tile_pos = tile_pos self.tile_gid = tile_gid + def replace_color(self, color, repcol): + """ + Replace all occurances of 'color' in this entity's image + with 'repcol'. + """ + pygame.PixelArray(self.image).replace(color, repcol) + def act(self): """ This method is called as part of update() and is meant @@ -340,11 +347,15 @@ class PlayerEntity(Entity): affected by where the Player is located. """ - def __init__(self, sheet, sprite = (0, 0), animation = None, animated = False): + def __init__(self, sheet, sprite = (0, 0), animation = None, animated = False, pixcolor = 15): # Parent initialization super().__init__(sheet, sprite, animation, animated) + # Values + self.palette_color = pixcolor + self.replace_color(BASE_COLOR, COLOR_PALETTE[self.palette_color]) + class OverlayEntity(Entity): """ Class representing the transparent overlay entities diff --git a/src/envvars.py b/src/envvars.py @@ -36,7 +36,8 @@ 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"] +OVERWORLD_BOARD = SETTINGS_RAW["overworld_board"] +COLOR_PALETTE = tuple(tuple(color) for color in SETTINGS_RAW["color_palette"]) # Tile settings BASE_TILE_WIDTH = 16 @@ -44,7 +45,10 @@ BASE_TILE_HEIGHT = 16 LOCATION_SCALE_FACTOR = 4 OVERWORLD_SCALE_FACTOR = 2 VIEWPORT_SIZE = (640, 480) + +# Color constants COLORKEY = (255, 0, 255) +BASE_COLOR = (255, 255, 255) # Stats constants # TODO: Could be totally changed going forward @@ -56,7 +60,6 @@ META_STATS = ["LVL", "EXP", "RNG"] STATE_MODES = enum.Enum('STATE_MODES', 'Main_Menu_Mode Overworld_Mode Location_Mode Battle_Mode Dungeon_Mode') CTRL_MODES = enum.Enum('CTRL_MODES', 'No_Control Main_Menu_Normal') GAME_EFFECTS = enum.Enum('GAME_EFFECTS', 'ef_game_dummy ef_game_quit ef_game_switch_mode ef_game_switch_control') -TEAMS = enum.Enum('TEAMS', 'Player Ally Neutral Enemy Other') # Enum collections OVERHEAD_MODES = [