2018-07-02 06:40:07 +10:00
|
|
|
use super::variables::{Cell, Position, Size };
|
2018-08-12 01:58:15 +10:00
|
|
|
use crossterm::{Crossterm, Screen};
|
|
|
|
use crossterm::cursor::cursor;
|
2018-07-09 06:13:32 +10:00
|
|
|
use crossterm::style::{ObjectStyle, StyledObject, Color};
|
2018-07-02 06:40:07 +10:00
|
|
|
|
|
|
|
use std::fmt::Display;
|
|
|
|
|
2018-07-09 06:13:32 +10:00
|
|
|
pub struct Map
|
2018-07-02 06:40:07 +10:00
|
|
|
{
|
2018-07-09 06:13:32 +10:00
|
|
|
pub map: Vec<Vec<Cell>>,
|
|
|
|
pub size: Size,
|
2018-07-02 06:40:07 +10:00
|
|
|
}
|
|
|
|
|
2018-07-09 06:13:32 +10:00
|
|
|
impl Map
|
2018-07-02 06:40:07 +10:00
|
|
|
{
|
2018-07-09 06:13:32 +10:00
|
|
|
pub fn new(map_size: Size, wall_cell_char: char, map_cell_char: char) -> Map
|
2018-07-02 06:40:07 +10:00
|
|
|
{
|
2018-07-09 06:13:32 +10:00
|
|
|
let mut map: Vec<Vec<Cell>> = Vec::new();
|
|
|
|
|
|
|
|
// initialize the map shown on the screen. Each cell of terminal should have a value that could be changed by the algorithm
|
|
|
|
// create n rows with n cells.
|
|
|
|
for y in 0..map_size.height
|
|
|
|
{
|
|
|
|
let mut row: Vec<Cell> = Vec::new();
|
|
|
|
|
|
|
|
for x in 0..map_size.width
|
2018-08-12 01:58:15 +10:00
|
|
|
{
|
|
|
|
if (y == 0 || y == map_size.height - 1) || (x == 0 || x == map_size.width - 1)
|
|
|
|
{
|
|
|
|
row.push(Cell::new(Position::new(x, y), Color::Black, wall_cell_char, true));
|
|
|
|
} else {
|
|
|
|
row.push(Cell::new(Position::new(x, y), Color::Black, map_cell_char, false));
|
2018-07-09 06:13:32 +10:00
|
|
|
}
|
2018-08-12 01:58:15 +10:00
|
|
|
}
|
2018-07-09 06:13:32 +10:00
|
|
|
map.push(row);
|
|
|
|
}
|
|
|
|
|
|
|
|
Map { map: map, size: Size::new(map_size.width, map_size.height)}
|
2018-07-02 06:40:07 +10:00
|
|
|
}
|
|
|
|
|
2018-07-09 06:13:32 +10:00
|
|
|
// render the map on the screen.
|
2018-08-12 01:58:15 +10:00
|
|
|
pub fn render_map(&mut self, screen: &Screen)
|
2018-07-02 06:40:07 +10:00
|
|
|
{
|
2018-11-15 02:53:27 +11:00
|
|
|
let crossterm = Crossterm::from_screen(screen);
|
2018-07-02 06:40:07 +10:00
|
|
|
|
2018-07-09 06:13:32 +10:00
|
|
|
for row in self.map.iter_mut()
|
2018-07-02 06:40:07 +10:00
|
|
|
{
|
2018-07-09 06:13:32 +10:00
|
|
|
for column in row.iter_mut()
|
2018-07-13 03:48:13 +10:00
|
|
|
{
|
|
|
|
// we only have to render the walls
|
|
|
|
if (column.position.y == 0 || column.position.y == self.size.height - 1) || (column.position.x == 0 || column.position.x == self.size.width - 1)
|
2018-08-12 01:58:15 +10:00
|
|
|
{
|
|
|
|
let cell_style = crossterm.style(column.look).on(column.color);
|
2018-11-15 02:53:27 +11:00
|
|
|
cursor().goto(column.position.x as u16, column.position.y as u16);
|
2018-08-12 01:58:15 +10:00
|
|
|
cell_style.paint(&screen);
|
|
|
|
}
|
2018-07-13 03:48:13 +10:00
|
|
|
}
|
2018-07-02 06:40:07 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-09 06:13:32 +10:00
|
|
|
// check if position in the map at the given coords is visted.
|
|
|
|
pub fn is_cell_visited(&self, x: usize, y: usize) -> bool
|
2018-07-02 06:40:07 +10:00
|
|
|
{
|
2018-07-09 06:13:32 +10:00
|
|
|
self.map[y][x].visited
|
|
|
|
}
|
2018-07-02 06:40:07 +10:00
|
|
|
|
2018-07-09 06:13:32 +10:00
|
|
|
// change an position in the map to visited.
|
|
|
|
pub fn set_visited(&mut self, x: usize, y: usize)
|
|
|
|
{
|
|
|
|
self.map[y][x].visited = true;
|
2018-07-02 06:40:07 +10:00
|
|
|
}
|
|
|
|
}
|