Struct game_grid::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§

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.

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.

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

Get the cell value at some position.

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

Set the cell value at some position.

An iterator visiting the cells in order of memory.

An iterator visiting the cells mutably in order of memory.

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

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

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

Returns the number of cells in the grid.

Check whether teh grid is empty.

Returns the width of the grid.

Returns the height of the grid.

Check if a position is in the grid bounds.

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Formats the value using the given formatter. Read more
The associated error which can be returned from parsing.
Parses a string s to return a value of this type. Read more
The returned type after indexing.
Performs the indexing (container[index]) operation. Read more
The returned type after indexing.
Performs the indexing (container[index]) operation. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
Converts the given value to a String. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.