Skip to content

Commit

Permalink
Begin refactoring aneri-pathfind; update Cargo.lock
Browse files Browse the repository at this point in the history
  • Loading branch information
Absolucy committed Oct 13, 2024
1 parent 5552ee7 commit 882e30a
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 7 deletions.
20 changes: 13 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/pathfind/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ meowtonin = { workspace = true }
pathfinding = "4"
bitflags = "2"
ordered-float = "4"
grid = "0.15"
16 changes: 16 additions & 0 deletions crates/pathfind/src/direction.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// SPDX-License-Identifier: MPL-2.0
use bitflags::bitflags;

bitflags! {
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Direction: u8 {
const NORTH = 1;
const SOUTH = 2;
const EAST = 4;
const WEST = 8;
const NORTHEAST = Self::NORTH.bits() | Self::EAST.bits();
const NORTHWEST = Self::NORTH.bits() | Self::WEST.bits();
const SOUTHEAST = Self::SOUTH.bits() | Self::EAST.bits();
const SOUTHWEST = Self::SOUTH.bits() | Self::WEST.bits();
}
}
7 changes: 7 additions & 0 deletions crates/pathfind/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
clippy::perf,
clippy::style
)]
pub mod direction;
pub mod map;
pub mod position;
pub mod tile;

/*
use ahash::AHashMap;
use bitflags::bitflags;
use ordered_float::OrderedFloat;
Expand Down Expand Up @@ -184,3 +190,4 @@ pub fn main() {
}
}
*/
*/
50 changes: 50 additions & 0 deletions crates/pathfind/src/map.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// SPDX-License-Identifier: MPL-2.0
use crate::{position::Position, tile::Tile};
use grid::Grid;

pub struct GameMap {
grid: Grid<Option<Tile>>,
}

impl GameMap {
pub fn new(width: usize, height: usize) -> Self {
Self {
grid: Grid::new(width, height),
}
}

pub fn get_tile(&self, pos: Position) -> Option<&Tile> {
self.grid
.get(pos.x(), pos.y())
.and_then(|tile| tile.as_ref())
}

pub fn get_tiles<T>(&self, pos_list: T) -> Vec<Option<&Tile>>
where
T: AsRef<[Position]>,
{
pos_list
.as_ref()
.iter()
.map(|&pos| self.get_tile(pos))
.collect()
}

pub fn set_tile<T>(&mut self, pos: Position, tile: T) -> Option<Tile>
where
T: Into<Option<Tile>>,
{
let tile_ref = self.grid.get_mut(pos.x(), pos.y())?;
let mut tile = tile.into();
std::mem::swap(tile_ref, &mut tile);
tile
}

pub fn width(&self) -> usize {
self.grid.rows()
}

pub fn height(&self) -> usize {
self.grid.cols()
}
}
39 changes: 39 additions & 0 deletions crates/pathfind/src/position.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// SPDX-License-Identifier: MPL-2.0

#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct Position {
x: u16,
y: u16,
}

impl Position {
/// Creates a new [`Position`], from 0-indexed x and y coordinates.
pub const fn new(x: u16, y: u16) -> Self {
Self { x, y }
}

/// Creates a new [`Position`], from 1-indexed x and y coordinates.
pub const fn new_1i(x: u16, y: u16) -> Self {
Self::new(x.saturating_sub(1), y.saturating_sub(1))
}

/// Gets the x coordinate of this [`Position`], as a 0-indexed [`u16`].
pub const fn x(&self) -> u16 {
self.x
}

/// Gets the y coordinate of this [`Position`], as a 0-indexed [`u16`].
pub const fn y(&self) -> u16 {
self.y
}

/// Gets the x coordinate of this [`Position`], as a 1-indexed [`u16`].
pub const fn x_1(&self) -> u16 {
self.x().saturating_add(1)
}

/// Gets the y coordinate of this [`Position`], as a 1-indexed [`u16`].
pub const fn y_1(&self) -> u16 {
self.y().saturating_add(1)
}
}
9 changes: 9 additions & 0 deletions crates/pathfind/src/tile.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// SPDX-License-Identifier: MPL-2.0
use crate::direction::Direction;
use ordered_float::OrderedFloat;

pub struct Tile {
pub weight: OrderedFloat<f32>,
pub passable_directions: Direction,
pub condition: Option<Box<dyn Fn() -> bool>>,
}

0 comments on commit 882e30a

Please sign in to comment.