Return to repo list

heart-of-gold

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

commit 290b8cb93f25bccbdf6046448653d4ca80957250
parent 131afddf3449c6d115d9393edd581b415ce94179
Author: Erik Letson <hmagellan@hmagellan.com>
Date:   Tue, 10 Nov 2020 13:09:48 -0600

Movement restored 110%. Refactor Complete!

Diffstat:
Msrc/board.py | 6------
Msrc/entity.py | 14++++++++------
Msrc/piece.py | 96+++++++++++++++++++------------------------------------------------------------
Msrc/subsystem.py | 4+++-
4 files changed, 34 insertions(+), 86 deletions(-)

diff --git a/src/board.py b/src/board.py @@ -6,12 +6,6 @@ from .constants import BOARD_PATH # board.py # ############ -# TODO: -# 1. BoardManager should do the things GameInterface does with regards -# to frame-by-frame board interaction. This file should not require -# so much refactoring as vgo.py to bring the new implementation of -# managers into compliance here. - # This file contains: # 1. The BoardManager class, which manages boards and swaps between them # 2. The Board class, which represents a grid of Tile objects and is the play area diff --git a/src/entity.py b/src/entity.py @@ -106,12 +106,13 @@ class Entity(pygame.sprite.DirtySprite): def set_motion(self, target_pos, speed): """ Assign a new motion to this Entity and start playing it. - Unlike animations, motions must be played if assigned. + Unlike animations, motions must be played if assigned. Note: + Motions go by rect center, rather than topleft. """ self.motion = { "target" : target_pos, "speed" : speed, - "direction" : ((target_pos[0] - self.rect.topleft[0]) / speed, (target_pos[1] - self.rect.topleft[1]) / speed), + "direction" : ((target_pos[0] - self.rect.center[0]) / speed, (target_pos[1] - self.rect.center[1]) / speed), "timer" : speed } @@ -146,13 +147,14 @@ class Entity(pygame.sprite.DirtySprite): def motion_move(self): """ - Perform current motion, if it is set. + Perform current motion, if it is set. Note: Motions go by + rect center, rather than topleft. """ if self.motion != {} and not self.rect.collidepoint(self.motion["target"]): if self.motion_timer == 0: - mx = self.rect.topleft[0] + self.motion["direction"][0] - my = self.rect.topleft[1] + self.motion["direction"][1] - self.rect.topleft = (mx, my) + mx = self.rect.center[0] + self.motion["direction"][0] + my = self.rect.center[1] + self.motion["direction"][1] + self.rect.center = (mx, my) self.motion_timer = self.motion["speed"] else: self.motion_timer -= 1 diff --git a/src/piece.py b/src/piece.py @@ -164,41 +164,6 @@ class PieceManager(manager.Manager): if surface != None: self.pieces.update(surface) - - #def set_entity_move_to_tile_pos(self, entity, tile_pos): - # """ - # Sets the entity to move to the given tile_pos. A 'move' - # is an assignment from the EntityManager. A 'motion' is - # a setting similar to an animation (but for spatial travel) - # for an entity. - # """ - # # TODO: Set speed of motion somehow, maybe calculated from MOVE of entity - # if entity != None and tile_pos != None: - # entity.set_motion((tile_pos[0] * self.game.board_manager.current_board.tmx_data.tilewidth, tile_pos[1] * self.game.board_manager.current_board.tmx_data.tileheight), 6) - # entity.tile_pos = tile_pos - # return True - # else: - # return False - - #def select_entities_with_tile_cursor(self, tile_def): - # """ - # Select the current entity under the tile cursor by way - # of the tile_def. Returns True if an entity is selected, - # and False otherwise. - # """ - # # TODO: Eventually this should return info about the team of the selected - # # entity so that GameInterface can make control mode decisions. - # e = self.get_entities_by_tile(tile_def) - # if e != [] and e[0].team == "Player": - # self.selected_entity = e[0] # Select only the topmost ent on this tile - # self.game.board_manager.display_as_move_range(self.get_entity_legal_move_tile_pos(self.selected_entity)) - # return True - # else: - # self.selected_entity = None - # self.game.board_manager.load_overlay() - # return False - - ############################### # Section 2 - The Piece class # ############################### @@ -233,6 +198,7 @@ class Piece(entity.Entity): self.back_to_stand = False # TODO: This may not be the best way self.current_tile_path = [] self.current_tile_path_index = 0 + self.through_tile_pos = (-1, -1) self.path_moving = False def set_facing(self, direction): @@ -242,34 +208,6 @@ class Piece(entity.Entity): """ self.facing = direction - def set_motion(self, target_pos, speed): - """ - Overwrite of the basic Entity version to support facing and - animating automatically. - """ - super().set_motion(target_pos, speed) - - # Decide facing - # TODO: This should be determined by the last move in the path instead - mx = target_pos[0] - self.rect.topleft[0] - my = target_pos[1] - self.rect.topleft[1] - a = math.atan2(mx, my) * 180 / math.pi - if a >= -67.5 and a < 0: - self.facing = FACE_DIR.D - elif a >= 0 and a < 112.5: - self.facing = FACE_DIR.R - elif a > 112.5 or a < -112.5: - self.facing = FACE_DIR.U - else: - self.facing = FACE_DIR.L - - # Set a movement animation - # TODO: It may be neccessary in the future to indicate whether or not this - # 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(self.sheet.name, "walk_" + self.facing.name), True) - def set_move_along_tile_path(self, tile_seq): """ Move along a sequence of tiles in order. @@ -277,14 +215,7 @@ class Piece(entity.Entity): self.current_tile_path = tile_seq self.current_tile_path_index = 0 self.path_moving = True - - # Set facing values - #c = None - #for t in tile_seq: - # if c != None: - # if tile_seq[t][0] < tile_seq[c][0]: - # - # c = t + self.through_tile_pos = self.tile_pos def execute_tile_path_move(self): """ @@ -292,14 +223,33 @@ class Piece(entity.Entity): """ if self.current_tile_path != None and self.path_moving: if self.motion == {} and self.current_tile_path_index < len(self.current_tile_path): + + # Decide facing: + face_diff = (self.through_tile_pos[0] - self.current_tile_path[self.current_tile_path_index][0], + self.through_tile_pos[1] - self.current_tile_path[self.current_tile_path_index][1]) + oldface = self.facing + if face_diff == (1, 0): + self.facing = FACE_DIR.L + elif face_diff == (-1, 0): + self.facing = FACE_DIR.R + elif face_diff == (0, 1): + self.facing = FACE_DIR.U + elif face_diff == (0, -1): + self.facing = FACE_DIR.D + + if self.facing != oldface: + self.set_animation(self.manager.bus.fetch_animation(self.sheet.name, "walk_" + self.facing.name), True) + + # Setup motion to next tile in the path next_tar = ((self.current_tile_path[self.current_tile_path_index][0] * TILE_WIDTH) + (TILE_WIDTH / 2), (self.current_tile_path[self.current_tile_path_index][1] * TILE_HEIGHT) + (TILE_HEIGHT / 2)) self.set_motion(next_tar, PIECE_MOVE_SPEED) - #anim = None - #if next_tar[ + self.through_tile_pos = self.current_tile_path[self.current_tile_path_index] self.current_tile_path_index += 1 + elif self.motion == {}: self.tile_pos = self.current_tile_path[self.current_tile_path_index - 1] + self.through_tile_pos = (-1, -1) self.path_moving = False self.current_tile_path = [] self.back_to_stand = True diff --git a/src/subsystem.py b/src/subsystem.py @@ -178,7 +178,7 @@ class ManagerBus(GameSubsystem): # Parent init super().__init__(game) - # Fetches: All return values from managers + # Fetches: All return values from managers and other subsystems def fetch_sheet(self, name): return self.game.sheet_manager.loaded_sheets[name] def fetch_animation(self, sheet, anim): @@ -209,6 +209,8 @@ class ManagerBus(GameSubsystem): return self.game.board_manager.current_board.tmx_data.height else: return None + def fetch_camera_offset(self): + return self.game.camera.camera_surface_offset # Performs: All callable non-return behaviors of managers # TODO: Error-checking would be one of the great additions possible here