Return to repo list

heart-of-gold

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

commit bc7653ca250f3027fa5bef31cbedf5d9ce0e4b40
parent c85951c3a8f1dc000ea637f53613ee7dd5a54854
Author: Erik Letson <hmagellan@hmagellan.com>
Date:   Sat,  7 Nov 2020 22:23:23 -0600

last refactor checkpoint, time to bring it back to life

Diffstat:
Msrc/board.py | 3+--
Msrc/game.py | 4+++-
Msrc/piece.py | 16+++++++---------
Msrc/subsystem.py | 92+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------------------
4 files changed, 80 insertions(+), 35 deletions(-)

diff --git a/src/board.py b/src/board.py @@ -121,7 +121,7 @@ class BoardManager(manager.Manager): self.load_overlay() # TEMP!!!! - self.create_move_range(self.bus.fetch_selected_piece) + self.create_move_range(self.bus.fetch_selected_piece()) for t in self.move_targets: e = entity.Entity(self.bus.fetch_sheet("board_overlays_1"), (1, 0)) e.set_position((t[0] * self.current_board.tmx_data.tilewidth, t[1] * self.current_board.tmx_data.tileheight)) @@ -215,7 +215,6 @@ class BoardManager(manager.Manager): my = tile_pos[1] - y if (mx, my) in adj.keys(): adj[(mx, my)] = (x, y) - return adj def get_adjacency_list(self, tiles): diff --git a/src/game.py b/src/game.py @@ -6,6 +6,8 @@ from .constants import * # game.py # ########### +# TODO: There should be a method to call to change control mode as well as state mode + # This file contains: # 1. The 'Game' class, which defines the overarching game object @@ -40,11 +42,11 @@ class Game(object): self.frame_clock = pygame.time.Clock() # Subsystems - self.interface = subsystem.GameInterface(self) self.manager_bus = subsystem.ManagerBus(self) self.camera = subsystem.GameCamera(self) self.object_oracle = subsystem.ObjectOracle(self) self.save_system = subsystem.SaveSystem(self) + self.interface = subsystem.GameInterface(self, self.manager_bus, self.camera) # Managers self.sheet_manager = images.SheetManager(self, self.manager_bus) diff --git a/src/piece.py b/src/piece.py @@ -123,6 +123,13 @@ class PieceManager(manager.Manager): # 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): + """ + Assigns a path to the given piece. + """ + if piece != None and path != None: + piece.set_move_along_tile_path(path) + def update_tile_cursor(self, surface = None): """ Update the tile cursor object. @@ -160,15 +167,6 @@ class PieceManager(manager.Manager): # ne.snap_to_tile() # self.add_entity(ne) - #def set_entity_move_to_tile_path(self, entity, path): - # """ - # Assigns a path to the selected entity. - # """ - # if entity != None and path != None: - # entity.set_move_along_tile_path(path) - # return True - # else: - # return False #def set_entity_move_to_tile_pos(self, entity, tile_pos): # """ diff --git a/src/subsystem.py b/src/subsystem.py @@ -37,14 +37,21 @@ class GameInterface(GameSubsystem): """ GameInterface handles all PyGame events, meaning it is responsible for all input and the responses - to that input. + to that input. GameInterface takes extra arguments + compared to other subsystems, representing its need + to directly access manager calls via bus and the + game camera. """ - def __init__(self, game): + def __init__(self, game, bus, camera): # Parent init super().__init__(game) + # Others + self.bus = bus + self.camera = camera + def handle_events(self, events): """ Handle any kind of PyGame event and react appropriately. @@ -69,8 +76,7 @@ class GameInterface(GameSubsystem): if self.game.control_mode == CTRL_MODES.Turn_Select_Move: if event.key == pygame.K_q: self.game.control_mode = CTRL_MODES.Turn_Normal - # TODO: Should this really be done here??? - self.game.board_manager.load_overlay() + self.bus.perform_load_board_overlay() def handle_key_release(self, event): """ @@ -82,29 +88,46 @@ class GameInterface(GameSubsystem): """ React to a mousebutton being clicked. """ - # TODO: ManagerBus will have a field day here... + # If we clicked the left mouse button... if event.button == 1: - mousepos = pygame.mouse.get_pos() - mousepos = (mousepos[0] - self.game.camera.camera_surface_offset[0], mousepos[1] - self.game.camera.camera_surface_offset[1]) + + # First, get important mouse positional info, namely unoffset mouse position and camera-offset mouse position + mouseraw = pygame.mouse.get_pos() + mousepos = (mouseraw[0] - self.camera.camera_surface_offset[0], mouseraw[1] - self.camera.camera_surface_offset[1]) + + # State checker + # Main menu mode behavior if self.game.state_mode == STATE_MODES.Main_Menu_Mode: + + # Normal main menu control if self.game.control_mode == CTRL_MODES.Main_Menu_Normal: - self.game.menu_manager.trigger_button_at_pos(mousepos) + self.bus.perform_trigger_button_at_pos(mousepos) + + # Battle mode behavior elif self.game.state_mode == STATE_MODES.Battle_Mode: + + # Normal in-battle turn control if self.game.control_mode == CTRL_MODES.Turn_Normal: - # TODO: Bussing - # NOTE: Functional change... - self.game.piece_manager.select_piece_with_tile_cursor() - if self.game.piece_manager.selected_piece != None: + self.bus.perform_select_piece_with_tile_cursor() + if self.bus.fetch_selected_piece() != None: self.game.control_mode = CTRL_MODES.Turn_Select_Move + + # Selecting a move for the active piece control elif self.game.control_mode == CTRL_MODES.Turn_Select_Move: - to_path = self.game.board_manager.get_path_by_previous_moves(self.game.entity_manager.selected_entity.tile_pos, self.game.board_manager.get_tile_pos_at_position(mousepos)) - # TODO: Decouple this and make it work with the modifications - if self.game.entity_manager.set_entity_move_to_tile_path(self.game.entity_manager.selected_entity, to_path): + to_path = self.bus.fetch_piece_path_by_previous_moves(self.bus.fetch_selected_entity_tile_pos(), + self.bus.fetch_tile_pos_by_screen_pos(mousepos)) + if to_path != None: + self.bus.perform_set_piece_move_along_tile_path(self.bus.fetch_selected_piece(), to_path) + # TODO: Not like this self.game.lose_control(len(to_path) * PIECE_MOVE_DELAY, CTRL_MODES.Turn_Normal) - self.game.board_manager.load_overlay() + self.bus.perform_load_board_overlay() + + # Still-scene mode behavior elif self.game.state_mode == STATE_MODES.Still_Scene_Mode: + + # Normal still-scene control if self.game.control_mode == CTRL_MODES.Still_Scene_Normal: - self.game.scene_manager.current_scene.continue_script() + self.bus.perform_continue_current_scene_script() def handle_mouse_release(self, event): """ @@ -121,15 +144,15 @@ class GameInterface(GameSubsystem): # Update cursor position if self.game.state_mode == STATE_MODES.Battle_Mode: mouseraw = pygame.mouse.get_pos() - mousepos = (mouseraw[0] - self.game.camera.camera_surface_offset[0], mouseraw[1] - self.game.camera.camera_surface_offset[1]) - tilepos = self.game.board_manager.get_tile_at_position(mousepos) + mousepos = (mouseraw[0] - self.camera.camera_surface_offset[0], mouseraw[1] - self.camera.camera_surface_offset[1]) + tilepos = self.bus.fetch_tile_at_screen_pos(mousepos) if tilepos != None: - self.game.entity_manager.position_tile_cursor(tilepos) + self.bus.perform_position_tile_cursor(tilepos) if self.game.control_mode == CTRL_MODES.Turn_Normal: - for r in self.game.camera.scroll_rects: - if self.game.camera.scroll_rects[r].collidepoint(mouseraw): - self.game.camera.move_offset(r, SCROLL_SPEED) + for r in self.camera.scroll_rects: + if self.camera.scroll_rects[r].collidepoint(mouseraw): + self.camera.move_offset(r, SCROLL_SPEED) #################################### # Section 3 - The ManagerBus class # @@ -165,8 +188,31 @@ class ManagerBus(GameSubsystem): return self.game.piece_manager.get_piece_by_tile(tile_pos) def fetch_selected_piece(self): return self.game.piece_manager.selected_piece + def fetch_selected_piece_tile_pos(self): + return self.game.piece_manager.selected_piece.tile_pos def fetch_tile_by_screen_pos(self, position): return self.game.board_manager.get_tile_by_position(position) + def fetch_tile_by_tile_pos(self, tile_pos): + return self.game.board_manager.get_tile_by_tile_pos(tile_pos) + def fetch_tile_pos_by_screen_pos(self, position): + 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) + + # Performs: All callable non-return behaviors of managers + # TODO: Error-checking would be one of the great additions possible here + def perform_load_board_overlay(self): + self.game.board_manager.load_overlay() + def perform_trigger_menu_button_at_pos(self, pos): + self.game.menu_manager.trigger_button_at_pos(pos) + def perform_select_piece_with_tile_cursor(self): + self.game.piece_manager.select_piece_with_tile_cursor() + def perform_position_tile_cursor(tile_pos): + self.game.entity_manager.position_tile_cursor(tile_pos) + def perform_set_piece_move_along_tile_path(self, piece, path): + self.game.piece_manager.set_piece_move_to_tile_path(piece, path) + def perform_continue_current_scene_script(): + self.game.scene_manager.current_scene.continue_script() #################################### # Section 4 - The GameCamera class #