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:
+53
-7
@@ -1,19 +1,40 @@
|
||||
use eframe::egui;
|
||||
use egui::{Align2, FontId, Key, Pos2, Rect, vec2};
|
||||
|
||||
mod game;
|
||||
use game::{GameState, Glyph};
|
||||
|
||||
const CELL_W: f32 = 14.0;
|
||||
const CELL_H: f32 = 20.0;
|
||||
|
||||
fn main() -> eframe::Result<()> {
|
||||
let options = eframe::NativeOptions::default();
|
||||
eframe::run_native(
|
||||
"Viberogue",
|
||||
options,
|
||||
Box::new(|_cc| Ok(Box::new(App::default()))),
|
||||
Box::new(|_cc| Ok(Box::new(App::new()))),
|
||||
)
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct App;
|
||||
struct App {
|
||||
state: GameState,
|
||||
}
|
||||
|
||||
impl App {
|
||||
fn new() -> Self {
|
||||
Self { state: GameState::new() }
|
||||
}
|
||||
}
|
||||
|
||||
impl eframe::App for App {
|
||||
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
|
||||
ctx.input(|i| {
|
||||
if i.key_pressed(Key::ArrowUp) { self.state.try_move( 0, -1); }
|
||||
if i.key_pressed(Key::ArrowDown) { self.state.try_move( 0, 1); }
|
||||
if i.key_pressed(Key::ArrowLeft) { self.state.try_move(-1, 0); }
|
||||
if i.key_pressed(Key::ArrowRight) { self.state.try_move( 1, 0); }
|
||||
});
|
||||
|
||||
egui::TopBottomPanel::top("menu_bar").show(ctx, |ui| {
|
||||
egui::MenuBar::new().ui(ui, |ui| {
|
||||
ui.menu_button("File", |ui| {
|
||||
@@ -25,10 +46,35 @@ impl eframe::App for App {
|
||||
});
|
||||
|
||||
egui::CentralPanel::default().show(ctx, |ui| {
|
||||
let rect = ui.available_rect_before_wrap();
|
||||
let center = rect.center();
|
||||
let square = egui::Rect::from_center_size(center, egui::vec2(200.0, 200.0));
|
||||
ui.painter().rect_filled(square, 0.0, egui::Color32::BLUE);
|
||||
let board = &self.state.board;
|
||||
let origin = ui.available_rect_before_wrap().min;
|
||||
let painter = ui.painter();
|
||||
|
||||
for y in 0..board.height {
|
||||
for x in 0..board.width {
|
||||
let (glyph, _) = board.get(x, y);
|
||||
draw_glyph(painter, origin, x, y, glyph);
|
||||
}
|
||||
}
|
||||
|
||||
let player_glyph = Glyph::player();
|
||||
draw_glyph(
|
||||
painter,
|
||||
origin,
|
||||
self.state.player.x as usize,
|
||||
self.state.player.y as usize,
|
||||
&player_glyph,
|
||||
);
|
||||
});
|
||||
|
||||
ctx.request_repaint();
|
||||
}
|
||||
}
|
||||
|
||||
fn draw_glyph(painter: &egui::Painter, origin: Pos2, x: usize, y: usize, glyph: &Glyph) {
|
||||
let tl = origin + vec2(x as f32 * CELL_W, y as f32 * CELL_H);
|
||||
let rect = Rect::from_min_size(tl, vec2(CELL_W, CELL_H));
|
||||
let center = rect.center();
|
||||
painter.rect_filled(rect, 0.0, glyph.bg);
|
||||
painter.text(center, Align2::CENTER_CENTER, glyph.ch, FontId::monospace(CELL_H), glyph.fg);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user