Return to repo list

heart-of-gold

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

commit adf55165c2690a98545ec379fc465ea271b84505
parent 2402721f2d6423e48de05b807df68f758bb06841
Author: Erik Letson <hmagellan@hmagellan.com>
Date:   Tue, 27 Apr 2021 22:56:27 -0500

redo button controls to be list of bools

Diffstat:
Msrc/interface.py | 87++++++++++++++++++++++++++++++++++++++++++++-----------------------------------
1 file changed, 48 insertions(+), 39 deletions(-)

diff --git a/src/interface.py b/src/interface.py @@ -38,6 +38,7 @@ class GameInterface(subsystem.GameSubsystem): self.double_click_mousepos = None self.old_mousepos = None self.camera_motions = [0, 0] + self.key_bools = [ False for k in range(0, 350) ] def handle_events(self, events): """ @@ -59,42 +60,52 @@ class GameInterface(subsystem.GameSubsystem): """ React to a key being pressed. """ - # TODO: This is irregular compared to mouseclick. Desireable??? - if self.game.control_mode == CTRL_MODES.Turn_Normal: - if event.key == pygame.K_i: - self.bus.perform_turn_manager_display_stats(self.bus.fetch("turn_manager", "active_piece")) - self.game.control_mode = CTRL_MODES.Turn_Display_Stats - elif self.game.control_mode == CTRL_MODES.Turn_Display_Stats: - if event.key == pygame.K_i or event.key == pygame.K_q: - self.bus.perform_turn_manager_display_stats(None) - self.game.control_mode = CTRL_MODES.Turn_Normal - if self.game.control_mode in EXITABLE_TURN_MODES: - if event.key == pygame.K_q: - self.game.control_mode = CTRL_MODES.Turn_Normal - self.bus.perform_load_board_overlay() - if self.game.control_mode in SCROLLABLE_TURN_MODES: - if event.key == pygame.K_w or event.key == pygame.K_UP and self.camera_motions[1] < 1: - self.camera_motions[1] += 1 - elif event.key == pygame.K_s or event.key == pygame.K_DOWN and self.camera_motions[1] > -1: - self.camera_motions[1] -= 1 - elif event.key == pygame.K_a or event.key == pygame.K_LEFT and self.camera_motions[0] < 1: - self.camera_motions[0] += 1 - elif event.key == pygame.K_d or event.key == pygame.K_RIGHT and self.camera_motions[0] > -1: - self.camera_motions[0] -= 1 + if event.key < len(self.key_bools): + self.key_bools[event.key] = True def handle_key_release(self, event): """ React to a key being released. """ - if self.game.control_mode in SCROLLABLE_TURN_MODES: - if event.key == pygame.K_w or event.key == pygame.K_UP and self.camera_motions[1] > 0: - self.camera_motions[1] -= 1 - elif event.key == pygame.K_s or event.key == pygame.K_DOWN and self.camera_motions[1] < 0: - self.camera_motions[1] += 1 - elif event.key == pygame.K_a or event.key == pygame.K_LEFT and self.camera_motions[0] > 0: - self.camera_motions[0] -= 1 - elif event.key == pygame.K_d or event.key == pygame.K_RIGHT and self.camera_motions[0] < 0: - self.camera_motions[0] += 1 + if event.key < len(self.key_bools): + self.key_bools[event.key] = False + + def react_to_keys(self): + """ + React to certain pressed/not-pressed statuses + of keys on a mode-by-mode basis. Called during + update. + """ + # BATTLE REACTIONS + if self.game.state_mode == STATE_MODES.Battle_Mode: + + # Handle opening the stat display dialog with the keys + if self.game.control_mode == CTRL_MODES.Turn_Normal: + if self.key_bools[105]: + self.bus.perform_turn_manager_display_stats(self.bus.fetch("turn_manager", "active_piece")) + self.game.control_mode = CTRL_MODES.Turn_Display_Stats + self.key_bools[105] = False + self.key_bools[113] = False + + # Handle exiting the stat display + elif self.game.control_mode == CTRL_MODES.Turn_Display_Stats: + if self.key_bools[105] or self.key_bools[113]: + self.bus.perform_turn_manager_display_stats(None) + self.game.control_mode = CTRL_MODES.Turn_Normal + self.key_bools[105] = False + self.key_bools[113] = False + + # Handle exiting in exitable modes with the keys + if self.game.control_mode in EXITABLE_TURN_MODES: + if self.key_bools[113]: + self.game.control_mode = CTRL_MODES.Turn_Normal + self.bus.perform_load_board_overlay() + + # Handle scrolling the camera with the keys + if self.game.control_mode in SCROLLABLE_TURN_MODES: + mx = max(int(self.key_bools[97]), int(self.key_bools[276])) - max(int(self.key_bools[100]), int(self.key_bools[275])) + my = max(int(self.key_bools[119]), int(self.key_bools[273])) - max(int(self.key_bools[115]), int(self.key_bools[274])) + self.camera_motions = [mx, my] def handle_mouse_click(self, event): """ @@ -232,11 +243,13 @@ class GameInterface(subsystem.GameSubsystem): else: self.double_clicking = False + # React to keys + self.react_to_keys() + # BASE MODE ONLY UPDATES # Update tile cursor if self.game.state_mode == STATE_MODES.Base_Mode: tilepos = self.bus.check_for_tile_by_screen_pos(mousepos) - if tilepos != None: self.bus.perform_base_manager_position_tile_cursor((tilepos[0], tilepos[1])) @@ -263,15 +276,11 @@ class GameInterface(subsystem.GameSubsystem): ap.set_animation(ap.sheet.animations["stand_" + ap.facing.name], True) # Position cursor in scrollable modes and/or scroll - if self.game.control_mode in SCROLLABLE_TURN_MODES and tilepos != None: - self.bus.perform_piece_manager_position_tile_cursor((tilepos[0], tilepos[1])) - + if self.game.control_mode in SCROLLABLE_TURN_MODES: if self.camera_motions != (0, 0): - if self.camera_motions[0] > 1 or self.camera_motions[0] < -1: - self.camera_motions[0] = 0 - if self.camera_motions[1] > 1 or self.camera_motions[1] < -1: - self.camera_motions[1] = 0 self.camera.move_offset(self.camera_motions, SCROLL_SPEED) + if tilepos != None: + self.bus.perform_piece_manager_position_tile_cursor((tilepos[0], tilepos[1])) # Watching a move complete elif self.game.control_mode == CTRL_MODES.Turn_Watch_Move: