rewrote handle_killed, added datastructure for different creatures
This commit is contained in:
parent
3cc0e1bd10
commit
6d864e73c9
@ -6,6 +6,13 @@ use crossterm::style::Color;
|
|||||||
use crate::TERM_SIZE;
|
use crate::TERM_SIZE;
|
||||||
use crate::part_handler;
|
use crate::part_handler;
|
||||||
|
|
||||||
|
#[derive(PartialEq, Clone)]
|
||||||
|
pub enum Species {
|
||||||
|
NormalSnake,
|
||||||
|
DetachedSnake,
|
||||||
|
Wesp
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(PartialEq, Eq, Hash, Copy, Clone)]
|
#[derive(PartialEq, Eq, Hash, Copy, Clone)]
|
||||||
pub enum Direction {
|
pub enum Direction {
|
||||||
Up,
|
Up,
|
||||||
@ -41,6 +48,7 @@ pub struct Part {
|
|||||||
pub(crate) struct Creature {
|
pub(crate) struct Creature {
|
||||||
pub(crate) parts: Vec<Part>,
|
pub(crate) parts: Vec<Part>,
|
||||||
pub(crate) color: Color,
|
pub(crate) color: Color,
|
||||||
|
pub(crate) species: Species,
|
||||||
pub(crate) curent_direction: (Direction, Direction),
|
pub(crate) curent_direction: (Direction, Direction),
|
||||||
pub(crate) spawner_at: (u16, u16),
|
pub(crate) spawner_at: (u16, u16),
|
||||||
pub(crate) killed: bool,
|
pub(crate) killed: bool,
|
||||||
@ -86,9 +94,10 @@ impl CanvasParts{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub(crate) fn add_creature(&mut self, position: (u16, u16), color: Color, curent_direction: (Direction, Direction), spawner_at: (u16, u16)) {
|
pub(crate) fn add_creature(&mut self, position: (u16, u16), color: Color, curent_direction: (Direction, Direction), species: Species, spawner_at: (u16, u16)) {
|
||||||
self.alive.push(Creature {parts: vec![Part { element: Element::BodyPartHead, position: position, color: color }],
|
self.alive.push(Creature {parts: vec![Part { element: Element::BodyPartHead, position: position, color: color }],
|
||||||
color: color, curent_direction: curent_direction, killed: false, spawner_at: spawner_at});
|
color: color, curent_direction: curent_direction, killed: false,
|
||||||
|
species: species, spawner_at: spawner_at});
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn unify_elements(&self) -> Vec<&Part> {
|
pub(crate) fn unify_elements(&self) -> Vec<&Part> {
|
||||||
@ -101,5 +110,6 @@ impl CanvasParts{
|
|||||||
|
|
||||||
pub struct CanvasState {
|
pub struct CanvasState {
|
||||||
pub(crate) iterations: u128,
|
pub(crate) iterations: u128,
|
||||||
|
pub(crate) cleared_coords: Vec<(u16, u16)>,
|
||||||
//pub(crate) food_rate: u8,
|
//pub(crate) food_rate: u8,
|
||||||
}
|
}
|
||||||
|
|||||||
46
src/main.rs
46
src/main.rs
@ -2,12 +2,13 @@
|
|||||||
pub use std; // for documentation purposes
|
pub use std; // for documentation purposes
|
||||||
use std::io::{self, Write};
|
use std::io::{self, Write};
|
||||||
use components::CanvasParts;
|
use components::CanvasParts;
|
||||||
use crossterm::{cursor::MoveTo, execute, queue, style::{Color, PrintStyledContent, Stylize}, terminal::{enable_raw_mode, BeginSynchronizedUpdate, EnableLineWrap}, QueueableCommand};
|
use crossterm::{cursor::MoveTo, execute, queue, style::{Color, PrintStyledContent, Stylize}, terminal::{enable_raw_mode, EnableLineWrap}, QueueableCommand};
|
||||||
use lazy_static::lazy_static;
|
use lazy_static::lazy_static;
|
||||||
//lazy_static is ok, mutability not needed
|
//lazy_static is ok, mutability not needed
|
||||||
use rand::{thread_rng, Rng};
|
use rand::{thread_rng, Rng};
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use std::thread::sleep;
|
use std::thread::sleep;
|
||||||
|
use std::time::Instant;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
mod part_handler;
|
mod part_handler;
|
||||||
@ -46,40 +47,29 @@ fn create_canvas() -> CanvasParts{
|
|||||||
canvas.add_element(components::Element::Wall, (0, y), Some(wall_color), None); // Use array indexing instead of tuple indexing
|
canvas.add_element(components::Element::Wall, (0, y), Some(wall_color), None); // Use array indexing instead of tuple indexing
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for _ in 0..3{
|
for _ in 0..6{
|
||||||
// Use array indexing instead of tuple indexing
|
// Use array indexing instead of tuple indexing
|
||||||
canvas.add_element(components::Element::Spawn, (rng.gen_range(1..TERM_SIZE.0-1), rng.gen_range(1..TERM_SIZE.1-1)), Some(Color::Rgb { r: 10, g: 255, b: 10 }), None);
|
canvas.add_element(components::Element::Spawn, (rng.gen_range(1..TERM_SIZE.0-1), rng.gen_range(1..TERM_SIZE.1-1)), Some(Color::Rgb { r: 10, g: 255, b: 10 }), None);
|
||||||
}
|
}
|
||||||
|
|
||||||
canvas // Return the canvas vector
|
canvas // Return the canvas vector
|
||||||
}
|
}
|
||||||
fn draw_canvas(canvas: &CanvasParts) {
|
fn draw_canvas(canvas: &CanvasParts, cleared_coords: &mut Vec<(u16, u16)>) {
|
||||||
let mut stdout = io::stdout();
|
let mut stdout = io::stdout();
|
||||||
|
|
||||||
let mut unified_elements = canvas.unify_elements();
|
let mut unified_elements = canvas.unify_elements();
|
||||||
unified_elements.sort_by_key(|part| (part.position.1, part.position.0));
|
unified_elements.sort_by_key(|part| (part.position.1, part.position.0));
|
||||||
|
|
||||||
if true { // Alternative draw method
|
for location in cleared_coords.iter() {
|
||||||
execute!(stdout, MoveTo(0,0)).unwrap();
|
queue!(stdout, MoveTo(location.0, location.1), PrintStyledContent(" ".to_string().with(Color::Reset))).unwrap();
|
||||||
for y in 0..TERM_SIZE.1 {
|
}
|
||||||
for x in 0..TERM_SIZE.0 {
|
|
||||||
if unified_elements.iter().filter(|part| part.position == (x, y)).count() == 0 {
|
|
||||||
stdout.write(b" ").unwrap();
|
|
||||||
} else {
|
|
||||||
let part = unified_elements.iter().find(|part| part.position == (x, y)).unwrap();
|
|
||||||
queue!(stdout, PrintStyledContent(ELEMENT_VISUALS[&part.element].to_string().with(part.color))).unwrap();
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
stdout.queue(crossterm::terminal::Clear(crossterm::terminal::ClearType::All)).unwrap();
|
|
||||||
for part in unified_elements.iter() {
|
for part in unified_elements.iter() {
|
||||||
queue!(stdout, MoveTo(part.position.0, part.position.1),
|
queue!(stdout, MoveTo(part.position.0, part.position.1),
|
||||||
PrintStyledContent(ELEMENT_VISUALS[&part.element].to_string().with(part.color))).unwrap();
|
PrintStyledContent(ELEMENT_VISUALS[&part.element].to_string().with(part.color))).unwrap();
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
}
|
||||||
|
cleared_coords.clear();
|
||||||
stdout.flush().unwrap();
|
stdout.flush().unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -89,21 +79,25 @@ fn main() {
|
|||||||
execute!(io::stdout(), EnableLineWrap).unwrap();
|
execute!(io::stdout(), EnableLineWrap).unwrap();
|
||||||
let mut stdout = io::stdout();
|
let mut stdout = io::stdout();
|
||||||
stdout.queue(crossterm::cursor::Hide).unwrap();
|
stdout.queue(crossterm::cursor::Hide).unwrap();
|
||||||
|
stdout.queue(crossterm::terminal::Clear(crossterm::terminal::ClearType::All)).unwrap();
|
||||||
stdout.flush().unwrap();
|
stdout.flush().unwrap();
|
||||||
|
|
||||||
// Create the canvas
|
// Create the canvas
|
||||||
let mut canvas: CanvasParts = create_canvas();
|
let mut canvas: CanvasParts = create_canvas();
|
||||||
let mut state = components::CanvasState {iterations: 0, }; //food_rate: 0
|
let mut state = components::CanvasState { iterations: 0, cleared_coords: Vec::new() }; //food_rate: 0
|
||||||
draw_canvas(&canvas);
|
draw_canvas(&canvas, &mut state.cleared_coords);
|
||||||
loop {
|
loop {
|
||||||
|
//let mut now = Instant::now();
|
||||||
part_handler::head_handle(&mut canvas);
|
part_handler::head_handle(&mut canvas);
|
||||||
part_handler::spawner_handle(&mut canvas);
|
part_handler::spawner_handle(&mut canvas);
|
||||||
part_handler::handle_killed(&mut canvas.alive);
|
part_handler::handle_killed(&mut canvas.alive, &mut state.cleared_coords);
|
||||||
part_handler::spawn_food(&mut canvas);
|
part_handler::spawn_food(&mut canvas);
|
||||||
|
//let elapsed_calc = now.elapsed();
|
||||||
draw_canvas(&canvas);
|
//now = Instant::now();
|
||||||
|
draw_canvas(&canvas, &mut state.cleared_coords);
|
||||||
|
//let elapsed = now.elapsed();
|
||||||
|
//println!("Print Elapsed: {:.2?}, Calc Elapsed: {:.2?}", elapsed, elapsed_calc);
|
||||||
state.iterations += 1;
|
state.iterations += 1;
|
||||||
sleep(Duration::from_millis(32));
|
//sleep(Duration::from_millis(8));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -2,10 +2,11 @@ use std::thread;
|
|||||||
use std::sync::mpsc;
|
use std::sync::mpsc;
|
||||||
use crossterm::style::Color;
|
use crossterm::style::Color;
|
||||||
use lazy_static::lazy_static;
|
use lazy_static::lazy_static;
|
||||||
|
use rand::seq::index;
|
||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
use crate::HashMap;
|
use crate::HashMap;
|
||||||
|
|
||||||
use crate::components::{self, CanvasParts, Creature, Direction, Element, Part};
|
use crate::components::{self, CanvasParts, Creature, Direction, Element, Part, Species};
|
||||||
const SIGHT_RADIUS: u16 = 4;
|
const SIGHT_RADIUS: u16 = 4;
|
||||||
|
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
@ -82,7 +83,7 @@ pub fn spawner_handle(canvas: &mut CanvasParts) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
if !check_collision(&canvas.environment, (pos.0, pos.1)) {
|
if !check_collision(&canvas.environment, (pos.0, pos.1)) {
|
||||||
canvas.add_creature((pos.0, pos.1), get_unused_color(&canvas.alive), pos.2, canvas.interactable[index].position);
|
canvas.add_creature((pos.0, pos.1), get_unused_color(&canvas.alive), pos.2, Species::NormalSnake, canvas.interactable[index].position);
|
||||||
canvas.interactable[index].color = Color::Rgb { r: 10, g: 100, b: 10 };
|
canvas.interactable[index].color = Color::Rgb { r: 10, g: 100, b: 10 };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -202,29 +203,34 @@ pub fn head_handle(canvas: &mut CanvasParts) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
pub fn handle_killed(creatures: &mut Vec<Creature>) {
|
pub fn handle_killed(creatures: &mut Vec<Creature>, cleared_coords: &mut Vec<(u16, u16)>) {
|
||||||
let killed_creatures: Vec<usize> = creatures.iter().enumerate().filter(|(_, elem)| elem.killed).map(|(index, _)| index).collect::<Vec<_>>();
|
//let killed_creatures: Vec<usize> = creatures.iter().enumerate().filter(|(_, elem)| elem.killed).map(|(index, _)| index).collect::<Vec<_>>();
|
||||||
for index in &killed_creatures {
|
let mut to_remove: Vec<usize> = Vec::new();
|
||||||
//let len_creature = creatures[index].parts.len();
|
for (index, creature) in creatures.iter_mut().enumerate().rev() {
|
||||||
|
//if creature.species == Species::DetachedSnake && creature.parts.len() > 4{
|
||||||
|
// creatures[index].parts.remove(1);
|
||||||
|
//}
|
||||||
|
if creature.killed {
|
||||||
for _ in 0..=3 {
|
for _ in 0..=3 {
|
||||||
if creatures[*index].parts.len() == 0 {
|
if creature.parts.len() == 0 {
|
||||||
|
to_remove.push(index);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
creatures[*index].parts.remove(0);
|
cleared_coords.push(creature.parts[0].position);
|
||||||
|
creature.parts.remove(0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for index in killed_creatures.iter().rev() {
|
|
||||||
if creatures[*index].parts.len() == 0 {
|
|
||||||
creatures.remove(*index);
|
|
||||||
}
|
}
|
||||||
|
// It is bad to remove elements from a vector while iterating over it
|
||||||
|
to_remove.iter().for_each(|x| {creatures.remove(*x);});
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
pub fn spawn_food(canvas: &mut CanvasParts) {
|
pub fn spawn_food(canvas: &mut CanvasParts) {
|
||||||
let mut rng = rand::thread_rng();
|
let mut rng = rand::thread_rng();
|
||||||
|
|
||||||
if rng.gen_bool(0.2){
|
if rng.gen_bool(0.4){
|
||||||
let mut pos: (u16, u16) = (rng.gen_range(1..crate::TERM_SIZE.0 - 1), rng.gen_range(1..crate::TERM_SIZE.1 - 1));
|
let mut pos: (u16, u16) = (rng.gen_range(1..crate::TERM_SIZE.0 - 1), rng.gen_range(1..crate::TERM_SIZE.1 - 1));
|
||||||
while check_collision(&canvas.unify_elements().iter().map(|part| **part).collect(), pos) {
|
while check_collision(&canvas.unify_elements().iter().map(|part| **part).collect(), pos) {
|
||||||
pos = (rng.gen_range(1..crate::TERM_SIZE.0 - 1), rng.gen_range(1..crate::TERM_SIZE.1 - 1));
|
pos = (rng.gen_range(1..crate::TERM_SIZE.0 - 1), rng.gen_range(1..crate::TERM_SIZE.1 - 1));
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user