2023-11-29 10:53:30 +00:00
|
|
|
import os
|
|
|
|
import pygame
|
|
|
|
|
|
|
|
from utils.resource_loader import import_assets
|
|
|
|
|
|
|
|
|
|
|
|
class Camera(pygame.sprite.Group):
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
|
|
|
|
# General Setup
|
|
|
|
self.display_surface = pygame.display.get_surface()
|
2023-12-14 17:28:45 +00:00
|
|
|
self.display_size = self.display_surface.get_size()
|
2023-11-29 10:53:30 +00:00
|
|
|
|
|
|
|
self.floor_surf = pygame.image.load(
|
|
|
|
import_assets(
|
|
|
|
os.path.join('graphics',
|
|
|
|
'tilemap',
|
|
|
|
'ground.png')
|
|
|
|
)
|
|
|
|
).convert()
|
|
|
|
|
|
|
|
self.floor_rect = self.floor_surf.get_rect(topleft=(0, 0))
|
|
|
|
|
2023-12-14 17:28:45 +00:00
|
|
|
self.calculate_scale()
|
2023-11-29 10:53:30 +00:00
|
|
|
|
2023-12-14 17:28:45 +00:00
|
|
|
def calculate_scale(self):
|
|
|
|
map_width, map_height = self.floor_rect.size
|
|
|
|
screen_width, screen_height = self.display_size
|
2023-11-29 10:53:30 +00:00
|
|
|
|
2023-12-14 17:28:45 +00:00
|
|
|
# Calculating the scale to fit the map on the screen
|
|
|
|
self.scale = min(screen_width / map_width, screen_height / map_height)
|
|
|
|
self.scaled_floor_surf = pygame.transform.scale(self.floor_surf,
|
|
|
|
(int(map_width * self.scale),
|
|
|
|
int(map_height * self.scale)))
|
|
|
|
self.scaled_floor_rect = self.scaled_floor_surf.get_rect()
|
2023-11-29 10:53:30 +00:00
|
|
|
|
2023-12-14 17:28:45 +00:00
|
|
|
def custom_draw(self):
|
|
|
|
# Drawing the scaled floor
|
|
|
|
self.display_surface.blit(
|
|
|
|
self.scaled_floor_surf, self.scaled_floor_rect.topleft)
|
2023-11-29 10:53:30 +00:00
|
|
|
|
2023-12-14 17:28:45 +00:00
|
|
|
for sprite in sorted(self.sprites(), key=lambda sprite: sprite.rect.centery if not hasattr(sprite, 'animation') else sprite.animation.rect.centery):
|
|
|
|
# Check for sprites with 'animation' attribute
|
2023-11-29 10:53:30 +00:00
|
|
|
if hasattr(sprite, 'animation'):
|
2023-12-14 17:28:45 +00:00
|
|
|
scaled_sprite_image = pygame.transform.scale(sprite.animation.image,
|
|
|
|
(int(sprite.animation.rect.width * self.scale),
|
|
|
|
int(sprite.animation.rect.height * self.scale)))
|
|
|
|
scaled_position = (int(sprite.animation.rect.x * self.scale),
|
|
|
|
int(sprite.animation.rect.y * self.scale))
|
|
|
|
for sprite in sorted(self.sprites(), key=lambda sprite: sprite.rect.centery if not hasattr(sprite, 'animation') else sprite.animation.rect.centery):
|
|
|
|
# Check for sprites with 'animation' attribute
|
|
|
|
if hasattr(sprite, 'animation'):
|
|
|
|
scaled_sprite_image = pygame.transform.scale(sprite.animation.image,
|
|
|
|
(int(sprite.animation.rect.width * self.scale),
|
|
|
|
int(sprite.animation.rect.height * self.scale)))
|
|
|
|
scaled_position = (int(sprite.animation.rect.x * self.scale),
|
|
|
|
int(sprite.animation.rect.y * self.scale))
|
2023-11-29 10:53:30 +00:00
|
|
|
else:
|
2023-12-14 17:28:45 +00:00
|
|
|
scaled_sprite_image = pygame.transform.scale(sprite.image,
|
|
|
|
(int(sprite.rect.width * self.scale),
|
|
|
|
int(sprite.rect.height * self.scale)))
|
|
|
|
scaled_position = (int(sprite.rect.x * self.scale),
|
|
|
|
int(sprite.rect.y * self.scale))
|
|
|
|
|
|
|
|
self.display_surface.blit(scaled_sprite_image, scaled_position)
|