Use a ResourceManager to find assets

This commit is contained in:
Yohann D'ANELLO 2020-11-19 02:49:59 +01:00
parent d92a5e1629
commit 00f843754a
5 changed files with 21 additions and 4 deletions

View File

@ -2,6 +2,7 @@ from typing import List
from squirrelbattle.menus import Menu, MainMenu from squirrelbattle.menus import Menu, MainMenu
from .display import Display from .display import Display
from ..resources import ResourceManager
class MenuDisplay(Display): class MenuDisplay(Display):
@ -74,7 +75,7 @@ class MainMenuDisplay(Display):
super().__init__(*args) super().__init__(*args)
self.menu = menu 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.title = file.read().split("\n")
self.pad = self.newpad(max(self.rows, len(self.title) + 30), self.pad = self.newpad(max(self.rows, len(self.title) + 30),

View File

@ -7,6 +7,7 @@ import sys
from .entities.player import Player from .entities.player import Player
from .enums import GameMode, KeyValues, DisplayActions from .enums import GameMode, KeyValues, DisplayActions
from .interfaces import Map from .interfaces import Map
from .resources import ResourceManager
from .settings import Settings from .settings import Settings
from . import menus from . import menus
from typing import Callable from typing import Callable
@ -38,7 +39,7 @@ class Game:
Create a new game on the screen. Create a new game on the screen.
""" """
# TODO generate a new map procedurally # 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.player = Player()
self.map.add_entity(self.player) self.map.add_entity(self.player)
self.player.move(self.map.start_y, self.map.start_x) self.player.move(self.map.start_y, self.map.start_x)

View 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)

View File

@ -4,6 +4,7 @@ from squirrelbattle.entities.items import Bomb, Heart, Item
from squirrelbattle.entities.monsters import Beaver, Hedgehog, Rabbit, TeddyBear from squirrelbattle.entities.monsters import Beaver, Hedgehog, Rabbit, TeddyBear
from squirrelbattle.entities.player import Player from squirrelbattle.entities.player import Player
from squirrelbattle.interfaces import Entity, Map from squirrelbattle.interfaces import Entity, Map
from squirrelbattle.resources import ResourceManager
class TestEntities(unittest.TestCase): class TestEntities(unittest.TestCase):
@ -11,7 +12,7 @@ class TestEntities(unittest.TestCase):
""" """
Load example map that can be used in tests. 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.player = Player()
self.map.add_entity(self.player) self.map.add_entity(self.player)
self.player.move(self.map.start_y, self.map.start_x) self.player.move(self.map.start_y, self.map.start_x)

View File

@ -2,6 +2,7 @@ import unittest
from squirrelbattle.display.texturepack import TexturePack from squirrelbattle.display.texturepack import TexturePack
from squirrelbattle.interfaces import Map, Tile from squirrelbattle.interfaces import Map, Tile
from squirrelbattle.resources import ResourceManager
class TestInterfaces(unittest.TestCase): class TestInterfaces(unittest.TestCase):
@ -18,7 +19,7 @@ class TestInterfaces(unittest.TestCase):
""" """
Try to load a map from a file. 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.width, 52)
self.assertEqual(m.height, 17) self.assertEqual(m.height, 17)