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:
+37
-53
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user