2026-05-16 01:10:04 -05:00
|
|
|
use eframe::egui::Color32;
|
2026-05-16 01:50:05 -05:00
|
|
|
use serde::Deserialize;
|
2026-05-16 01:10:04 -05:00
|
|
|
|
2026-05-16 01:50:05 -05:00
|
|
|
#[derive(Clone, Copy)]
|
2026-05-16 01:10:04 -05:00
|
|
|
pub struct Glyph {
|
|
|
|
|
pub ch: char,
|
|
|
|
|
pub fg: Color32,
|
|
|
|
|
pub bg: Color32,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Glyph {
|
|
|
|
|
pub fn player() -> Self {
|
|
|
|
|
Self { ch: '@', fg: Color32::LIGHT_BLUE, bg: Color32::BLACK }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub struct Element {
|
|
|
|
|
pub passable: bool,
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-16 01:50:05 -05:00
|
|
|
#[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)]
|
2026-05-16 01:10:04 -05:00
|
|
|
pub struct Board {
|
|
|
|
|
pub width: usize,
|
|
|
|
|
pub height: usize,
|
|
|
|
|
pub elements: Vec<Element>,
|
2026-05-16 01:50:05 -05:00
|
|
|
pub(crate) cells: Vec<(Glyph, usize)>,
|
|
|
|
|
pub player: Player,
|
|
|
|
|
pub objects: Vec<ObjectDef>,
|
|
|
|
|
pub portals: Vec<PortalDef>,
|
2026-05-16 01:10:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Board {
|
2026-05-16 01:50:05 -05:00
|
|
|
#[allow(dead_code)]
|
2026-05-16 01:10:04 -05:00
|
|
|
pub fn add_element(&mut self, element: Element) -> usize {
|
|
|
|
|
let idx = self.elements.len();
|
|
|
|
|
self.elements.push(element);
|
|
|
|
|
idx
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn get(&self, x: usize, y: usize) -> &(Glyph, usize) {
|
|
|
|
|
&self.cells[y * self.width + x]
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-16 01:50:05 -05:00
|
|
|
#[allow(dead_code)]
|
2026-05-16 01:10:04 -05:00
|
|
|
pub fn get_mut(&mut self, x: usize, y: usize) -> &mut (Glyph, usize) {
|
|
|
|
|
&mut self.cells[y * self.width + x]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn is_passable(&self, x: usize, y: usize) -> bool {
|
|
|
|
|
let (_, elem_idx) = self.get(x, y);
|
|
|
|
|
self.elements[*elem_idx].passable
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub struct GameState {
|
|
|
|
|
pub board: Board,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl GameState {
|
2026-05-16 01:50:05 -05:00
|
|
|
pub fn new(board: Board) -> Self {
|
|
|
|
|
Self { board }
|
2026-05-16 01:10:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn try_move(&mut self, dx: i32, dy: i32) {
|
2026-05-16 01:50:05 -05:00
|
|
|
let nx = self.board.player.x + dx;
|
|
|
|
|
let ny = self.board.player.y + dy;
|
2026-05-16 01:10:04 -05:00
|
|
|
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) {
|
2026-05-16 01:50:05 -05:00
|
|
|
self.board.player.x = nx as i32;
|
|
|
|
|
self.board.player.y = ny as i32;
|
2026-05-16 01:10:04 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|