Struct Grid

Source
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>
where Cell: Clone + Default,

Source

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,

Source

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>
where Cell: Clone + Copy,

Source

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);
Source

pub fn cell_at<Point: GridPosition>(&self, position: Point) -> Cell

Get the cell value at some position.

Source

pub fn new(width: usize, height: usize, value: Cell) -> Self

Construct a new grid with width, height and an initial value.

Source§

impl<Cell> Grid<Cell>

Source

pub fn set_cell<Point: GridPosition>(&mut self, position: Point, value: Cell)

Set the cell value at some position.

Source

pub fn cells(&self) -> Iter<'_, Cell>

An iterator visiting the cells in order of memory.

Source

pub fn mut_cells(&mut self) -> IterMut<'_, Cell>

An iterator visiting the cells mutably in order of memory.

Source

pub fn iter<Point: GridPosition>(&self) -> GridIter<'_, Cell, Point>

An iterator visiting the cell and associated position in the grid.

Source

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));
Source

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);
Source

pub fn len(&self) -> usize

Returns the number of cells in the grid.

Source

pub fn is_empty(&self) -> bool

Check whether teh grid is empty.

Source

pub fn width(&self) -> usize

Returns the width of the grid.

Source

pub fn height(&self) -> usize

Returns the height of the grid.

Source

pub fn is_in_bounds<Point: GridPosition>(&self, position: Point) -> bool

Check if a position is in the grid bounds.

Trait Implementations§

Source§

impl<Cell: Clone> Clone for Grid<Cell>

Source§

fn clone(&self) -> Grid<Cell>

Returns a duplicate of the value. Read more
1.0.0 · Source§

const fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<Cell: Debug> Debug for Grid<Cell>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<Cell> Display for Grid<Cell>
where char: From<Cell>, Cell: Copy,

Source§

fn fmt(&self, formater: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<Cell> FromStr for Grid<Cell>
where Cell: Default + TryFrom<char> + Clone,

Source§

type Err = ParseGridError<<Cell as TryFrom<char>>::Error>

The associated error which can be returned from parsing.
Source§

fn from_str(string: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
Source§

impl<Cell, Point: GridPosition> Index<Point> for Grid<Cell>

Source§

type Output = Cell

The returned type after indexing.
Source§

fn index(&self, position: Point) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<Cell> Index<usize> for Grid<Cell>

Source§

type Output = Cell

The returned type after indexing.
Source§

fn index(&self, index: usize) -> &Self::Output

Performs the indexing (container[index]) operation. Read more

Auto Trait Implementations§

§

impl<Cell> Freeze for Grid<Cell>

§

impl<Cell> RefUnwindSafe for Grid<Cell>
where Cell: RefUnwindSafe,

§

impl<Cell> Send for Grid<Cell>
where Cell: Send,

§

impl<Cell> Sync for Grid<Cell>
where Cell: Sync,

§

impl<Cell> Unpin for Grid<Cell>
where Cell: Unpin,

§

impl<Cell> UnwindSafe for Grid<Cell>
where Cell: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.