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
: ABlock
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<'_>
impl Map<'_>
Sourcepub fn new<'a>(case_size_in_px: u16, viewport: Rect) -> Map<'a>
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 !”.
Sourcepub fn resize_to_terminal(&mut self, viewport: Rect)
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
Sourcepub fn out_of_map(&self, _: &Position) -> bool
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));
Sourcepub fn out_of_map_reverse_position(&self, _: &Position) -> Position
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
Sourcepub fn area(&self) -> &Rect
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.
Sourcepub fn get_widget(&self) -> &Block<'_>
pub fn get_widget(&self) -> &Block<'_>
Returns the Block
widget representing the map’s border and title.
§Returns
A reference to the Block
widget.
Sourcepub fn get_case_size(&self) -> u16
pub fn get_case_size(&self) -> u16
Trait Implementations§
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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