Add basic player movement on a ZZT-style board
Implements Glyph (per-cell visual), Element (shared behavior palette), and Board (60x25 grid of (Glyph, usize) cells). Player walks around with arrow keys and is blocked by wall elements. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
+111
@@ -0,0 +1,111 @@
|
||||
use eframe::egui::Color32;
|
||||
|
||||
pub struct Glyph {
|
||||
pub ch: char,
|
||||
pub fg: Color32,
|
||||
pub bg: Color32,
|
||||
}
|
||||
|
||||
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 }
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Element {
|
||||
pub passable: bool,
|
||||
}
|
||||
|
||||
pub struct Board {
|
||||
pub width: usize,
|
||||
pub height: usize,
|
||||
pub elements: Vec<Element>,
|
||||
cells: Vec<(Glyph, usize)>,
|
||||
}
|
||||
|
||||
impl Board {
|
||||
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]
|
||||
}
|
||||
|
||||
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 Player {
|
||||
pub x: i32,
|
||||
pub y: i32,
|
||||
}
|
||||
|
||||
pub struct GameState {
|
||||
pub board: Board,
|
||||
pub player: Player,
|
||||
}
|
||||
|
||||
impl GameState {
|
||||
pub fn new() -> Self {
|
||||
let w = 60;
|
||||
let h = 25;
|
||||
|
||||
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 try_move(&mut self, dx: i32, dy: i32) {
|
||||
let nx = self.player.x + dx;
|
||||
let ny = self.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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user