Load maps from TOML files with XPM-style palette

Introduces a map file format: TOML with a [palette] mapping characters
to (Glyph, Element) definitions and a [grid] multi-line string. Board
now owns the player position, objects, and portals. map_file::load()
deserializes a file and converts it to a Board via From<MapFile>.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-05-16 01:50:05 -05:00
parent 37c10c7c38
commit 2dc749e037
6 changed files with 254 additions and 69 deletions
Generated
+69 -8
View File
@@ -2369,7 +2369,7 @@ version = "3.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e67ba7e9b2b56446f1d419b1d807906278ffa1a658a8a5d8a39dcb1f5a78614f"
dependencies = [
"toml_edit",
"toml_edit 0.25.11+spec-1.1.0",
]
[[package]]
@@ -2650,6 +2650,15 @@ dependencies = [
"syn",
]
[[package]]
name = "serde_spanned"
version = "0.6.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bf41e0cfaf7226dca15e8197172c295a782857fcb97fad1808a166870dee75a3"
dependencies = [
"serde",
]
[[package]]
name = "shlex"
version = "1.3.0"
@@ -2967,6 +2976,27 @@ dependencies = [
"zerovec",
]
[[package]]
name = "toml"
version = "0.8.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc1beb996b9d83529a9e75c17a1686767d148d70663143c7854d8b4a09ced362"
dependencies = [
"serde",
"serde_spanned",
"toml_datetime 0.6.11",
"toml_edit 0.22.27",
]
[[package]]
name = "toml_datetime"
version = "0.6.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "22cddaf88f4fbc13c51aebbf5f8eceb5c7c5a9da2ac40a13519eb5b0a0e8f11c"
dependencies = [
"serde",
]
[[package]]
name = "toml_datetime"
version = "1.1.1+spec-1.1.0"
@@ -2976,6 +3006,20 @@ dependencies = [
"serde_core",
]
[[package]]
name = "toml_edit"
version = "0.22.27"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "41fe8c660ae4257887cf66394862d21dbca4a6ddd26f04a3560410406a2f819a"
dependencies = [
"indexmap",
"serde",
"serde_spanned",
"toml_datetime 0.6.11",
"toml_write",
"winnow 0.7.15",
]
[[package]]
name = "toml_edit"
version = "0.25.11+spec-1.1.0"
@@ -2983,9 +3027,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b59c4d22ed448339746c59b905d24568fcbb3ab65a500494f7b8c3e97739f2b"
dependencies = [
"indexmap",
"toml_datetime",
"toml_datetime 1.1.1+spec-1.1.0",
"toml_parser",
"winnow",
"winnow 1.0.3",
]
[[package]]
@@ -2994,9 +3038,15 @@ version = "1.1.2+spec-1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a2abe9b86193656635d2411dc43050282ca48aa31c2451210f4202550afb7526"
dependencies = [
"winnow",
"winnow 1.0.3",
]
[[package]]
name = "toml_write"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5d99f8c9a7727884afe522e9bd5edbfc91a3312b36a77b5fb8926e4c31a41801"
[[package]]
name = "tracing"
version = "0.1.44"
@@ -3120,6 +3170,8 @@ version = "0.1.0"
dependencies = [
"eframe",
"rhai",
"serde",
"toml",
]
[[package]]
@@ -3980,6 +4032,15 @@ dependencies = [
"xkbcommon-dl",
]
[[package]]
name = "winnow"
version = "0.7.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "df79d97927682d2fd8adb29682d1140b343be4ac0f08fd68b7765d9c059d3945"
dependencies = [
"memchr",
]
[[package]]
name = "winnow"
version = "1.0.3"
@@ -4204,7 +4265,7 @@ dependencies = [
"uds_windows",
"uuid",
"windows-sys 0.61.2",
"winnow",
"winnow 1.0.3",
"zbus_macros",
"zbus_names",
"zvariant",
@@ -4256,7 +4317,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7074f3e50b894eac91750142016d30d0a89be8e67dbfd9704fb875825760e52d"
dependencies = [
"serde",
"winnow",
"winnow 1.0.3",
"zvariant",
]
@@ -4376,7 +4437,7 @@ dependencies = [
"endi",
"enumflags2",
"serde",
"winnow",
"winnow 1.0.3",
"zvariant_derive",
"zvariant_utils",
]
@@ -4404,5 +4465,5 @@ dependencies = [
"quote",
"serde",
"syn",
"winnow",
"winnow 1.0.3",
]
+2
View File
@@ -6,3 +6,5 @@ edition = "2024"
[dependencies]
eframe = "0.33.3"
rhai = "1"
serde = { version = "1", features = ["derive"] }
toml = "0.8"
+38
View File
@@ -0,0 +1,38 @@
[map]
name = "Starting Room"
width = 60
height = 25
player_start = [30, 12]
[palette]
" " = { passable = true, ch = " ", fg = "#000000", bg = "#000000" }
"#" = { passable = false, ch = "#", fg = "#808080", bg = "#606060" }
[grid]
content = """
############################################################
# #
# #
# #
# #
# #
# ###### #
# # #
# # #
# # #
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
############################################################
"""
+37 -53
View File
@@ -1,8 +1,7 @@
use eframe::egui::Color32;
use serde::Deserialize;
pub const BOARD_W: usize = 60;
pub const BOARD_H: usize = 25;
#[derive(Clone, Copy)]
pub struct Glyph {
pub ch: char,
pub fg: Color32,
@@ -10,14 +9,6 @@ pub struct Glyph {
}
impl Glyph {
pub fn floor() -> Self {
Self { ch: ' ', fg: Color32::BLACK, bg: Color32::BLACK }
}
pub fn wall() -> Self {
Self { ch: '#', fg: Color32::GRAY, bg: Color32::DARK_GRAY }
}
pub fn player() -> Self {
Self { ch: '@', fg: Color32::LIGHT_BLUE, bg: Color32::BLACK }
}
@@ -27,14 +18,41 @@ pub struct Element {
pub passable: bool,
}
#[derive(Deserialize)]
#[allow(dead_code)]
pub struct ObjectDef {
pub x: usize,
pub y: usize,
pub script: String,
}
#[derive(Deserialize)]
#[allow(dead_code)]
pub struct PortalDef {
pub x: usize,
pub y: usize,
pub target_map: String,
pub target_entry: String,
}
pub struct Player {
pub x: i32,
pub y: i32,
}
#[allow(dead_code)]
pub struct Board {
pub width: usize,
pub height: usize,
pub elements: Vec<Element>,
cells: Vec<(Glyph, usize)>,
pub(crate) cells: Vec<(Glyph, usize)>,
pub player: Player,
pub objects: Vec<ObjectDef>,
pub portals: Vec<PortalDef>,
}
impl Board {
#[allow(dead_code)]
pub fn add_element(&mut self, element: Element) -> usize {
let idx = self.elements.len();
self.elements.push(element);
@@ -45,6 +63,7 @@ impl Board {
&self.cells[y * self.width + x]
}
#[allow(dead_code)]
pub fn get_mut(&mut self, x: usize, y: usize) -> &mut (Glyph, usize) {
&mut self.cells[y * self.width + x]
}
@@ -55,59 +74,24 @@ impl Board {
}
}
pub struct Player {
pub x: i32,
pub y: i32,
}
pub struct GameState {
pub board: Board,
pub player: Player,
}
impl GameState {
pub fn new() -> Self {
let w = BOARD_W;
let h = BOARD_H;
let mut board = Board {
width: w,
height: h,
elements: Vec::new(),
cells: Vec::new(),
};
let floor = board.add_element(Element { passable: true });
let wall = board.add_element(Element { passable: false });
board.cells = (0..w * h)
.map(|_| (Glyph::floor(), floor))
.collect();
for x in 0..w {
*board.get_mut(x, 0) = (Glyph::wall(), wall);
*board.get_mut(x, h - 1) = (Glyph::wall(), wall);
}
for y in 0..h {
*board.get_mut(0, y) = (Glyph::wall(), wall);
*board.get_mut(w - 1, y) = (Glyph::wall(), wall);
}
Self {
board,
player: Player { x: 30, y: 12 },
}
pub fn new(board: Board) -> Self {
Self { board }
}
pub fn try_move(&mut self, dx: i32, dy: i32) {
let nx = self.player.x + dx;
let ny = self.player.y + dy;
let nx = self.board.player.x + dx;
let ny = self.board.player.y + dy;
if nx >= 0 && ny >= 0 {
let nx = nx as usize;
let ny = ny as usize;
if nx < self.board.width && ny < self.board.height && self.board.is_passable(nx, ny) {
self.player.x = nx as i32;
self.player.y = ny as i32;
self.board.player.x = nx as i32;
self.board.player.y = ny as i32;
}
}
}
+11 -8
View File
@@ -2,7 +2,8 @@ use eframe::egui;
use egui::{Align2, FontId, Key, Pos2, Rect, vec2};
mod game;
use game::{GameState, Glyph, BOARD_W, BOARD_H};
mod map_file;
use game::{GameState, Glyph};
const CELL_W: f32 = 14.0;
const CELL_H: f32 = 20.0;
@@ -11,8 +12,10 @@ const MENU_H: f32 = 24.0;
const PANEL_MARGIN: f32 = 8.0;
fn main() -> eframe::Result<()> {
let board_px_w = BOARD_W as f32 * CELL_W + 2.0 * PANEL_MARGIN;
let board_px_h = BOARD_H as f32 * CELL_H + MENU_H + 2.0 * PANEL_MARGIN;
let board = map_file::load("maps/start.toml").expect("failed to load maps/start.toml");
let board_px_w = board.width as f32 * CELL_W + 2.0 * PANEL_MARGIN;
let board_px_h = board.height as f32 * CELL_H + MENU_H + 2.0 * PANEL_MARGIN;
let options = eframe::NativeOptions {
viewport: egui::ViewportBuilder::default()
.with_inner_size([board_px_w, board_px_h])
@@ -22,7 +25,7 @@ fn main() -> eframe::Result<()> {
eframe::run_native(
"Viberogue",
options,
Box::new(|_cc| Ok(Box::new(App::new()))),
Box::new(move |_cc| Ok(Box::new(App::new(board)))),
)
}
@@ -31,8 +34,8 @@ struct App {
}
impl App {
fn new() -> Self {
Self { state: GameState::new() }
fn new(board: game::Board) -> Self {
Self { state: GameState::new(board) }
}
}
@@ -74,8 +77,8 @@ impl eframe::App for App {
draw_glyph(
painter,
origin,
self.state.player.x as usize,
self.state.player.y as usize,
self.state.board.player.x as usize,
self.state.board.player.y as usize,
&player_glyph,
);
});
+97
View File
@@ -0,0 +1,97 @@
use std::collections::HashMap;
use serde::Deserialize;
use eframe::egui::Color32;
use crate::game::{Board, Element, Glyph, ObjectDef, Player, PortalDef};
#[derive(Deserialize)]
pub struct MapFile {
pub map: MapHeader,
pub palette: HashMap<String, PaletteEntry>,
pub grid: GridData,
#[serde(default)]
pub objects: Vec<ObjectDef>,
#[serde(default)]
pub portals: Vec<PortalDef>,
}
#[derive(Deserialize)]
pub struct MapHeader {
#[allow(dead_code)]
pub name: String,
pub width: usize,
pub height: usize,
pub player_start: [i32; 2],
}
#[derive(Deserialize)]
pub struct PaletteEntry {
pub passable: bool,
pub ch: char,
pub fg: String,
pub bg: String,
}
#[derive(Deserialize)]
pub struct GridData {
pub content: String,
}
fn parse_color(hex: &str) -> Color32 {
let hex = hex.trim_start_matches('#');
if hex.len() != 6 {
return Color32::BLACK;
}
let r = u8::from_str_radix(&hex[0..2], 16).unwrap_or(0);
let g = u8::from_str_radix(&hex[2..4], 16).unwrap_or(0);
let b = u8::from_str_radix(&hex[4..6], 16).unwrap_or(0);
Color32::from_rgb(r, g, b)
}
impl From<MapFile> for Board {
fn from(mf: MapFile) -> Self {
let w = mf.map.width;
let h = mf.map.height;
let mut elements: Vec<Element> = Vec::new();
let mut palette: HashMap<char, (Glyph, usize)> = 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));
}
let mut cells: Vec<(Glyph, usize)> = 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));
}
}
}
Board {
width: w,
height: h,
elements,
cells,
player: Player {
x: mf.map.player_start[0],
y: mf.map.player_start[1],
},
objects: mf.objects,
portals: mf.portals,
}
}
}
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))
}