flatland_presentation/
player_config.rs1use std::path::Path;
4
5use serde::Deserialize;
6
7use crate::SpriteModeMap;
8
9#[derive(Debug, Clone, Default, Deserialize)]
10pub struct PlayerPresentationDoc {
11 #[serde(default)]
12 pub player_presentation: PlayerPresentationConfig,
13}
14
15#[derive(Debug, Clone, Default, Deserialize)]
16pub struct PlayerPresentationConfig {
17 #[serde(default)]
18 pub tile_id: Option<String>,
19 #[serde(default)]
21 pub paperdoll_ref: Option<String>,
22 #[serde(default)]
23 pub sprite_modes: SpriteModeMap,
24}
25
26impl PlayerPresentationConfig {
27 pub fn empty() -> Self {
28 Self::default()
29 }
30}
31
32pub fn default_player_presentation_path() -> Option<std::path::PathBuf> {
33 let mut dir = std::env::current_dir().ok()?;
34 for _ in 0..6 {
35 let candidate = dir.join("assets/gfx/player-presentation.yaml");
36 if candidate.is_file() {
37 return Some(candidate);
38 }
39 if !dir.pop() {
40 break;
41 }
42 }
43 None
44}
45
46pub fn load_player_presentation(
47 path: impl AsRef<Path>,
48) -> anyhow::Result<PlayerPresentationConfig> {
49 let raw = std::fs::read_to_string(path.as_ref())?;
50 let doc: PlayerPresentationDoc = serde_yaml::from_str(&raw)?;
51 Ok(doc.player_presentation)
52}
53
54pub fn load_player_presentation_default() -> PlayerPresentationConfig {
55 default_player_presentation_path()
56 .and_then(|p| load_player_presentation(&p).ok())
57 .unwrap_or_default()
58}
59
60#[cfg(test)]
61mod tests {
62 use super::*;
63
64 #[test]
65 fn loads_repo_default() {
66 let path = default_player_presentation_path().expect("player-presentation.yaml");
67 let cfg = load_player_presentation(&path).expect("parse");
68 assert!(cfg.tile_id.is_some() || !cfg.sprite_modes.is_empty());
69 }
70}