Replace Element with Behavior + Archetype; update map format
Element is replaced by two types: Behavior (a plain struct of runtime properties: passable, opaque) and Archetype (an enum of named presets: Empty, Wall, Object, ErrorBlock). Board.cells changes from Vec<(Glyph, usize)> to Vec<(Glyph, Archetype)>, eliminating the per-board element palette. Map files now reference archetypes by name (archetype = "wall") instead of property bags (passable = false), so the archetype list can be reordered without breaking saved games. Unknown archetype names produce an ErrorBlock cell (yellow ? on red) so malformed maps are immediately visible in-game. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
+34
-29
@@ -1,7 +1,7 @@
|
||||
use std::collections::HashMap;
|
||||
use serde::Deserialize;
|
||||
use eframe::egui::Color32;
|
||||
use crate::game::{Board, Element, Glyph, ObjectDef, Player, PortalDef};
|
||||
use crate::game::{Archetype, Board, Glyph, ObjectDef, Player, PortalDef};
|
||||
|
||||
/// The top-level deserialization type for a `.toml` map file.
|
||||
///
|
||||
@@ -15,7 +15,7 @@ pub struct MapFile {
|
||||
/// The `[map]` section: name, dimensions, player start position.
|
||||
pub map: MapHeader,
|
||||
/// The `[palette]` section: maps single-character keys to tile definitions.
|
||||
/// Each entry defines both the visual ([`Glyph`]) and behavior ([`Element`])
|
||||
/// Each entry defines both the visual ([`Glyph`]) and the [`Archetype`]
|
||||
/// for all cells that use that character in the grid.
|
||||
pub palette: HashMap<String, PaletteEntry>,
|
||||
/// The `[grid]` section: the multi-line string that defines cell layout.
|
||||
@@ -51,8 +51,8 @@ pub struct MapHeader {
|
||||
///
|
||||
/// Each palette 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 (`ch`, `fg`, `bg`) and how it
|
||||
/// behaves (`passable`).
|
||||
/// The entry defines both how the tile looks (`ch`, `fg`, `bg`) and which
|
||||
/// [`Archetype`] it uses for behavior.
|
||||
///
|
||||
/// Note that `ch` (the display character) does not have to match the palette
|
||||
/// key. The key is just a label for the grid; `ch` is what actually gets
|
||||
@@ -60,8 +60,10 @@ pub struct MapHeader {
|
||||
/// wall tile that displays as `#`.
|
||||
#[derive(Deserialize)]
|
||||
pub struct PaletteEntry {
|
||||
/// Whether entities can walk onto this tile.
|
||||
pub passable: bool,
|
||||
/// The archetype name for this tile, e.g. `"wall"` or `"empty"`.
|
||||
/// Parsed via [`Archetype::try_from`]; unknown names produce an
|
||||
/// [`Archetype::ErrorBlock`] cell and a logged warning.
|
||||
pub archetype: String,
|
||||
/// The character displayed in this cell.
|
||||
pub ch: char,
|
||||
/// Foreground color as an `"#RRGGBB"` hex string.
|
||||
@@ -100,42 +102,46 @@ fn parse_color(hex: &str) -> Color32 {
|
||||
///
|
||||
/// The conversion proceeds in two passes:
|
||||
///
|
||||
/// 1. **Palette pass** — each palette entry becomes an [`Element`] (pushed
|
||||
/// into `elements`) and a [`Glyph`] (stored in a temporary `HashMap`
|
||||
/// keyed by the palette character). The element index is recorded
|
||||
/// alongside the glyph so cells can reference it.
|
||||
/// 1. **Palette pass** — each entry is parsed into a `(Glyph, Archetype)` pair
|
||||
/// and stored in a temporary `HashMap` keyed by the palette character.
|
||||
/// Unknown archetype names produce an [`Archetype::ErrorBlock`] and a
|
||||
/// logged warning so malformed maps are visible in-game.
|
||||
///
|
||||
/// 2. **Grid pass** — `grid.content` is split into lines; each character
|
||||
/// is looked up in the palette map. Unknown characters are silently
|
||||
/// skipped (they produce no cell), so a malformed grid will result in
|
||||
/// a `cells` vec shorter than `width * height`. No explicit validation
|
||||
/// is done here yet.
|
||||
/// is looked up in the palette map and pushed directly into `cells`.
|
||||
/// Unknown characters are silently skipped, so a malformed grid will
|
||||
/// result in a `cells` vec shorter than `width * height`.
|
||||
impl From<MapFile> for Board {
|
||||
fn from(mf: MapFile) -> Self {
|
||||
let w = mf.map.width;
|
||||
let h = mf.map.height;
|
||||
|
||||
// Pass 1: build the element palette and a char→(Glyph, idx) lookup.
|
||||
let mut elements: Vec<Element> = Vec::new();
|
||||
let mut palette: HashMap<char, (Glyph, usize)> = HashMap::new();
|
||||
// Pass 1: build a char → (Glyph, Archetype) lookup from the palette.
|
||||
let mut palette: HashMap<char, (Glyph, Archetype)> = HashMap::new();
|
||||
|
||||
for (key, entry) in &mf.palette {
|
||||
let key_char = key.chars().next().unwrap_or(' ');
|
||||
let idx = elements.len();
|
||||
elements.push(Element { passable: entry.passable });
|
||||
palette.insert(key_char, (Glyph {
|
||||
ch: entry.ch,
|
||||
fg: parse_color(&entry.fg),
|
||||
bg: parse_color(&entry.bg),
|
||||
}, idx));
|
||||
// Unknown archetype names fall back to ErrorBlock so the error is
|
||||
// visible in-game rather than silently producing empty tiles.
|
||||
match Archetype::try_from(entry.archetype.as_str()) {
|
||||
Ok(archetype) => {
|
||||
let glyph = Glyph { ch: entry.ch, fg: parse_color(&entry.fg), bg: parse_color(&entry.bg) };
|
||||
palette.insert(key_char, (glyph, archetype));
|
||||
}
|
||||
Err(e) => {
|
||||
// Be sure to log and ignore the default glyph for an unknown archetype
|
||||
eprintln!("{e}");
|
||||
palette.insert(key_char, (Archetype::ErrorBlock.default_glyph(), Archetype::ErrorBlock));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Pass 2: walk the grid string and build cells.
|
||||
let mut cells: Vec<(Glyph, usize)> = Vec::with_capacity(w * h);
|
||||
let mut cells: Vec<(Glyph, Archetype)> = Vec::with_capacity(w * h);
|
||||
for line in mf.grid.content.lines() {
|
||||
for ch in line.chars() {
|
||||
if let Some(&(glyph, idx)) = palette.get(&ch) {
|
||||
cells.push((glyph, idx));
|
||||
if let Some(&cell) = palette.get(&ch) {
|
||||
cells.push(cell);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -143,7 +149,6 @@ impl From<MapFile> for Board {
|
||||
Board {
|
||||
width: w,
|
||||
height: h,
|
||||
elements,
|
||||
cells,
|
||||
player: Player {
|
||||
x: mf.map.player_start[0],
|
||||
@@ -167,4 +172,4 @@ pub fn load(path: &str) -> Result<Board, Box<dyn std::error::Error>> {
|
||||
let content = std::fs::read_to_string(path)?;
|
||||
let map_file: MapFile = toml::from_str(&content)?;
|
||||
Ok(Board::from(map_file))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user