Compare commits
10
Commits
dev-world
...
9c61078414
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9c61078414 | ||
|
|
d9b301ea04 | ||
|
|
3663c74bd9 | ||
|
|
6f292cbb21 | ||
|
|
b09fa9e2e6 | ||
|
|
88f5f7f61a | ||
|
|
7da7d578e1 | ||
|
|
6534ed18b4 | ||
|
|
d0c051c7b1 | ||
|
|
3196347936 |
No files matched your search
@@ -4,3 +4,5 @@ version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
crossterm = "0.29"
|
||||
rand = "0.8"
|
||||
@@ -0,0 +1,103 @@
|
||||
use std::{
|
||||
error,
|
||||
fmt::format,
|
||||
io::{Stdout, Write, stdout},
|
||||
process::Output,
|
||||
};
|
||||
|
||||
use crossterm::{
|
||||
cursor::MoveTo,
|
||||
execute, queue,
|
||||
style::{Color, Print, ResetColor, SetBackgroundColor, SetForegroundColor},
|
||||
terminal::{Clear, ClearType, enable_raw_mode},
|
||||
};
|
||||
|
||||
use super::TorusGrid;
|
||||
|
||||
pub enum PosCom {
|
||||
UP,
|
||||
DW,
|
||||
LF,
|
||||
RH,
|
||||
}
|
||||
|
||||
pub struct Render {
|
||||
out: Stdout,
|
||||
rez: (usize, usize),
|
||||
pos: (i32, i32),
|
||||
}
|
||||
|
||||
impl Render {
|
||||
pub fn new() -> Self {
|
||||
Render {
|
||||
out: stdout(),
|
||||
rez: (10, 10),
|
||||
pos: (0, 0),
|
||||
}
|
||||
}
|
||||
pub fn set_rez(&mut self, a: usize, b: usize) -> Result<(), String> {
|
||||
if a == 0 || b == 0 {
|
||||
return Err("Invalid resolution\n".to_string());
|
||||
}
|
||||
self.rez = (a, b);
|
||||
Ok(())
|
||||
}
|
||||
pub fn set_pos(&mut self, a: i32, b: i32) {
|
||||
self.pos = (a, b);
|
||||
}
|
||||
pub fn offset_pos(&mut self, a: PosCom) {
|
||||
match a {
|
||||
PosCom::LF => {
|
||||
self.pos.1 -= 1;
|
||||
}
|
||||
PosCom::UP => {
|
||||
self.pos.0 -= 1;
|
||||
}
|
||||
PosCom::DW => {
|
||||
self.pos.0 += 1;
|
||||
}
|
||||
PosCom::RH => {
|
||||
self.pos.1 += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
pub fn clear(&mut self) {
|
||||
_ = execute!(self.out, Clear(ClearType::All), MoveTo(0, 0));
|
||||
}
|
||||
pub fn print(&mut self, out_str: &str) {
|
||||
_ = execute!(self.out, Print(out_str));
|
||||
}
|
||||
pub fn print_c(&mut self, out_str: &str, c: Color) {
|
||||
_ = execute!(self.out, SetForegroundColor(c), Print(out_str), ResetColor);
|
||||
}
|
||||
pub fn print_err(&mut self, out_err: &str) {
|
||||
_ = execute!(
|
||||
self.out,
|
||||
SetForegroundColor(Color::Red),
|
||||
Print(out_err),
|
||||
ResetColor
|
||||
);
|
||||
}
|
||||
pub fn frame(&mut self, grid: &TorusGrid<i32>) {
|
||||
let mut out = self.out.lock();
|
||||
_ = queue!(out, MoveTo(0, 0));
|
||||
for i in 0..self.rez.0 {
|
||||
for j in 0..self.rez.1 {
|
||||
let a = grid.get_wrap((i as i32 + self.pos.0, j as i32 + self.pos.1));
|
||||
let b = *a as u8;
|
||||
_ = queue!(
|
||||
out,
|
||||
SetBackgroundColor(Color::Rgb { r: b, g: b, b: b }),
|
||||
Print(" "),
|
||||
ResetColor
|
||||
);
|
||||
}
|
||||
_ = queue!(out, Print("\r\n"));
|
||||
}
|
||||
_ = out.flush();
|
||||
self.print_c(
|
||||
&format!("pos is: {} {} \n", self.pos.1, self.pos.0),
|
||||
Color::Yellow,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
use core::error;
|
||||
use crossterm::{
|
||||
event::{self, Event, KeyCode},
|
||||
terminal::{disable_raw_mode, enable_raw_mode},
|
||||
};
|
||||
use std::io::{self, Write, stdout};
|
||||
|
||||
use crate::input;
|
||||
|
||||
pub enum ComandEvent {
|
||||
//who is this
|
||||
None,
|
||||
Exit,
|
||||
Key,
|
||||
}
|
||||
pub fn Comand_pars() -> Result<ComandEvent, String> {
|
||||
let mut input = String::new();
|
||||
io::stdin()
|
||||
.read_line(&mut input)
|
||||
.expect("error input scan\n");
|
||||
let mut words: Vec<&str> = input.trim().split(" ").collect();
|
||||
if words.len() == 0 {
|
||||
return Err(String::from("Zerro lenth comand\n"));
|
||||
}
|
||||
|
||||
match words[0] {
|
||||
"q" => {
|
||||
return Ok(ComandEvent::Exit);
|
||||
}
|
||||
"key" => {
|
||||
_ = enable_raw_mode();
|
||||
let mut out = stdout();
|
||||
_ = crossterm::execute!(out, crossterm::cursor::Hide);
|
||||
return Ok(ComandEvent::Key);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
||||
Ok(ComandEvent::None)
|
||||
}
|
||||
|
||||
pub enum KeyEvent {
|
||||
//KeuEvent for your input
|
||||
None,
|
||||
Exit,
|
||||
Comand,
|
||||
A,
|
||||
W,
|
||||
S,
|
||||
D,
|
||||
}
|
||||
pub fn Key_pars() -> Result<Option<KeyEvent>, std::io::Error> {
|
||||
if let Event::Key(key) = event::read()? {
|
||||
match key.code {
|
||||
KeyCode::Char('q') => {
|
||||
_ = disable_raw_mode();
|
||||
let mut out = stdout();
|
||||
_ = crossterm::execute!(out, crossterm::cursor::Show);
|
||||
return Ok(Some(KeyEvent::Exit));
|
||||
}
|
||||
KeyCode::Char('c') => {
|
||||
_ = disable_raw_mode();
|
||||
let mut out = stdout();
|
||||
_ = crossterm::execute!(out, crossterm::cursor::Show);
|
||||
return Ok(Some(KeyEvent::Comand));
|
||||
}
|
||||
KeyCode::Char('a') => {
|
||||
return Ok(Some(KeyEvent::A));
|
||||
}
|
||||
KeyCode::Char('w') => {
|
||||
return Ok(Some(KeyEvent::W));
|
||||
}
|
||||
KeyCode::Char('s') => {
|
||||
return Ok(Some(KeyEvent::S));
|
||||
}
|
||||
KeyCode::Char('d') => {
|
||||
return Ok(Some(KeyEvent::D));
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
Ok(Some(KeyEvent::None))
|
||||
}
|
||||
+86
-3
@@ -1,7 +1,90 @@
|
||||
|
||||
|
||||
use rand::Rng;
|
||||
mod world;
|
||||
pub use world::*;
|
||||
|
||||
mod input;
|
||||
use input::*;
|
||||
|
||||
mod console_render;
|
||||
use console_render::*;
|
||||
|
||||
enum Input_type {
|
||||
comand,
|
||||
keybard,
|
||||
}
|
||||
fn main() {
|
||||
println!("Hello, dev!");
|
||||
println!("Hello, world!");
|
||||
let mut rng = rand::thread_rng();
|
||||
let mut it = Input_type::comand;
|
||||
let mut R = Render::new();
|
||||
R.clear();
|
||||
R.set_rez(40, 80);
|
||||
|
||||
let g_rez: (usize, usize) = (100, 50);
|
||||
let mut grid: TorusGrid<i32> = TorusGrid::new(0, g_rez);
|
||||
|
||||
for i in 0..g_rez.0 {
|
||||
for j in 0..g_rez.1 {
|
||||
if let Some(a) = grid.get_mut((i as i32, j as i32)) {
|
||||
*a = rng.gen_range(0..=255) as i32;
|
||||
}
|
||||
}
|
||||
}
|
||||
loop {
|
||||
//R.clear();
|
||||
R.frame(&grid);
|
||||
match it {
|
||||
Input_type::comand => {
|
||||
R.print("Input your comand: ");
|
||||
let input = Comand_pars();
|
||||
if input.is_err() {
|
||||
R.print_err(&format!("ERROR: {}\n", input.err().unwrap()));
|
||||
} else {
|
||||
match input.unwrap() {
|
||||
ComandEvent::Exit => {
|
||||
R.print("progarm is end\n");
|
||||
return;
|
||||
}
|
||||
ComandEvent::Key => {
|
||||
R.print("jamp to keybard\n");
|
||||
it = Input_type::keybard
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
Input_type::keybard => {
|
||||
let input = Key_pars();
|
||||
if input.is_err() {
|
||||
R.print_err(&format!("ERROR: {}", input.err().unwrap()));
|
||||
} else {
|
||||
if let Some(k) = input.unwrap() {
|
||||
match k {
|
||||
KeyEvent::Exit => {
|
||||
R.print("progarm is end\n");
|
||||
return;
|
||||
}
|
||||
KeyEvent::Comand => {
|
||||
R.print("jamp to comand");
|
||||
it = Input_type::comand
|
||||
}
|
||||
KeyEvent::A => {
|
||||
R.offset_pos(PosCom::LF);
|
||||
}
|
||||
KeyEvent::W => {
|
||||
R.offset_pos(PosCom::UP);
|
||||
}
|
||||
KeyEvent::S => {
|
||||
R.offset_pos(PosCom::DW);
|
||||
}
|
||||
KeyEvent::D => {
|
||||
R.offset_pos(PosCom::RH);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+2
-10
@@ -1,8 +1,3 @@
|
||||
use std::usize;
|
||||
|
||||
|
||||
|
||||
|
||||
fn wrap_pos(p: (i32, i32), s: (usize, usize)) -> (usize, usize) {
|
||||
let x = ((p.0 % s.0 as i32 + s.0 as i32) % s.0 as i32) as usize;
|
||||
let y = ((p.1 % s.1 as i32 + s.1 as i32) % s.1 as i32) as usize;
|
||||
@@ -10,7 +5,7 @@ fn wrap_pos(p: (i32, i32), s: (usize, usize)) -> (usize, usize) {
|
||||
(x, y)
|
||||
}
|
||||
|
||||
struct TorusGrid<T: Clone>{
|
||||
pub struct TorusGrid<T: Clone> {
|
||||
vec: Vec<T>,
|
||||
size: (usize, usize),
|
||||
}
|
||||
@@ -18,10 +13,7 @@ struct TorusGrid<T: Clone>{
|
||||
impl<T: Clone> TorusGrid<T> {
|
||||
pub fn new(value: T, s: (usize, usize)) -> Self {
|
||||
let v = vec![value; s.0 * s.1];
|
||||
TorusGrid {
|
||||
vec: v,
|
||||
size: s
|
||||
}
|
||||
TorusGrid { vec: v, size: s }
|
||||
}
|
||||
fn index(&self, x: usize, y: usize) -> usize {
|
||||
y * self.size.0 + x
|
||||
|
||||
Reference in new issue
Block a user