Files
Flora/assets/__init__.py
T

64 lines
2.2 KiB
Python

from __future__ import annotations
import os
import pygame
_ASSETS = os.path.dirname(os.path.abspath(__file__))
def sprite(name: str, variant: str = "") -> str:
"""Path to a unit sprite, e.g. sprite('sweet pea') -> assets/sprites/sweet_pea.png.
With a variant ('full' or 'head') looks under the split folders, e.g.
sprite('daisy', 'head') -> assets/sprites/plant_head/daisy_head.png."""
base = name.lower().replace(' ', '_')
if variant:
return os.path.join(_ASSETS, "sprites", f"plant_{variant}", f"{base}_{variant}.png")
return os.path.join(_ASSETS, "sprites", f"{base}.png")
def load_sprite(name: str, variant: str = "") -> "pygame.Surface":
"""Load a unit sprite, trying the split variant first ('full'/'head') and
falling back to the legacy single sprite when the variant file is missing.
Returns a surface or None."""
def _exists(nm):
p = sprite(nm)
if os.path.exists(p):
return p
return None
try:
if variant:
path = sprite(name, variant)
if not os.path.exists(path):
path = _exists(name)
if path is None:
return None
else:
path = _exists(name)
if path is None:
return None
return pygame.image.load(path).convert_alpha()
except (pygame.error, OSError):
return None
def icon(category: str, name: str) -> str:
"""Path to an icon under a category folder, e.g. icon('currency_icons', 'coin')
-> assets/icons/currency_icons/coin.png"""
return os.path.join(_ASSETS, "icons", category, f"{name}.png")
def type_icon_path(ptype: str) -> str:
"""Path to a plant type icon, e.g. type_icon_path('radiant') -> assets/icons/types/radiant_icon.png"""
return icon("types", f"{ptype}_icon")
def plot_icon_path(name: str) -> str:
"""Path to a soil plot sprite, e.g. plot_icon_path('soil_barren') -> assets/icons/plot/soil_barren.png"""
return icon("plot", name)
def rune_icon_path(color: str) -> str:
"""Path to a rune sprite by color, e.g. rune_icon_path('red') -> assets/icons/runes/ruby_rune.png"""
from engine import runes
return icon("runes", runes.RUNE_ICON_FILES.get(color, "rune_brown"))