pushers
This commit is contained in:
+25
-13
@@ -101,21 +101,25 @@ pub enum PlayerStart {
|
||||
///
|
||||
/// Each entry is keyed by a single character (e.g. `"#"` or `" "`).
|
||||
/// That character is used in the `[grid]` content string to place tiles.
|
||||
/// The entry defines both how the tile looks (`tile`, `fg`, `bg`) and which
|
||||
/// [`Archetype`] it uses for behavior.
|
||||
/// The entry defines which [`Archetype`] the cell uses for behavior; `tile`,
|
||||
/// `fg`, and `bg` are optional and fall back to [`Archetype::default_glyph`]
|
||||
/// when absent. On save, fields that match the archetype default are omitted.
|
||||
///
|
||||
/// `tile` accepts either an integer (`tile = 35`) or a char (`tile = "#"`).
|
||||
#[derive(Deserialize, Serialize)]
|
||||
pub struct PaletteEntry {
|
||||
/// The archetype name for this tile, e.g. `"wall"` or `"empty"`.
|
||||
pub archetype: String,
|
||||
/// The tile index into the board's bitmap font.
|
||||
/// Tile index into the board's bitmap font. Omit to use the archetype default.
|
||||
/// Accepts either an integer or a single-character string.
|
||||
tile: TileIndex,
|
||||
/// Foreground color as an `"#RRGGBB"` hex string.
|
||||
pub fg: String,
|
||||
/// Background color as an `"#RRGGBB"` hex string.
|
||||
pub bg: String,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
tile: Option<TileIndex>,
|
||||
/// Foreground color as an `"#RRGGBB"` hex string. Omit to use the archetype default.
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub fg: Option<String>,
|
||||
/// Background color as an `"#RRGGBB"` hex string. Omit to use the archetype default.
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub bg: Option<String>,
|
||||
}
|
||||
|
||||
/// The `[grid]` section of a map file.
|
||||
@@ -216,12 +220,16 @@ fn make_glyph(tile: u32, fg: &str, bg: &str) -> Glyph {
|
||||
}
|
||||
|
||||
/// Constructs a serializable [`PaletteEntry`] from core board types.
|
||||
///
|
||||
/// Fields that match the archetype's [`default_glyph`](Archetype::default_glyph)
|
||||
/// are omitted (`None`) so saved files stay minimal and round-trip cleanly.
|
||||
fn make_palette_entry(glyph: Glyph, arch: Archetype) -> PaletteEntry {
|
||||
let default = arch.default_glyph();
|
||||
PaletteEntry {
|
||||
archetype: arch.name().to_string(),
|
||||
tile: TileIndex::Num(glyph.tile),
|
||||
fg: color_to_hex(glyph.fg),
|
||||
bg: color_to_hex(glyph.bg),
|
||||
tile: (glyph.tile != default.tile).then_some(TileIndex::Num(glyph.tile)),
|
||||
fg: (glyph.fg != default.fg ).then_some(color_to_hex(glyph.fg)),
|
||||
bg: (glyph.bg != default.bg ).then_some(color_to_hex(glyph.bg)),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -324,10 +332,14 @@ impl TryFrom<MapFile> for Board {
|
||||
|
||||
for (key, entry) in mf.palette {
|
||||
let key_char = key.chars().next().unwrap_or(' ');
|
||||
let tile = entry.tile.into_u32();
|
||||
match Archetype::try_from(entry.archetype.as_str()) {
|
||||
Ok(archetype) => {
|
||||
let glyph = make_glyph(tile, &entry.fg, &entry.bg);
|
||||
// Fall back to default_glyph() for any absent visual field.
|
||||
let default = archetype.default_glyph();
|
||||
let tile = entry.tile.map(|t| t.into_u32()).unwrap_or(default.tile);
|
||||
let fg = entry.fg.as_deref().map(parse_color).unwrap_or(default.fg);
|
||||
let bg = entry.bg.as_deref().map(parse_color).unwrap_or(default.bg);
|
||||
let glyph = Glyph { tile, fg, bg };
|
||||
palette.insert(key_char, (glyph, archetype));
|
||||
}
|
||||
Err(e) => {
|
||||
|
||||
Reference in New Issue
Block a user