sep-pm-platformer/project/sprite/SpritesheetManager.py

29 lines
953 B
Python
Raw Normal View History

2023-03-24 17:41:48 +01:00
import json
2023-03-25 15:41:32 +01:00
import sys
2023-03-24 17:41:48 +01:00
2023-03-25 15:41:32 +01:00
from sprite.Spritesheet import Spritesheet
sys.path.append('./sprite')
2023-03-24 17:41:48 +01:00
# This class is used to load named sprite sheets from the img folder.
class SpritesheetManager:
def __init__(self, sprites_dir, config_file):
self.sprites_dir = sprites_dir
self.spritesheets = {}
self.load_from_config(config_file)
def load_from_config(self, config_file):
print("Loading sprites from sprite sheet config file", config_file)
config = json.load(open(config_file))
for sprite_data in config:
sprite_sheet = Spritesheet(self.sprites_dir, sprite_data)
self.spritesheets[sprite_sheet.sprite_id] = sprite_sheet
print("Loaded", len(self.spritesheets), "sprite sheet(s)")
def get_sheet(self, sheet):
if sheet not in self.spritesheets:
raise ValueError("Invalid/Missing sprite sheet " + sheet)
return self.spritesheets[sheet]