/
Vibek
/
First_Game
Обзор
Документация
Войти
/
Vibek
/
First_Game
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
resources/asset_manager.py
79 строк
3 KB
Vibek
все перменные перенесены в конфиг, отрисовка динамическая
21 июн 2026, 20:32
21 июн 2026, 20:32
f32a834
Код
Авторство
О чём код?
"""Загрузка и кэширование графических ресурсов (Infrastructure).""" from pathlib import Path import pygame from core.config import Config from core.paths import PROJECT_ROOT from resources.arena_loader import ArenaLoader from resources.entity_profile import EntityProfile, SpriteLayout from resources.profile_loader import ProfileLoader from resources.sprite_processor import SpriteProcessor class AssetManager: """ Единая точка доступа к профилям и подготовленным спрайтам. View-слой зависит от этого класса, Model — только от EntityProfile/HitboxDef. """ _PROFILE_NAMES = ("player", "scout", "stormtrooper", "sword", "projectile") def __init__(self, root: Path | None = None) -> None: self._root = root or PROJECT_ROOT self._profiles: dict[str, EntityProfile] = {} self._sprites: dict[str, pygame.Surface] = {} self._arena_background: pygame.Surface | None = None def load_all(self) -> None: assets_dir = self._root / "assets" for name in self._PROFILE_NAMES: profile = ProfileLoader.load(assets_dir / "data" / f"{name}.json") self._profiles[name] = profile for sprite_key, relative_path in profile.sprites.items(): cache_key = self._sprite_cache_key(name, sprite_key) self._sprites[cache_key] = self._load_scaled_sprite( assets_dir / relative_path, profile.sprite, ) self._load_arena_background(assets_dir) def get_arena_background(self) -> pygame.Surface: if self._arena_background is None: raise RuntimeError("Arena background is not loaded.") return self._arena_background def get_profile(self, name: str) -> EntityProfile: profile = self._profiles.get(name) if profile is None: raise KeyError(f"Profile '{name}' is not loaded.") return profile def get_sprite(self, profile_name: str, sprite_key: str) -> pygame.Surface: cache_key = self._sprite_cache_key(profile_name, sprite_key) sprite = self._sprites.get(cache_key) if sprite is None: raise KeyError(f"Sprite '{profile_name}:{sprite_key}' is not loaded.") return sprite def _load_arena_background(self, assets_dir: Path) -> None: background_path = ArenaLoader.load_background_path(assets_dir / "data" / "arena.json") surface = pygame.image.load(str(assets_dir / background_path)) if pygame.display.get_surface() is not None: surface = surface.convert() target_size = (Config.Design.WIDTH, Config.Design.HEIGHT) self._arena_background = pygame.transform.smoothscale(surface, target_size) @staticmethod def _sprite_cache_key(profile_name: str, sprite_key: str) -> str: return f"{profile_name}:{sprite_key}" @staticmethod def _load_scaled_sprite(path: Path, layout: SpriteLayout) -> pygame.Surface: surface = pygame.image.load(str(path)) surface = SpriteProcessor.prepare(surface, layout) if layout.scale == 1.0: return surface width, height = surface.get_size() scaled_size = (max(1, int(width * layout.scale)), max(1, int(height * layout.scale))) return pygame.transform.smoothscale(surface, scaled_size)