Return to repo list

heart-of-gold

Tactical RPG written in python, using pygame.
Return to HMagellan.com

commit 7fb88b39411974f6e9427ebe88ef443b9ce7e428
parent 673554315117b47ac2b76f455db05af565aa8e1c
Author: Erik Letson <hmagellan@hmagellan.com>
Date:   Sun, 20 Sep 2020 17:29:16 -0500

Proper movement calculation now

Diffstat:
Mdata/img/board_overlays_1.png | 0
Mdata/json/ents/testmap1.json | 2+-
Mmain.py | 2++
Msrc/board.py | 26++++++++++++++------------
Msrc/vgo.py | 21+++++++++++++++++++--
5 files changed, 36 insertions(+), 15 deletions(-)

diff --git a/data/img/board_overlays_1.png b/data/img/board_overlays_1.png Binary files differ. diff --git a/data/json/ents/testmap1.json b/data/json/ents/testmap1.json @@ -6,6 +6,6 @@ "animation" : "stand_L", "animated" : true, "passable" : false, - "tile" : [3, 7] + "tile" : [5, 4] } } diff --git a/main.py b/main.py @@ -9,6 +9,8 @@ from src import game # choice altogether. Perhaps there should be one huge save.json file for all changes. If that is so, then there could be several JSONs in the dir # located at data/json that are loaded first, then the save.json file is parsed by a save-load subsystem and any changes are applied, at which point # the game would be in a loaded state. This seems the ideal solution, and it can be put off a bit. +# 3. Implement the "draw()" functionality of DirtySprite for all VGOs, which will be done in anything that manages a LayeredDirty group, e.g. in the +# EntityManager object. pygame.init() diff --git a/src/board.py b/src/board.py @@ -86,23 +86,25 @@ class BoardManager(manager.Manager): return (x, y, gid) return None - def display_entity_move_range(self, ent = None): + def display_as_move_range(self, tile_pos_list): """ - Display the move range of the given entity, or undisplay - move range from overlay if no entity is passed. + Display a move range from a given list of tile_pos + tuples. """ - if ent != None: + # First, refresh overlay to avoid drawing over existing move markers + self.load_overlay() + + # Next, if there are some extant tile_pos moves listed, display them + if tile_pos_list != None: for layer in self.current_board.tmx_data.visible_layers: if isinstance(layer, pytmx.TiledTileLayer): for x, y, gid in layer: - if abs(x - ent.tile_pos[0]) <= ent.unit.active_stats["MOVE"] and abs(y - ent.tile_pos[1]) <= ent.unit.active_stats["MOVE"]: - v = vgo.VisibleGameObject(self.game.sheet_manager.loaded_sheets["board_overlays_1"]) - v.set_position((x * self.current_board.tmx_data.tilewidth, y * self.current_board.tmx_data.tileheight)) - v.set_sprite((1, 0)) - self.board_overlay.add(v) - else: - #TODO: This should probably do more than just refresh overlay load - self.load_overlay() + for p in tile_pos_list: + if p == (x, y) and self.current_board.tmx_data.get_tile_properties_by_gid(gid)["Passable"] == 1: + v = vgo.VisibleGameObject(self.game.sheet_manager.loaded_sheets["board_overlays_1"]) + v.set_position((x * self.current_board.tmx_data.tilewidth, y * self.current_board.tmx_data.tileheight)) + v.set_sprite((1, 0)) + self.board_overlay.add(v) def update_board(self, surface = None): """ diff --git a/src/vgo.py b/src/vgo.py @@ -256,10 +256,27 @@ class EntityManager(manager.Manager): e = self.get_entities_by_tile(tile_def) if e != []: self.selected_entity = e[0] # Select only the topmost ent on this tile - self.game.board_manager.display_entity_move_range(self.selected_entity) + self.game.board_manager.display_as_move_range(self.get_entity_legal_move_tile_pos(self.selected_entity)) else: self.selected_entity = None - self.game.board_manager.display_entity_move_range(self.selected_entity) + self.game.board_manager.load_overlay() + + def get_entity_legal_move_tile_pos(self, entity): + """ + Calculate potential legal moves for entity. Note, this + does not return tiles, but rather hypothetical legal + tile_pos coordinate in the form (x, y). The return is + a list of these coordinates. Uses the A* algorithm. + """ + m = entity.unit.active_stats["MOVE"] + legal_moves = [] + for x in range(0, self.game.board_manager.current_board.tmx_data.width): + for y in range(0, self.game.board_manager.current_board.tmx_data.height): + mx = entity.tile_pos[0] - x + my = entity.tile_pos[1] - y + if (abs(mx) + abs(my)) <= m: + legal_moves.append((x, y)) + return legal_moves def update_entities(self, surface = None): """