diff --git a/src/grid/mod.rs b/src/grid/mod.rs index dddf2de..21444e0 100644 --- a/src/grid/mod.rs +++ b/src/grid/mod.rs @@ -50,6 +50,7 @@ pub struct Grid { maze: Vec, rez: (usize, usize), pos: (i32, i32), + vin_pos: (i32, i32), size: usize, his: Vec<(i32, i32)>, } @@ -60,6 +61,7 @@ impl Grid { maze: vec![Status::init(false, true, true); r.0 * r.1], rez: r.clone(), pos: (1, 1), + vin_pos: (r.0 as i32 - 1, r.1 as i32 - 1), size: r.0 * r.1, his: Vec::new(), } @@ -69,8 +71,8 @@ impl Grid { let y = self.pos.1 + pos.1; (x >= 0 && x < self.rez.0 as i32) && (y >= 0 && y < self.rez.1 as i32) } - fn this_is_pos(&self, pos: &(i32, i32)) -> bool { - (pos.0 >= 0 && pos.0 < self.rez.0 as i32) && (pos.1 >= 0 && pos.1 < self.rez.1 as i32) + pub fn is_vin(&self) -> bool { + self.pos.0 == self.vin_pos.0 && self.pos.1 == self.vin_pos.1 } fn index(&self) -> usize { (self.pos.1 * self.rez.0 as i32 + self.pos.0) as usize @@ -87,6 +89,9 @@ impl Grid { pub fn get_cursor(&self) -> (i32, i32) { self.pos.clone() } + pub fn get_vin(&self) -> (i32, i32) { + self.vin_pos.clone() + } fn mut_pos(&mut self, pos: &(i32, i32)) -> bool { if self.is_pos(pos) { self.pos.0 += pos.0.clone(); @@ -95,6 +100,37 @@ impl Grid { } return false; } + pub fn game_pace(&mut self, pos: &(i32, i32)) { + if self.is_pos(pos) { + match pos { + (1, 0) => { + let p = self.index(); + if !self.maze[p].r_wall { + self.mut_pos(pos); + } + } + (-1, 0) => { + let p = self.get_index(&((self.pos.0 - 1) as usize, self.pos.1 as usize)); + if !self.maze[p].r_wall { + self.mut_pos(pos); + } + } + (0, 1) => { + let p = self.index(); + if !self.maze[p].d_wall { + self.mut_pos(pos); + } + } + (0, -1) => { + let p = self.get_index(&(self.pos.0 as usize, (self.pos.1 - 1) as usize)); + if !self.maze[p].d_wall { + self.mut_pos(pos); + } + } + _ => {} + } + } + } pub fn pace(&mut self, pos: &(i32, i32)) { if self.is_pos(pos) { diff --git a/src/main.rs b/src/main.rs index 891602f..aa26402 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,7 +2,9 @@ mod grid; mod input; mod render; -use crossterm::cursor::Show; +use std::io::Stdout; + +use crossterm::{cursor::Show, terminal::disable_raw_mode}; use grid::*; use input::*; use render::*; @@ -20,6 +22,18 @@ fn main() { let mut space = Grid::init(&rez); space.rand_gen(); loop { + if space.is_vin() { + _ = r.print(&"You is vin!"); + match it { + InputType::keyboard => { + let mut out = std::io::stdout(); + _ = crossterm::execute!(out, crossterm::cursor::Show); + _ = disable_raw_mode(); + } + _ => {} + } + return; + } _ = r.clear(); _ = r.frame(&space); match it { @@ -56,16 +70,16 @@ fn main() { it = InputType::comand; } KeyEvent::A => { - space.pace(&(-1, 0)); + space.game_pace(&(-1, 0)); } KeyEvent::W => { - space.pace(&(0, -1)); + space.game_pace(&(0, -1)); } KeyEvent::S => { - space.pace(&(0, 1)); + space.game_pace(&(0, 1)); } KeyEvent::D => { - space.pace(&(1, 0)); + space.game_pace(&(1, 0)); } KeyEvent::R => { if let Some(k) = space.rand_pace() { diff --git a/src/render/mod.rs b/src/render/mod.rs index db16c9a..921b281 100644 --- a/src/render/mod.rs +++ b/src/render/mod.rs @@ -3,7 +3,7 @@ use std::io::{Stdout, Write, stdout}; use crossterm::{ cursor::MoveTo, queue, - style::{Color, Print, ResetColor, SetBackgroundColor}, + style::{Color, Print, ResetColor, SetBackgroundColor, SetForegroundColor}, terminal::{Clear, ClearType}, }; @@ -37,7 +37,11 @@ impl Render { Ok(()) } pub fn frame(&mut self, g: &Grid) -> Result<(), std::io::Error> { - queue!(self.out, MoveTo(0, 0))?; + queue!( + self.out, + SetBackgroundColor(Color::Grey), + SetForegroundColor(Color::Black) + )?; for j in 0..self.rez.0 * 2 + 1 { match (j % 2) == 0 { true => { @@ -48,48 +52,50 @@ impl Render { } } } - queue!(self.out, Print("\r\n"))?; + queue!(self.out, ResetColor, Print("\r\n"))?; for i in 0..self.rez.1 { + queue!( + self.out, + SetBackgroundColor(Color::Grey), + SetForegroundColor(Color::Black) + )?; queue!(self.out, Print("║"))?; for j in 0..self.rez.0 { let r = g.who_in(&(j, i)); let c = g.get_cursor(); + let v = g.get_vin(); if c.0 as usize == j && c.1 as usize == i { + queue!(self.out, SetBackgroundColor(Color::Magenta))?; + queue!(self.out, Print(" "), ResetColor)?; + queue!( + self.out, + SetBackgroundColor(Color::Grey), + SetForegroundColor(Color::Black) + )?; + } else if v.0 as usize == j && v.1 as usize == i { queue!(self.out, SetBackgroundColor(Color::Green))?; queue!(self.out, Print(" "), ResetColor)?; + queue!( + self.out, + SetBackgroundColor(Color::Grey), + SetForegroundColor(Color::Black) + )?; } else { - if r.who_in('t').unwrap_or(false) { - queue!( - self.out, - SetBackgroundColor(Color::Magenta), - Print(" "), - ResetColor - )?; - } else { - queue!( - self.out, - SetBackgroundColor(Color::DarkGrey), - Print(" "), - ResetColor - )?; - } + queue!(self.out, Print(" "))?; } - /* - if r.who_in('d').unwrap_or(false) { - queue!(self.out, Print("═"))?; - } else { - queue!(self.out, Print(" "))?; - } - */ if r.who_in('r').unwrap_or(false) { queue!(self.out, Print("║"))?; } else { queue!(self.out, Print(" "))?; } - queue!(self.out, ResetColor)?; } - queue!(self.out, Print("\r\n"))?; + queue!(self.out, ResetColor, Print("\r\n"))?; + queue!( + self.out, + SetBackgroundColor(Color::Grey), + SetForegroundColor(Color::Black) + )?; queue!(self.out, Print("╬"))?; for j in 0..self.rez.0 { let r = g.who_in(&(j, i)); @@ -98,16 +104,9 @@ impl Render { } else { queue!(self.out, Print(" "))?; } - /* - if r.who_in('r').unwrap_or(false) { - queue!(self.out, Print("║"))?; - } else { - queue!(self.out, Print(" "))?; - } - */ queue!(self.out, Print("╬"))?; } - queue!(self.out, Print("\r\n"))?; + queue!(self.out, ResetColor, Print("\r\n"))?; } self.out.flush()?; Ok(())