Return to repo list

heart-of-gold

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

commit 131afddf3449c6d115d9393edd581b415ce94179
parent 12bfb2530a8065f51befa03d09b5a5b90267e770
Author: Erik Letson <hmagellan@hmagellan.com>
Date:   Sat,  7 Nov 2020 23:11:37 -0600

Restored movement functionality (mostly)

Diffstat:
Msrc/piece.py | 64++++++++++++++++++++--------------------------------------------
Msrc/subsystem.py | 13+++++++++++++
2 files changed, 33 insertions(+), 44 deletions(-)

diff --git a/src/piece.py b/src/piece.py @@ -1,4 +1,4 @@ -import pygame, os, json +import pygame, os, json, math from . import manager, entity from .constants import * @@ -62,7 +62,7 @@ class PieceManager(manager.Manager): n_astats = definition[p]["active_stats"] n_team = definition[p]["team"] n_equip = definition[p]["equipment"] - np = Piece(n_sheet, n_sprite, n_anim, n_animated, n_name, n_passable, n_nstats, n_astats, n_team, n_equip) + np = Piece(n_sheet, n_sprite, n_anim, n_animated, self, n_name, n_passable, n_nstats, n_astats, n_team, n_equip) np.assign_tile(self.bus.fetch_tile_by_tile_pos(tuple(definition[p]["tile"]))) np.snap_to_tile() self.add_piece(np) @@ -126,7 +126,6 @@ class PieceManager(manager.Manager): no piece is under the tile cursor, the selected piece should become None. """ - # TODO: This could stand to be more robust self.selected_piece = self.get_piece_by_tile(self.tile_cursor.tile_pos) def set_piece_move_to_tile_path(self, piece, path): @@ -136,6 +135,21 @@ class PieceManager(manager.Manager): if piece != None and path != None: piece.set_move_along_tile_path(path) + def get_piece_max_legal_move(self, piece): + """ + Calculate the absolute maximum legal move of a piece. + Returns a list of maximum legal moved. + """ + m = piece.active_stats["MOVE"] + legal_moves = [] + for x in range(0, self.bus.fetch_current_board_width()): + for y in range(0, self.bus.fetch_current_board_height()): + mx = piece.tile_pos[0] - x + my = piece.tile_pos[1] - y + if (abs(mx) + abs(my)) <= m: + legal_moves.append((x, y)) + return legal_moves + def update_tile_cursor(self, surface = None): """ Update the tile cursor object. @@ -150,29 +164,6 @@ class PieceManager(manager.Manager): if surface != None: self.pieces.update(surface) - #def load_pieces_from_json(self, entsjson): - # """ - # Load one or more pieces from a JSON file. - # """ - # j = json.load(open(os.path.join(ENTITY_JSON_PATH, entsjson))) - # for e in j: - # # TODO: type should be enum - # if j[e]["type"] == "Entity": - # ne = Entity(j[e]["name"], 0, - # self.game.sheet_manager.loaded_sheets[j[e]["sheet"]], tuple(j[e]["sprite"]), - # self.game.sheet_manager.animations[j[e]["sheet"]][j[e]["animation"]], - # j[e]["animated"], j[e]["passable"], unit.Unit(self, self.game.unit_manager.get_stats(j[e]["name"]))) - # elif j[e]["type"] == "Piece": - # ne = Piece(j[e]["name"], 0, - # self.game.sheet_manager.loaded_sheets[j[e]["sheet"]], tuple(j[e]["sprite"]), - # self.game.sheet_manager.animations[j[e]["sheet"]][j[e]["animation"]], - # j[e]["animated"], j[e]["passable"], unit.Unit(self, self.game.unit_manager.get_stats(j[e]["name"])), - # j[e]["team"]) - # self.pieces.append(ne) - # ne.assign_tile(self.game.board_manager.get_tile_at_tile_pos(tuple(j[e]["tile"]))) - # ne.snap_to_tile() - # self.add_entity(ne) - #def set_entity_move_to_tile_pos(self, entity, tile_pos): # """ @@ -207,22 +198,6 @@ class PieceManager(manager.Manager): # self.game.board_manager.load_overlay() # return False - #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 ############################### # Section 2 - The Piece class # @@ -239,7 +214,7 @@ class Piece(entity.Entity): """ def __init__(self, sheet, sprite = (0, 0), animation = None, animated = False, - name = None, passable = False, normal_stats = None, active_stats = None, team = None, equipment = None): + manager = None, name = None, passable = False, normal_stats = None, active_stats = None, team = None, equipment = None): # Parent initialization super().__init__(sheet, sprite, animation, animated) @@ -248,6 +223,7 @@ class Piece(entity.Entity): self.facing = FACE_DIR.L # Others + self.manager = manager self.name = name self.passable = passable self.normal_stats = normal_stats @@ -292,7 +268,7 @@ class Piece(entity.Entity): # animation should be assigned according to whether or not a board move is # actually taking place. Also, this is a very roundabout way to reference # the sheet's animations. Maybe sheets should be aware of their anims? - self.set_animation(self.manager.bus.fetch_animation("walk_" + self.facing.name), True) + self.set_animation(self.manager.bus.fetch_animation(self.sheet.name, "walk_" + self.facing.name), True) def set_move_along_tile_path(self, tile_seq): """ diff --git a/src/subsystem.py b/src/subsystem.py @@ -110,6 +110,7 @@ class GameInterface(GameSubsystem): if self.game.control_mode == CTRL_MODES.Turn_Normal: self.bus.perform_select_piece_with_tile_cursor() if self.bus.fetch_selected_piece() != None: + self.bus.perform_display_move_range_of_piece(self.bus.fetch_selected_piece()) self.game.control_mode = CTRL_MODES.Turn_Select_Move # Selecting a move for the active piece control @@ -198,6 +199,16 @@ class ManagerBus(GameSubsystem): return self.game.board_manager.get_tile_pos_at_position(position) def fetch_piece_path_by_previous_moves(self, start_tile, target_tile): return self.game.board_manager.get_path_by_previous_moves(start_tile, target_tile) + def fetch_current_board_width(self): + if self.game.board_manager.current_board != None: + return self.game.board_manager.current_board.tmx_data.width + else: + return None + def fetch_current_board_height(self): + if self.game.board_manager.current_board != None: + return self.game.board_manager.current_board.tmx_data.height + else: + return None # Performs: All callable non-return behaviors of managers # TODO: Error-checking would be one of the great additions possible here @@ -213,6 +224,8 @@ class ManagerBus(GameSubsystem): self.game.piece_manager.set_piece_move_to_tile_path(piece, path) def perform_continue_current_scene_script(self): self.game.scene_manager.current_scene.continue_script() + def perform_display_move_range_of_piece(self, piece): + self.game.board_manager.display_as_move_range(self.game.piece_manager.get_piece_max_legal_move(self.game.piece_manager.selected_piece)) #################################### # Section 4 - The GameCamera class #