Skip to content

Commit

Permalink
Add Point struct
Browse files Browse the repository at this point in the history
Add a Point struct as a convenient holder for (x,y) indices. The Point
internally uses i64s in order to handle maps that place the origin at
some point other than the corner. Currently, the map only supports
indices starting at (0,0) but that is expected to change in the future.
  • Loading branch information
seilis committed Jan 4, 2025
1 parent f6de66b commit 3ca2b64
Show file tree
Hide file tree
Showing 7 changed files with 446 additions and 98 deletions.
48 changes: 35 additions & 13 deletions src/bin/rpgmap-gui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
use clap::{command, value_parser, Arg};
use egui;

use rpgtools::error::Result;
use rpgtools::map::gridmap::AreaType;
use rpgtools::map::{GridMap, Renderer};
use rpgtools::map::{gridmap::Point, GridMap};

fn main() {
fn main() -> Result<()> {
let cli = command!()
.author("Aaron Seilis <[email protected]>")
.about("A simple map generator for role playing games")
Expand Down Expand Up @@ -92,12 +93,14 @@ fn main() {
match style.as_str() {
"halls" => {
map.generate_dungeon(num_rooms, 5);
map.place_entrance_near((width / 2, height / 2))
let point: Point = (width / 2, height / 2).try_into().unwrap();
map.place_entrance_near(point)
.expect("width/height is outside of map");
}
"cave" => {
map.generate_cave(4, 50);
map.place_entrance_near((width / 2, height / 2))
let point: Point = (width / 2, height / 2).try_into().unwrap();
map.place_entrance_near(point)
.expect("width/height is outside of map");
}
_ => unreachable!(),
Expand All @@ -108,7 +111,9 @@ fn main() {
"RPG Map",
options,
Box::new(|_cc| Ok(Box::new(RpgMapGui::new(map)))),
);
)?;

Ok(())
}

enum Tool {
Expand Down Expand Up @@ -137,7 +142,11 @@ impl RpgMapGui {
fn new(map: GridMap) -> Self {
let tool = Tool::default();
let dragging = false;
Self { map, tool, dragging }
Self {
map,
tool,
dragging,
}
}
}

Expand All @@ -154,13 +163,17 @@ impl eframe::App for RpgMapGui {
if ui.button("Dungeon").clicked() {
// Generate a dungeon!
self.map.generate_dungeon(10, 5);
self.map.place_entrance_near((0, 0));
self.map
.place_entrance_near((0, 0))
.expect("failed to place entrance");
}

if ui.button("Cave").clicked() {
// Generate a cave!
self.map.generate_cave(4, 50);
self.map.place_entrance_near((0, 0));
self.map
.place_entrance_near((0, 0))
.expect("failed to place entrance");
}
});
});
Expand Down Expand Up @@ -208,6 +221,8 @@ impl eframe::App for RpgMapGui {
let cell_x = scroll_offset.x + x as f32 * cell_size;
let cell_y = scroll_offset.y + y as f32 * cell_size;

let point: Point = (x, y).try_into().unwrap();

let cell = ui.allocate_rect(
egui::Rect::from_min_size(
egui::pos2(cell_x, cell_y),
Expand All @@ -217,7 +232,7 @@ impl eframe::App for RpgMapGui {
);

// TODO: Refactor this into an into() call.
let color = match self.map.get_cell_ref(x,y).area() {
let color = match self.map.get_cell_ref(point).area() {
AreaType::Room => egui::Color32::LIGHT_GRAY,
AreaType::Entrance => egui::Color32::RED,
AreaType::Nothing => egui::Color32::DARK_GRAY,
Expand All @@ -234,13 +249,20 @@ impl eframe::App for RpgMapGui {

// If the mouse main button is down then we may need to
// set a cell.
if self.dragging && cell.rect.contains(ctx.pointer_hover_pos().unwrap_or_default()) {
if self.dragging
&& cell
.rect
.contains(ctx.pointer_hover_pos().unwrap_or_default())
{
if let Tool::CellPainter(ref area) = &self.tool {
self.map.get_cell_mut(x, y).set_area(area.to_owned());
self.map.get_cell_mut(point).set_area(area.to_owned());
}
} else if ui.interact(cell.rect, egui::Id::new((x, y)), egui::Sense::click()).clicked() {
} else if ui
.interact(cell.rect, egui::Id::new(point), egui::Sense::click())
.clicked()
{
if let Tool::CellPainter(ref area) = &self.tool {
self.map.get_cell_mut(x, y).set_area(area.to_owned());
self.map.get_cell_mut(point).set_area(area.to_owned());
}
}
}
Expand Down
15 changes: 9 additions & 6 deletions src/bin/rpgmap.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
//! Program for making simple RPG maps. This is the Rust language implementation.
use clap::{command, value_parser, Arg};

use rpgtools::map::{GridMap, Renderer};
use rpgtools::error::Result;
use rpgtools::map::{gridmap::Point, GridMap, Renderer};

fn main() {
fn main() -> Result<()> {
let cli = command!()
.author("Aaron Seilis <[email protected]>")
.about("A simple map generator for role playing games")
Expand Down Expand Up @@ -97,13 +98,13 @@ fn main() {
match style.as_str() {
"halls" => {
map.generate_dungeon(num_rooms, 5);
map.place_entrance_near((width / 2, height / 2))
.expect("width/height is outside of map");
let point: Point = (width / 2, height / 2).try_into().unwrap();
map.place_entrance_near(point)?;
}
"cave" => {
map.generate_cave(4, 50);
map.place_entrance_near((width / 2, height / 2))
.expect("width/height is outside of map");
let point: Point = (width / 2, height / 2).try_into().unwrap();
map.place_entrance_near(point)?;
}
_ => unreachable!(),
}
Expand All @@ -115,4 +116,6 @@ fn main() {
Ok(_) => println!("Map generated: {}", filename),
Err(e) => println!("Error: {}", e),
}

Ok(())
}
2 changes: 1 addition & 1 deletion src/rpgtools/error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use thiserror::Error;

pub type Result<T, E=RpgError> = std::result::Result<T, E>;
pub type Result<T, E = RpgError> = std::result::Result<T, E>;

#[derive(Error, Debug)]
pub enum RpgError {
Expand Down
Loading

0 comments on commit 3ca2b64

Please sign in to comment.