Return to repo list

tzed

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

commit 9b6d5304e1eae8c01dd499f99f5dce3c0359300e
parent 888393ab70d86ad8564a0d3542bb727b131cb4fe
Author: Erik Letson <hmagellan@hmagellan.com>
Date:   Wed, 30 Jun 2021 12:15:36 -0500

checkpoint for subset tile load

Diffstat:
Msrc/board.py | 58+++++++++++++++++++++++++++++++++++++++++-----------------
Msrc/bus.py | 2++
Msrc/entity.py | 13+++++++++++++
3 files changed, 56 insertions(+), 17 deletions(-)

diff --git a/src/board.py b/src/board.py @@ -33,6 +33,8 @@ class BoardManager(manager.Manager): Load a given board. """ self.current_board = Board(self, boardname) + r = int(24 / (self.system_bus.fetch("camera", "scale_factor") / 2)) + self.generate_board_tilesprites_by_range((-r, r), (-r, r)) self.update(None) def get_tile_pos_at_position(self, pos): @@ -113,6 +115,13 @@ class BoardManager(manager.Manager): adj_list[t] = self.get_adjacent_tiles_by_tile_pos(t) return adj_list + def generate_board_tilesprites_by_range(self, rangex, rangey): + """ + Generate the current board's tiles by range. + """ + if self.current_board != None: + self.current_board.generate_tilesprites_by_range(rangex, rangey) + def expose(self): """ Expose info about the board to the ManagerBus. @@ -148,6 +157,8 @@ class Board(object): # Saved values self.manager = manager self.boardname = boardname + self.tilesprites = pygame.sprite.Group() + self.tile_color_map = [] # File values self.filename = boardname + ".tmx" @@ -168,33 +179,46 @@ class Board(object): 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) ] + # Color + self.color_tiles(COLOR_PALETTE) + + def color_tiles(self, palette): + """ + Color the tiles on the tilesprite board. + """ + self.tile_color_map = [ [] for i in range(len(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 + def generate_tilesprites_by_range(self, rangex, rangey): + """ + Generate tilesprites in a range in two directions. The + arguments 'rangex' and 'rangey' should be tuples of + two members, index 0 being the start and index 1 being + the terminus. + """ self.tilesprites = pygame.sprite.Group() for layer in self.tmx_data.visible_layers: if isinstance(layer, pytmx.TiledTileLayer): for x, y, gid in layer: - t = self.tmx_data.get_tile_image_by_gid(gid) - if t: - r = t.get_rect() - r.topleft = (x * BASE_TILE_WIDTH, y * BASE_TILE_HEIGHT) - 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)) - + if x in range(rangex[0], rangex[1]) and y in range(rangey[0], rangey[1]): + t = self.tmx_data.get_tile_image_by_gid(gid) + if t: + r = t.get_rect() + r.topleft = (x * BASE_TILE_WIDTH, y * BASE_TILE_HEIGHT) + 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): """ Draw the tiles of the board onto the provided PyGame diff --git a/src/bus.py b/src/bus.py @@ -180,5 +180,7 @@ class ManagerBus(Bus): return self.collection["board_manager"].get_tile_at_tile_pos(tilepos) def perform_board_manager_get_board_tile_properties_by_tile_gid(self, gid): return self.collection["board_manager"].get_board_tile_properties_by_tile_gid(gid) + def perform_board_manager_generate_board_tilesprites_by_range(self, rangex, rangey): + self.collection["board_manager"].generate_board_tilesprites_by_range(rangex, rangey) def perform_entity_manager_move_player(self, tile_offset): self.collection["entity_manager"].move_player_by_tile_offset(tile_offset) diff --git a/src/entity.py b/src/entity.py @@ -125,6 +125,19 @@ class EntityManager(manager.Manager): self.player.snap_to_tile() self.system_bus.perform_camera_follow_entity(self.player, False) + # Regenerate the board + r = int(24 / (self.system_bus.fetch("camera", "scale_factor") / 2)) + self.bus.perform_board_manager_generate_board_tilesprites_by_range((-r, r), (-r, r)) + + def expose(self): + """ + Expose info about the board to the ManagerBus. + """ + data = { + "player" : self.player + } + self.bus.record(self.name, data) + def update_managed(self, surface = None): """ Update currently managed entities.