Compare commits
21
Commits
dev-world
...
b565771db2
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b565771db2 | ||
|
|
021d91d1ea | ||
|
|
9a2a3937e2 | ||
|
|
a02b40c043 | ||
|
|
60bd764bcf | ||
|
|
93b0c65fed | ||
|
|
d4cfb25c8d | ||
|
|
cac35a4047 | ||
|
|
0eab202b00 | ||
|
|
125fd73625 | ||
|
|
66c0bb839e | ||
|
|
9c61078414 | ||
|
|
d9b301ea04 | ||
|
|
3663c74bd9 | ||
|
|
6f292cbb21 | ||
|
|
b09fa9e2e6 | ||
|
|
88f5f7f61a | ||
|
|
7da7d578e1 | ||
|
|
6534ed18b4 | ||
|
|
d0c051c7b1 | ||
|
|
3196347936 |
No files matched your search
+2
-2
@@ -4,6 +4,6 @@ version = "0.1.0"
|
|||||||
edition = "2024"
|
edition = "2024"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
crossterm = "0.29"
|
||||||
|
rand = "0.8"
|
||||||
smallvec = "1.15"
|
smallvec = "1.15"
|
||||||
|
|
||||||
|
|
||||||
@@ -0,0 +1,117 @@
|
|||||||
|
use std::sync::{Arc, Mutex};
|
||||||
|
|
||||||
|
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::Agent;
|
||||||
|
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<bool>, a_grid: &TorusGrid<Option<usize>>) {
|
||||||
|
let mut out = self.out.lock();
|
||||||
|
_ = queue!(out, Clear(ClearType::All), MoveTo(0, 0));
|
||||||
|
for i in 0..self.rez.0 {
|
||||||
|
for j in 0..self.rez.1 {
|
||||||
|
let ca: Color;
|
||||||
|
let cg: Color;
|
||||||
|
let a = a_grid.get_wrap((i as i32 + self.pos.0, j as i32 + self.pos.1));
|
||||||
|
let g = grid.get_wrap((i as i32 + self.pos.0, j as i32 + self.pos.1));
|
||||||
|
let c = *g as u8 * 200;
|
||||||
|
cg = Color::Rgb { r: c, g: c, b: c };
|
||||||
|
if a.is_some() {
|
||||||
|
//c = Color::Red;
|
||||||
|
ca = Color::Red;
|
||||||
|
} else {
|
||||||
|
ca = cg;
|
||||||
|
}
|
||||||
|
_ = queue!(
|
||||||
|
out,
|
||||||
|
SetBackgroundColor(cg),
|
||||||
|
SetForegroundColor(ca),
|
||||||
|
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,88 @@
|
|||||||
|
use core::error;
|
||||||
|
use crossterm::{
|
||||||
|
cursor::Show,
|
||||||
|
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,
|
||||||
|
P,
|
||||||
|
}
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
KeyCode::Char('p') => {
|
||||||
|
return Ok(Some(KeyEvent::P));
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(Some(KeyEvent::None))
|
||||||
|
}
|
||||||
+95
-29
@@ -1,43 +1,109 @@
|
|||||||
|
use std::{clone, sync::atomic::AtomicI32};
|
||||||
|
|
||||||
|
use rand::Rng;
|
||||||
mod world;
|
mod world;
|
||||||
use world::*;
|
use world::*;
|
||||||
|
|
||||||
|
mod input;
|
||||||
|
use input::*;
|
||||||
|
|
||||||
|
mod console_render;
|
||||||
|
use console_render::*;
|
||||||
|
|
||||||
|
enum InputType {
|
||||||
|
comand,
|
||||||
|
keybard,
|
||||||
|
}
|
||||||
fn main() {
|
fn main() {
|
||||||
println!("Hello, dev!");
|
let mut rnd = rand::thread_rng();
|
||||||
|
let mut it = InputType::comand;
|
||||||
|
let mut R = Render::new();
|
||||||
|
R.clear();
|
||||||
|
_ = R.set_rez(40, 80);
|
||||||
|
|
||||||
let mut ma: MagicContainer<i32> = MagicContainer::new();
|
let rez: (usize, usize) = (100, 100);
|
||||||
|
let mut w = world::World::new(rez);
|
||||||
|
|
||||||
ma.add(2);
|
/*
|
||||||
ma.add(3);
|
for i in 0..rez.0 {
|
||||||
ma.add(4);
|
for j in 0..rez.1 {
|
||||||
ma.add(5);
|
if let Some(a) = w.get_mut_grid().get_mut((i as i32, j as i32)) {
|
||||||
ma.add(6);
|
*a = rnd.gen_range(0..=1) == 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
ma.debug_log();
|
for mut i in 0..20 {
|
||||||
|
let x = rnd.gen_range(0..=rez.0) as i32;
|
||||||
|
let y = rnd.gen_range(0..=rez.1) as i32;
|
||||||
|
let a = w.add_agent(world::Agent::new(x, y, 0));
|
||||||
|
if a.is_err() {
|
||||||
|
R.print_err(a.err().unwrap());
|
||||||
|
i -= 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if let Err(a) = ma.rm(1) {
|
loop {
|
||||||
println!("ERROR: {:?}", a);
|
//R.clear();
|
||||||
|
R.frame(w.get_grid(), w.get_a_grid());
|
||||||
|
R.print(&w.debug());
|
||||||
|
match it {
|
||||||
|
InputType::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;
|
return;
|
||||||
}
|
}
|
||||||
|
ComandEvent::Key => {
|
||||||
ma.debug_log();
|
R.print("jamp to keybard\n");
|
||||||
|
it = InputType::keybard
|
||||||
if let Err(a) = ma.rm(0) {
|
}
|
||||||
println!("ERROR: {:?}", a);
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
InputType::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;
|
return;
|
||||||
}
|
}
|
||||||
|
KeyEvent::Comand => {
|
||||||
ma.debug_log();
|
R.print("jamp to comand");
|
||||||
|
it = InputType::comand
|
||||||
|
}
|
||||||
ma.add(8);
|
KeyEvent::A => {
|
||||||
|
R.offset_pos(PosCom::LF);
|
||||||
ma.debug_log();
|
}
|
||||||
|
KeyEvent::W => {
|
||||||
ma.add(9);
|
R.offset_pos(PosCom::UP);
|
||||||
ma.add(10);
|
}
|
||||||
ma.add(11);
|
KeyEvent::S => {
|
||||||
|
R.offset_pos(PosCom::DW);
|
||||||
ma.debug_log();
|
}
|
||||||
|
KeyEvent::D => {
|
||||||
|
R.offset_pos(PosCom::RH);
|
||||||
|
}
|
||||||
|
KeyEvent::P => {
|
||||||
|
w.update();
|
||||||
|
w.render_grid_updaete();
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
+9
-2
@@ -1,7 +1,10 @@
|
|||||||
use smallvec::{SmallVec, smallvec};
|
use smallvec::{SmallVec, smallvec};
|
||||||
|
|
||||||
mod grid;
|
mod grid;
|
||||||
use std::{fmt::format, sync::{Arc, Mutex}};
|
use std::{
|
||||||
|
fmt::format,
|
||||||
|
sync::{Arc, Mutex},
|
||||||
|
};
|
||||||
|
|
||||||
pub use grid::TorusGrid;
|
pub use grid::TorusGrid;
|
||||||
|
|
||||||
@@ -126,7 +129,11 @@ impl World {
|
|||||||
}
|
}
|
||||||
fn debug_a(&self, n: usize) -> Option<String> {
|
fn debug_a(&self, n: usize) -> Option<String> {
|
||||||
if let Some(a) = self.agents.get(n) {
|
if let Some(a) = self.agents.get(n) {
|
||||||
return Some(format!("pos: {:?}, ang: {:?}, id: {n}", a.get_pos(), a.get_a()));
|
return Some(format!(
|
||||||
|
"pos: {:?}, ang: {:?}, id: {n}",
|
||||||
|
a.get_pos(),
|
||||||
|
a.get_a()
|
||||||
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
None
|
None
|
||||||
|
|||||||
Reference in new issue
Block a user