Return to repo list

heart-of-gold

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

commit 4ce18f339f53c692a90060cd355af8623766ff77
parent 7893889d9558e03129c71e0797e3a85731a06a6a
Author: Erik Letson <hmagellan@hmagellan.com>
Date:   Thu, 12 Nov 2020 13:50:51 -0600

Camera scroll improvement, also work on turns

Diffstat:
Msrc/camera.py | 8++++----
Msrc/interface.py | 2+-
Msrc/piece.py | 5+++++
Msrc/turn.py | 78++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------
4 files changed, 72 insertions(+), 21 deletions(-)

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 / 4, SCREEN_HEIGHT), - (-1, 0) : pygame.Rect(SCREEN_WIDTH - (SCREEN_WIDTH / 4), 0, SCREEN_WIDTH / 4, SCREEN_HEIGHT), - (0, 1) : pygame.Rect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT / 4), - (0, -1) : pygame.Rect(0, SCREEN_HEIGHT - (SCREEN_HEIGHT / 4), SCREEN_WIDTH, SCREEN_HEIGHT / 4) + (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) } def move_offset(self, rel_offset, speed_multiple = 1): diff --git a/src/interface.py b/src/interface.py @@ -134,6 +134,6 @@ class GameInterface(subsystem.GameSubsystem): if self.game.control_mode == CTRL_MODES.Turn_Normal: for r in self.camera.scroll_rects: - if self.camera.scroll_rects[r].collidepoint(mouseraw): + if self.camera.scroll_rects[r].collidepoint(mouseraw) and pygame.mouse.get_focused(): self.camera.move_offset(r, SCROLL_SPEED) diff --git a/src/piece.py b/src/piece.py @@ -212,6 +212,11 @@ class Piece(entity.Entity): self.through_tile_pos = (-1, -1) self.path_moving = False + # Turn order values + self.readiness = self.active_stats["INIT"] + self.active_stats["SPD"] + self.taking_turn = False + self.haste_mod = 3 # 3 - normal, 1 - minimum (slow 2), 5 - maximum (haste 2) + def set_facing(self, direction): """ Set the direction this piece should face. 'direction' should diff --git a/src/turn.py b/src/turn.py @@ -8,7 +8,7 @@ from .constants import * # This file contains the following: # 1. The TurnManager class, which handles turn order and displaying turn info in the GUI -# 2. The TurnTray Entity subclass, which represents the +# 2. The TurnTray Entity subclass, which represents the tray where turn information is reported onscreen ################################# # Section 1 - TurnManager class # @@ -27,31 +27,77 @@ class TurnManager(manager.Manager): super().__init__(game) # Important values - self.current_turn_number = 1 + self.current_turn = 0 + self.current_tick = 0 self.current_active_piece = None - self.current_turn_order = [] + self.turn_projection = [] + self.in_play_pieces = [] + self.turn_depth = 10 + + # Turn tray and other associated entities self.turn_tray = None - self.turn_depth = 10 # How many turns to calculate in advance - self.initiative_scores = {} + self.turn_icons = [] def initialize_turns(self, pieces): """ Load up the turns for the first time. """ - self.initiative_scores = { p.name : 0 for p in self.game.entity_manager.pieces } + self.in_play_pieces = pieces + self.shift_turn() - def calculate_turn_order(self): + def shift_turn(self): + """ + Shift to the next turn. + """ + # First, clean up the active piece + if self.current_active_piece != None: + self.current_active_piece.taking_turn = False + + # Next, set up the values we need + self.current_turn += 1 + candidates = [] + np = None + + # Generate a list of next turn candidates + while len(candidates) == 0: + self.current_tick += 1 + for p in self.in_play_pieces: + p.readiness += p.active_stats["INIT"] + min(1, (p.active_stats["SPD"] // 3)) + if p.readiness >= 100: + candidates.add(p) + + # Sort the candidates by highest readiness, taking the 0th candidate afterward + # as the presumptive next turn piece + candidates.sort(lambda can: can.readiness) + np = candidates[0] + + # In the event that more than one piece has equal readiness at the top of the + # list, let the piece with the higest speed go first + for c in candidates: + if c.readiness >= np.readiness and c.active_stats["SPD"] > np.active_stats["SPD"]: + np = c + + # Set np as the current active piece and let it take a turn + self.current_active_piece = np + np.taking_turn = True + np.readiness -= 100 + + def project_turn_order(self): """ - Calculate turn order based on Piece stats. + Make a projection of the assumed turn order. + Fills up a number of entries equal to the + turn_depth value (accounting for that many + projected turns). """ - self.current_turn_order = [] - while len(self.current_turn_order) - 1 < self.turn_depth: - for p in self.initiative_scores: - if self.initiative_scores[p] >= 100: - self.current_turn_order.append(p) - workent = self.game.entity_manager.get_entity_by_name(p) - result = workent.active_stats["INIT"] + int(workent.active_stats["SPD"] / 10) - self.initiative_scores[p] += result + pt = self.current_tick + self.turn_projection = [] + rds = { self.in_play_pieces[i] : self.in_play_pieces[i].readiness for i in self.in_play_pieces } + while len(projection) - 1 < self.turn_depth: + for p in rds: + rds[p] += p.active_stats["INIT"] + min(1, (p.active_stats["SPD"] // 3)) + if rds[p] >= 100: + self.turn_projection.add[p] + rds[p] -= 100 def update_turn_entities(self, surface): """