-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Begin refactoring
aneri-pathfind
; update Cargo.lock
- Loading branch information
Showing
7 changed files
with
135 additions
and
7 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,3 +14,4 @@ meowtonin = { workspace = true } | |
pathfinding = "4" | ||
bitflags = "2" | ||
ordered-float = "4" | ||
grid = "0.15" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>>, | ||
} |