Struct Map

Source
pub struct Map<'a> {
    block: Block<'a>,
    case_size: u16,
    viewport: Rect,
}
Expand description

A struct representing the game logic map. The map is resizable and represents the writeable area of the terminal, with a defined case size for each cell and a viewport for rendering the game logic world.

§Fields

  • block: A Block widget representing the map’s visual border and title.
  • CASE_SIZE: The size of each cell (case) in pixels on the terminal screen.
  • viewport: The dimensions of the terminal viewport for rendering the map.

Fields§

§block: Block<'a>§case_size: u16§viewport: Rect

Implementations§

Source§

impl Map<'_>

Source

pub fn new<'a>(case_size_in_px: u16, viewport: Rect) -> Map<'a>

Creates a new Map instance with a given case size and viewport.

§Parameters
  • case_size_in_px: The size of each case (cell) on the map in pixels.
  • viewport: The visible area of the terminal where the map will be rendered.
§Returns

A new Map instance with the specified case_size_in_px and viewport.

§Note

The map will have a double border and the title “Snake !”.

Source

pub fn resize_to_terminal(&mut self, viewport: Rect)

To resize the map viewport, not for in game logic use # unfair, but when after restarting it is OK

Source

pub fn out_of_map(&self, _: &Position) -> bool

Determines if a given position is outside the bounds of the map.

§Parameters
  • Position { x, y }: The position to check.
§Returns

true if the position is outside the map’s viewport, false otherwise.

§Example
use ratatui::layout::Rect;
use rsnaker::graphics::graphic_block::Position;
use rsnaker::graphics::sprites::map::Map;
let map = Map::new(10, Rect::new(0, 0, 100, 40));
let position = Position { x: 101, y: 20 };
assert!(map.out_of_map(&position));
Source

pub fn out_of_map_reverse_position(&self, _: &Position) -> Position

Reverses the position if it is outside the bounds of the map, effectively “wrapping” around to the opposite edge of the map.

§Parameters
  • Position { x, y }: The position to check and possibly adjust.
§Returns

A new Position where out-of-bounds coordinates are wrapped to the opposite edge of the map.

§Example
use ratatui::layout::Rect;
use rsnaker::graphics::graphic_block::Position;
use rsnaker::graphics::sprites::map::Map;
let map = Map::new(10, Rect::new(0, 0, 100, 40));
let position = Position { x: 101, y: 20 };
let new_position = map.out_of_map_reverse_position(&position);
assert_eq!(new_position.x, 10);  // Wrapped to the opposite side
Source

pub fn area(&self) -> &Rect

Returns the current viewport (the visible area) of the map.

§Returns

A reference to the Rect representing the map’s viewport.

Source

pub fn get_widget(&self) -> &Block<'_>

Returns the Block widget representing the map’s border and title.

§Returns

A reference to the Block widget.

Source

pub fn get_case_size(&self) -> u16

Returns the size of each case (cell) on the map.

§Returns

The size of each case in pixels.

Trait Implementations§

Source§

impl<'a> Clone for Map<'a>

Source§

fn clone(&self) -> Map<'a>

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

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

Performs copy-assignment from source. Read more

Auto Trait Implementations§

§

impl<'a> Freeze for Map<'a>

§

impl<'a> RefUnwindSafe for Map<'a>

§

impl<'a> Send for Map<'a>

§

impl<'a> Sync for Map<'a>

§

impl<'a> Unpin for Map<'a>

§

impl<'a> UnwindSafe for Map<'a>

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
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, 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.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V