Return to repo list

heart-of-gold

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

commit b91d511cc86d48e5c2a1750ce355c7fce93c20d7
parent da2ee7b3a66c28869faf2a9b3393a1b25e1e1470
Author: Erik Letson <hmagellan@hmagellan.com>
Date:   Sat, 14 Nov 2020 22:26:10 -0600

Buttons are functioning in turn manager ui

Diffstat:
Adata/img/toggle_more_turn_button_1.png | 0
Adata/img/turn_order_tray_extended_1.png | 0
Mdata/json/sheets.json | 10++++++++++
Msrc/bus.py | 2++
Msrc/camera.py | 8++++----
Msrc/entity.py | 15++++++++++++++-
Msrc/game.py | 2+-
Msrc/interface.py | 4++++
Msrc/turn.py | 87++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------
9 files changed, 115 insertions(+), 13 deletions(-)

diff --git a/data/img/toggle_more_turn_button_1.png b/data/img/toggle_more_turn_button_1.png Binary files differ. diff --git a/data/img/turn_order_tray_extended_1.png b/data/img/turn_order_tray_extended_1.png Binary files differ. diff --git a/data/json/sheets.json b/data/json/sheets.json @@ -89,9 +89,19 @@ "dimensions" : [135, 128], "total_sprites" : 1 }, + "turn_order_tray_extended_1" : { + "filename" : "turn_order_tray_extended_1.png", + "dimensions" : [135, 320], + "total_sprites" : 1 + }, "turn_icon_holder_1" : { "filename" : "turn_icon_holder_1.png", "dimensions" : [128, 28], "total_sprites" : 1 + }, + "toggle_more_turn_button_1" : { + "filename" : "toggle_more_turn_button_1.png", + "dimensions" : [135, 14], + "total_sprites" : 2 } } diff --git a/src/bus.py b/src/bus.py @@ -118,3 +118,5 @@ class ManagerBus(subsystem.GameSubsystem): self.game.board_manager.display_as_move_range(self.game.piece_manager.get_piece_max_legal_move(self.game.piece_manager.selected_piece)) def perform_shift_turns(self): self.game.turn_manager.shift_turns() + def perform_trigger_turn_manager_buttons_at_pos(self, pos): + self.game.turn_manager.trigger_button_at_pos(pos) diff --git a/src/camera.py b/src/camera.py @@ -42,10 +42,10 @@ class GameCamera(subsystem.GameSubsystem): self.camera_surface_rect = self.camera_surface.get_rect() self.camera_surface_offset = init_offset self.scroll_rects = { - (1, 0) : pygame.Rect(0, 0, SCREEN_WIDTH / 8, SCREEN_HEIGHT), - (-1, 0) : pygame.Rect(SCREEN_WIDTH - (SCREEN_WIDTH / 8), 0, SCREEN_WIDTH / 8, SCREEN_HEIGHT), - (0, 1) : pygame.Rect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT / 8), - (0, -1) : pygame.Rect(0, SCREEN_HEIGHT - (SCREEN_HEIGHT / 8), SCREEN_WIDTH, SCREEN_HEIGHT / 8) + (1, 0) : pygame.Rect(0, 0, SCREEN_WIDTH / 10, SCREEN_HEIGHT), + (-1, 0) : pygame.Rect(SCREEN_WIDTH - (SCREEN_WIDTH / 10), 0, SCREEN_WIDTH / 10, SCREEN_HEIGHT), + (0, 1) : pygame.Rect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT / 10), + (0, -1) : pygame.Rect(0, SCREEN_HEIGHT - (SCREEN_HEIGHT / 10), SCREEN_WIDTH, SCREEN_HEIGHT / 10) } def move_offset(self, rel_offset, speed_multiple = 1): diff --git a/src/entity.py b/src/entity.py @@ -31,6 +31,7 @@ class Entity(pygame.sprite.DirtySprite): # Saved values self.sheet = sheet + self.sprite = sprite self.image = sheet.sprites[sprite] self.rect = self.image.get_rect() self.rect.topleft = (0, 0) @@ -57,6 +58,17 @@ class Entity(pygame.sprite.DirtySprite): self.tile_pos = (-1, -1) self.tile_gid = None + def set_image(self, sheet, sprite = (0, 0)): + """ + Set the image of the Entity to a new image. + """ + self.sheet = sheet + self.sprite = sprite + self.image = sheet.sprites[sprite] + old_tp = self.rect.topleft + self.rect = self.image.get_rect() + self.rect.topleft = old_tp + def set_sprite(self, sprite): """ Set the Entity's sprite to another one on the board. @@ -65,7 +77,8 @@ class Entity(pygame.sprite.DirtySprite): Entity objects. """ if sprite in self.sheet.sprites.keys(): - self.image = self.sheet.sprites[sprite_coord] + self.image = self.sheet.sprites[sprite] + self.sprite = sprite def set_animation(self, new_animation, play = False, init_frame = 0): """ diff --git a/src/game.py b/src/game.py @@ -55,7 +55,7 @@ class Game(object): self.board_manager = board.BoardManager(self, self.manager_bus, "board_manager") self.piece_manager = piece.PieceManager(self, self.manager_bus, "piece_manager") self.scene_manager = scene.SceneManager(self, self.manager_bus, "scene_manager") - self.turn_manager = turn.TurnManager(self, self.manager_bus, "turn_manager") + self.turn_manager = turn.TurnManager(self, self.manager_bus, "turn_manager", self.camera) # Setup (This is WIP) self.sheet_manager.load_sheets_from_json("sheets.json") diff --git a/src/interface.py b/src/interface.py @@ -93,6 +93,10 @@ class GameInterface(subsystem.GameSubsystem): if sp != None and sp.taking_turn: self.bus.perform_display_move_range_of_piece(sp) self.game.control_mode = CTRL_MODES.Turn_Select_Move + + # Else, see if we clicked a UI button + else: + self.bus.perform_trigger_turn_manager_buttons_at_pos(mouseraw) # Selecting a move for the active piece control elif self.game.control_mode == CTRL_MODES.Turn_Select_Move: diff --git a/src/turn.py b/src/turn.py @@ -10,6 +10,8 @@ from .constants import * # 1. The TurnManager class, which handles turn order and displaying turn info in the GUI # 2. The TurnTray Entity subclass, which represents the tray where turn information is reported onscreen +# TODO: TurnManager takes the camera as an argument. Should all managers??? + ################################# # Section 1 - TurnManager class # ################################# @@ -21,12 +23,13 @@ class TurnManager(manager.Manager): info about the current turn in the UI. """ - def __init__(self, game, bus, name): + def __init__(self, game, bus, name, camera): # Parent initialization super().__init__(game, bus, name) # Important values + self.camera = camera self.current_turn = 1 self.current_active_piece = None #self.turn_projection = [] @@ -37,6 +40,7 @@ class TurnManager(manager.Manager): # Turn tray and other associated entities self.turn_tray = None + self.turn_tray_more_button = None self.turn_icons = [] def initialize_turns(self, pieces): @@ -44,9 +48,12 @@ class TurnManager(manager.Manager): Load up the turns for the first time. """ self.in_play_pieces = pieces - self.turn_tray = TurnTray(self.bus.fetch("sheet_manager", "sheets")["turn_order_tray_1"]) + self.turn_tray = TurnTray(self.bus.fetch("sheet_manager", "sheets")["turn_order_tray_1"], (0, 0), None, False, + self.bus.fetch("sheet_manager", "sheets")["turn_order_tray_extended_1"]) + self.turn_tray_more_button = TurnTrayMoreButton(self.bus.fetch("sheet_manager", "sheets")["toggle_more_turn_button_1"], (0, 0), None, False, self) # TODO: Probably shouldn't be hardcoded - self.turn_tray.set_position((898, 28)) + self.turn_tray.set_position((798, 28)) + self.turn_tray_more_button.set_position((798, self.turn_tray.rect.bottom + 2)) self.turn_icons = [] self.turn_projection = [] self.former_candidates = [] @@ -143,14 +150,40 @@ class TurnManager(manager.Manager): ni = TurnIcon(self.bus.fetch("sheet_manager", "sheets")["turn_icon_holder_1"], (0, 0), None, False, self, piece, index) self.turn_icons.append(ni) + def trigger_button_at_pos(self, pos): + """ + Trigger a button at the given pos, if any. The + TurnManager buttons do not use the effect + interface. + """ + # TODO: Should this use the effect interface??? + if self.turn_tray_more_button != None: + if self.turn_tray_more_button.rect.collidepoint(pos): + self.turn_tray_more_button.toggle_more() + self.turn_tray.toggle_sheet(self.turn_tray_more_button.more) + self.turn_tray_more_button.set_position((798, self.turn_tray.rect.bottom + 2)) + for i in self.turn_icons: + if i.rect.collidepoint(pos) and i.clickable: + self.camera.lock_to_position(i.piece.rect.center) + break + def update_managed(self, surface): """ Update the entities this manager controls. """ if surface != None: self.turn_tray.update(surface) - for i in self.turn_icons: - i.update(surface) + self.turn_tray_more_button.update(surface) + if self.turn_tray_more_button.more: + for i in self.turn_icons: + i.update(surface) + i.clickable = True + else: + for i in self.turn_icons: + i.clickable = False + for j in range(0, 4): + self.turn_icons[j].update(surface) + self.turn_icons[j].clickable = True ############################# # Section 2 - Turn Entities # @@ -165,10 +198,24 @@ class TurnTray(entity.Entity): it can grow to show more turns. """ - def __init__(self, sheet, sprite = (0, 0), animation = None, animated = False): + def __init__(self, sheet, sprite = (0, 0), animation = None, animated = False, + more_sheet = None): # Parent initialization super().__init__(sheet, sprite, animation, animated) + + # Other values + self.less_sheet = sheet + self.more_sheet = more_sheet + + def toggle_sheet(self, more): + """ + Toggle the sheet from long to short. + """ + if more: + self.set_image(self.more_sheet) + else: + self.set_image(self.less_sheet) class TurnIcon(entity.Entity): """ @@ -192,8 +239,9 @@ class TurnIcon(entity.Entity): self.font = pygame.font.Font(os.path.join(FONT_PATH, UI_FONT), 12) self.index = index # TODO: Set this in settings??? Should at least dynamically derive y multiple from screen size - self.set_position((900, 32 + (32 * index))) + self.set_position((800, 32 + (31 * index))) self.piece_picture.set_position(self.rect.topleft) + self.clickable = False def update(self, surface = None): """ @@ -202,3 +250,28 @@ class TurnIcon(entity.Entity): """ super().update(surface) self.piece_picture.update(surface) + +class TurnTrayMoreButton(entity.Entity): + """ + Class representing the small button that is + positioned at the bottom of the turn tray that + can be clicked to toggle off and on the more + turns display. + """ + + def __init__(self, sheet, sprite = (0, 0), animation = None, animated = False, + manager = None): + + # Parent initialization + super().__init__(sheet, sprite, animation, animated) + + # Other values + self.manager = manager + self.more = False + + def toggle_more(self): + """ + Toggle the more setting. + """ + self.more = not self.more + self.set_sprite((0, abs(self.sprite[1] - 1)))