commit 0f7d7113d56d39304548fd023abc5b3f4ded8948
parent 52f21499b0b8e2318bf67d06c34d29983288b547
Author: Erik Letson <hmagellan@hmagellan.com>
Date: Fri, 30 Oct 2020 17:33:39 -0500
Camera movement implemented
Diffstat:
3 files changed, 30 insertions(+), 5 deletions(-)
diff --git a/src/constants.py b/src/constants.py
@@ -26,6 +26,7 @@ COLORKEY = (255, 0, 255)
# Other constants
PIECE_MOVE_SPEED = 2
PIECE_MOVE_DELAY = PIECE_MOVE_SPEED * 4
+SCROLL_SPEED = 3
# File paths
DATA_PATH = os.path.join(os.getcwd(), "data")
diff --git a/src/game.py b/src/game.py
@@ -143,7 +143,8 @@ class Game(object):
self.scene_manager.update_scene(self.camera.camera_surface)
# Draw the camera to the screen
- self.screen.blit(self.camera.camera_surface, self.camera.camera_surface_offset)
+ #self.screen.blit(self.camera.camera_surface, self.camera.camera_surface_offset)
+ self.camera.update_camera(self.screen)
# State_Mode agnostic, Control_Mode specific actions
if self.control_mode == CTRL_MODES.No_Control:
diff --git a/src/subsystem.py b/src/subsystem.py
@@ -55,6 +55,8 @@ class GameInterface(GameSubsystem):
self.handle_key_release(event)
elif event.type == pygame.MOUSEBUTTONDOWN:
self.handle_mouse_click(event)
+ elif event.type == pygame.MOUSEBUTTONUP:
+ self.handle_mouse_release(event)
elif event.type == pygame.QUIT:
self.game.quit_game()
@@ -93,7 +95,6 @@ class GameInterface(GameSubsystem):
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))
if self.game.entity_manager.set_entity_move_to_tile_path(self.game.entity_manager.selected_entity, to_path):
- # TODO: The length of time shouldn't have magic numbers. Neither should unit visual movement speed.
self.game.lose_control(len(to_path) * PIECE_MOVE_DELAY, CTRL_MODES.Turn_Normal)
self.game.board_manager.load_overlay()
elif self.game.state_mode == STATE_MODES.Still_Scene_Mode:
@@ -114,12 +115,17 @@ class GameInterface(GameSubsystem):
"""
# Update cursor position
if self.game.state_mode == STATE_MODES.Battle_Mode:
- mousepos = pygame.mouse.get_pos()
- mousepos = (mousepos[0] - self.game.camera.camera_surface_offset[0], mousepos[1] - self.game.camera.camera_surface_offset[1])
+ 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)
if tilepos != None:
self.game.entity_manager.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)
+
####################################
# Section 3 - The ManagerBus class #
####################################
@@ -167,8 +173,9 @@ class GameCamera(GameSubsystem):
self.camera_surface = pygame.Surface((0, 0)).convert() # This surface is initialized whenever a new map or other game area is loaded
self.camera_surface_rect = self.camera_surface.get_rect()
self.camera_surface_offset = (0, 0) # The rect topleft is never changed; rather, this value is used by game to draw the camera surface at an offset
+ self.scroll_rects = {(1, 0) : None, (-1, 0) : None, (0, 1) : None, (0, -1) : None}
- def load_camera_surface(self, dimensions, init_offset = (300, 0)):
+ def load_camera_surface(self, dimensions, init_offset = (0, 0)):
"""
Load up the camera surface as a new PyGame
surface object of the necessary size. Also
@@ -177,6 +184,22 @@ class GameCamera(GameSubsystem):
self.camera_surface = pygame.Surface(dimensions).convert()
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)
+ }
+
+ def move_offset(self, rel_offset, speed_multiple = 1):
+ """
+ Move the offset by the given relative offset.
+ Speed can also be increased or decreased by
+ passing a multiple here.
+ """
+ changex = rel_offset[0] * speed_multiple
+ changey = rel_offset[1] * speed_multiple
+ self.camera_surface_offset = (self.camera_surface_offset[0] + changex, self.camera_surface_offset[1] + changey)
def update_camera(self, surface = None):
"""