Use a ResourceManager to find assets
This commit is contained in:
		| @@ -2,6 +2,7 @@ from typing import List | ||||
|  | ||||
| from squirrelbattle.menus import Menu, MainMenu | ||||
| from .display import Display | ||||
| from ..resources import ResourceManager | ||||
|  | ||||
|  | ||||
| class MenuDisplay(Display): | ||||
| @@ -74,7 +75,7 @@ class MainMenuDisplay(Display): | ||||
|         super().__init__(*args) | ||||
|         self.menu = menu | ||||
|  | ||||
|         with open("squirrelbattle/assets/ascii_art.txt", "r") as file: | ||||
|         with open(ResourceManager.get_asset_path("ascii_art.txt"), "r") as file: | ||||
|             self.title = file.read().split("\n") | ||||
|  | ||||
|         self.pad = self.newpad(max(self.rows, len(self.title) + 30), | ||||
|   | ||||
| @@ -7,6 +7,7 @@ import sys | ||||
| from .entities.player import Player | ||||
| from .enums import GameMode, KeyValues, DisplayActions | ||||
| from .interfaces import Map | ||||
| from .resources import ResourceManager | ||||
| from .settings import Settings | ||||
| from . import menus | ||||
| from typing import Callable | ||||
| @@ -38,7 +39,7 @@ class Game: | ||||
|         Create a new game on the screen. | ||||
|         """ | ||||
|         # TODO generate a new map procedurally | ||||
|         self.map = Map.load("squirrelbattle/assets/example_map_2.txt") | ||||
|         self.map = Map.load(ResourceManager.get_asset_path("example_map_2.txt")) | ||||
|         self.player = Player() | ||||
|         self.map.add_entity(self.player) | ||||
|         self.player.move(self.map.start_y, self.map.start_x) | ||||
|   | ||||
							
								
								
									
										13
									
								
								squirrelbattle/resources.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								squirrelbattle/resources.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,13 @@ | ||||
| from pathlib import Path | ||||
|  | ||||
|  | ||||
| class ResourceManager: | ||||
|     """ | ||||
|     The ResourceManager loads resources at their right place, | ||||
|     and stores files in config directory. | ||||
|     """ | ||||
|     BASE_DIR = Path(__file__).resolve().parent / 'assets' | ||||
|  | ||||
|     @classmethod | ||||
|     def get_asset_path(cls, filename: str) -> str: | ||||
|         return str(cls.BASE_DIR / filename) | ||||
| @@ -4,6 +4,7 @@ from squirrelbattle.entities.items import Bomb, Heart, Item | ||||
| from squirrelbattle.entities.monsters import Beaver, Hedgehog, Rabbit, TeddyBear | ||||
| from squirrelbattle.entities.player import Player | ||||
| from squirrelbattle.interfaces import Entity, Map | ||||
| from squirrelbattle.resources import ResourceManager | ||||
|  | ||||
|  | ||||
| class TestEntities(unittest.TestCase): | ||||
| @@ -11,7 +12,7 @@ class TestEntities(unittest.TestCase): | ||||
|         """ | ||||
|         Load example map that can be used in tests. | ||||
|         """ | ||||
|         self.map = Map.load("squirrelbattle/assets/example_map.txt") | ||||
|         self.map = Map.load(ResourceManager.get_asset_path("example_map.txt")) | ||||
|         self.player = Player() | ||||
|         self.map.add_entity(self.player) | ||||
|         self.player.move(self.map.start_y, self.map.start_x) | ||||
|   | ||||
| @@ -2,6 +2,7 @@ import unittest | ||||
|  | ||||
| from squirrelbattle.display.texturepack import TexturePack | ||||
| from squirrelbattle.interfaces import Map, Tile | ||||
| from squirrelbattle.resources import ResourceManager | ||||
|  | ||||
|  | ||||
| class TestInterfaces(unittest.TestCase): | ||||
| @@ -18,7 +19,7 @@ class TestInterfaces(unittest.TestCase): | ||||
|         """ | ||||
|         Try to load a map from a file. | ||||
|         """ | ||||
|         m = Map.load("squirrelbattle/assets/example_map.txt") | ||||
|         m = Map.load(ResourceManager.get_asset_path("example_map.txt")) | ||||
|         self.assertEqual(m.width, 52) | ||||
|         self.assertEqual(m.height, 17) | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user