pub struct Grid<Cell> { /* private fields */ }
Expand description
A struct maintaining a grid usable for game prototyping.
The grid is stored as a linear Vec
containing cells and Grid provides
functions to look up and write to the grid with 2-dimentional vector types implementing the trait GridPosition
use game_grid::*;
#[derive(Clone, Copy)]
enum Cell {
Wall,
Empty,
}
#[derive(GridPosition, PartialEq, Eq, Debug)]
struct Point {
x: i32,
y: i32,
}
// Create a 2x2 grid with empty cells.
let mut grid: Grid<Cell> = Grid::new(2, 2, Cell::Empty);
assert_eq!(grid.width(), 2);
assert_eq!(grid.height(), 2);
// Add a wall at cell (0, 0).
grid.set_cell(Point::new(0, 0), Cell::Wall);
Implementations§
Source§impl<Cell> Grid<Cell>
impl<Cell> Grid<Cell>
Sourcepub fn from_slice(width: usize, data: &[Cell]) -> Self
pub fn from_slice(width: usize, data: &[Cell]) -> Self
Construct a grid from a slice and the desired row width.
use game_grid::*;
// Create a 2x2 grid with some data.
let grid: Grid<i32> = Grid::from_slice(2, &[0, 1, 2, 3]);
assert_eq!(grid.width(), 2);
assert_eq!(grid.height(), 2);
Any slice with width equal to 0 will produce an empty grid. If the length of input slice is not a multiple of width, the last row will be filled with default cell values so that the grid is square.
Source§impl<Cell> Grid<Cell>where
Cell: Clone,
impl<Cell> Grid<Cell>where
Cell: Clone,
Sourcepub fn from_slice_exact(width: usize, data: &[Cell]) -> Self
pub fn from_slice_exact(width: usize, data: &[Cell]) -> Self
Construct a grid from a slice and the desired row width. Any slice with width equal to 0 will produce an empty grid. The function will panic if the length of the input slice is not a multiple of width.
Source§impl<Cell> Grid<Cell>
impl<Cell> Grid<Cell>
Sourcepub fn flip_y(self) -> Self
pub fn flip_y(self) -> Self
Flips the order of the lines vertically. Useful when the game’s y axis is upwards.
§Example:
use game_grid::Grid;
let string_grid = "aaa
bbb
ccc";
let grid = string_grid.parse::<Grid<char>>().unwrap().flip_y();
let string_grid_flipped = "ccc
bbb
aaa";
assert_eq!(grid.to_string(), string_grid_flipped);
Sourcepub fn cell_at<Point: GridPosition>(&self, position: Point) -> Cell
pub fn cell_at<Point: GridPosition>(&self, position: Point) -> Cell
Get the cell value at some position.
Source§impl<Cell> Grid<Cell>
impl<Cell> Grid<Cell>
Sourcepub fn set_cell<Point: GridPosition>(&mut self, position: Point, value: Cell)
pub fn set_cell<Point: GridPosition>(&mut self, position: Point, value: Cell)
Set the cell value at some position.
Sourcepub fn mut_cells(&mut self) -> IterMut<'_, Cell>
pub fn mut_cells(&mut self) -> IterMut<'_, Cell>
An iterator visiting the cells mutably in order of memory.
Sourcepub fn iter<Point: GridPosition>(&self) -> GridIter<'_, Cell, Point> ⓘ
pub fn iter<Point: GridPosition>(&self) -> GridIter<'_, Cell, Point> ⓘ
An iterator visiting the cell and associated position in the grid.
Sourcepub fn position_for_index<Point: GridPosition>(&self, index: usize) -> Point
pub fn position_for_index<Point: GridPosition>(&self, index: usize) -> Point
Get the 2D position for an index in the linear array. index = y * width + x
use game_grid::*;
// A 2D point struct deriving GridPosition.
#[derive(GridPosition, PartialEq, Eq, Debug)]
struct Point {
x: i32,
y: i32,
}
let grid = Grid::<i32>::new(2, 2, 0);
assert_eq!(grid.position_for_index::<Point>(3), Point::new(1, 1));
Sourcepub fn index_for_position<Point: GridPosition>(&self, position: Point) -> usize
pub fn index_for_position<Point: GridPosition>(&self, position: Point) -> usize
Get the index in the linear array for a 2D position. Index = y * width + x.
use game_grid::*;
// A 2D point struct deriving GridPosition.
#[derive(GridPosition, PartialEq, Eq, Debug)]
struct Point {
x: i32,
y: i32,
}
let grid = Grid::<i32>::new(2, 2, 0);
assert_eq!(grid.index_for_position(Point::new(1, 1)), 3);
Sourcepub fn is_in_bounds<Point: GridPosition>(&self, position: Point) -> bool
pub fn is_in_bounds<Point: GridPosition>(&self, position: Point) -> bool
Check if a position is in the grid bounds.