Refactor creature movement and add new direction enums
This commit is contained in:
parent
eed340b6bb
commit
9c2206c5d4
@ -4,7 +4,6 @@ use crossterm::style::Color;
|
|||||||
|
|
||||||
|
|
||||||
use crate::TERM_SIZE;
|
use crate::TERM_SIZE;
|
||||||
use crate::part_handler;
|
|
||||||
|
|
||||||
#[derive(PartialEq, Clone, Hash, Eq, Copy)]
|
#[derive(PartialEq, Clone, Hash, Eq, Copy)]
|
||||||
pub enum Species {
|
pub enum Species {
|
||||||
@ -13,14 +12,65 @@ pub enum Species {
|
|||||||
Wesp
|
Wesp
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(PartialEq, Eq, Hash, Copy, Clone)]
|
#[derive(PartialEq, Clone, Copy)]
|
||||||
pub enum Direction {
|
pub enum DirectionX {
|
||||||
Up,
|
|
||||||
Down,
|
|
||||||
Left,
|
Left,
|
||||||
Right,
|
Right,
|
||||||
None,
|
None,
|
||||||
}
|
}
|
||||||
|
#[derive(PartialEq, Clone, Copy)]
|
||||||
|
pub enum DirectionY {
|
||||||
|
Up,
|
||||||
|
Down,
|
||||||
|
None,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn directions_to_check(current_dir: &(DirectionX, DirectionY), fov: isize) -> Vec<(DirectionX, DirectionY)> {
|
||||||
|
let circle = vec![
|
||||||
|
(DirectionX::Left, DirectionY::None),
|
||||||
|
(DirectionX::Left, DirectionY::Up),
|
||||||
|
(DirectionX::None, DirectionY::Up),
|
||||||
|
(DirectionX::Right, DirectionY::Up),
|
||||||
|
(DirectionX::Right, DirectionY::None),
|
||||||
|
(DirectionX::Right, DirectionY::Down),
|
||||||
|
(DirectionX::None, DirectionY::Down),
|
||||||
|
(DirectionX::Left, DirectionY::Down),
|
||||||
|
];
|
||||||
|
|
||||||
|
let current_index = circle.iter().position(|&x| x == *current_dir).unwrap() as isize;
|
||||||
|
let circle_len = circle.len() as isize;
|
||||||
|
|
||||||
|
let mut directions_to_check = Vec::new();
|
||||||
|
for i in -fov..=fov {
|
||||||
|
// Move to the next index
|
||||||
|
// index = (index +1) % array.len(); loop through the array avoiding out of bounds
|
||||||
|
//% operator is the remainder operator, not the modulo operator
|
||||||
|
let index_circle = (((current_index + i)%circle_len) + circle_len)%circle_len;
|
||||||
|
directions_to_check.push(circle[index_circle as usize]);
|
||||||
|
}
|
||||||
|
directions_to_check
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn pos_alteration_by_direction(dir_x: Option<&DirectionX>, dir_y: Option<&DirectionY>, position: &(u16, u16)) -> (i32, i32) {
|
||||||
|
let mut new_position = (position.0 as i32, position.1 as i32);
|
||||||
|
if let Some(dir_x) = dir_x {
|
||||||
|
match dir_x {
|
||||||
|
DirectionX::Left => new_position.0 -= 1,
|
||||||
|
DirectionX::Right => new_position.0 += 1,
|
||||||
|
DirectionX::None => (),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if let Some(dir_y) = dir_y {
|
||||||
|
match dir_y {
|
||||||
|
DirectionY::Up => new_position.1 -= 1,
|
||||||
|
DirectionY::Down => new_position.1 += 1,
|
||||||
|
DirectionY::None => (),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
new_position
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#[derive(PartialEq, Eq, Hash, Copy, Clone)]
|
#[derive(PartialEq, Eq, Hash, Copy, Clone)]
|
||||||
|
|
||||||
@ -49,20 +99,31 @@ 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) species: Species,
|
||||||
pub(crate) curent_direction: (Direction, Direction),
|
pub(crate) curent_direction: (DirectionX, DirectionY),
|
||||||
pub(crate) spawner_at: (u16, u16),
|
pub(crate) spawner_at: (u16, u16),
|
||||||
pub(crate) killed: bool,
|
pub(crate) killed: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Creature {
|
impl Creature {
|
||||||
pub(crate) fn move_to(&mut self, new_position: (u16, u16), moved_in_direction: (Direction, Direction), colision: bool) {
|
pub(crate) fn move_to(&mut self, new_position: (u16, u16), moved_in_direction: (DirectionX, DirectionY), colision: bool) {
|
||||||
let old_position = self.parts[0].position; // 0 Allways the head
|
let old_position = self.parts[0].position; // 0 Allways the head
|
||||||
self.parts[0].position = new_position;
|
self.parts[0].position = new_position;
|
||||||
self.curent_direction = moved_in_direction;
|
self.curent_direction = moved_in_direction;
|
||||||
if new_position.0 > TERM_SIZE.0 || new_position.0 <= 0 || new_position.1 > TERM_SIZE.1 || new_position.1 <= 0 || colision {
|
if new_position.0 > TERM_SIZE.0 || new_position.0 <= 0 || new_position.1 > TERM_SIZE.1 || new_position.1 <= 0 || colision {
|
||||||
self.killed = true;
|
self.killed = true;
|
||||||
}
|
}
|
||||||
self.parts.append(&mut vec![Part { element: part_handler::CHECK_DIRECTIONS[&self.curent_direction].1,
|
let part_to_append = match moved_in_direction {
|
||||||
|
(DirectionX::None, DirectionY::Up) => Element::BodyPartVert,
|
||||||
|
(DirectionX::Right,DirectionY::Up) => Element::BodyPartLeftLean,
|
||||||
|
(DirectionX::Left, DirectionY::Up) => Element::BodyPartRightLean,
|
||||||
|
(DirectionX::None, DirectionY::Down) =>Element::BodyPartVert,
|
||||||
|
(DirectionX::Right,DirectionY::Down) =>Element::BodyPartRightLean,
|
||||||
|
(DirectionX::Left, DirectionY::Down) =>Element::BodyPartLeftLean,
|
||||||
|
(DirectionX::Right,DirectionY::None) =>Element::BodyPartHori,
|
||||||
|
(DirectionX::Left, DirectionY::None) =>Element::BodyPartHori,
|
||||||
|
_ => Element::BodyPartHori,
|
||||||
|
};
|
||||||
|
self.parts.append(&mut vec![Part { element: part_to_append,
|
||||||
position: old_position, color: self.color }]);
|
position: old_position, color: self.color }]);
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -94,7 +155,7 @@ impl CanvasParts{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub(crate) fn add_creature(&mut self, position: (u16, u16), color: Color, curent_direction: (Direction, Direction), species: Species, spawner_at: (u16, u16)) {
|
pub(crate) fn add_creature(&mut self, position: (u16, u16), color: Color, curent_direction: (DirectionX, DirectionY), 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,
|
color: color, curent_direction: curent_direction, killed: false,
|
||||||
species: species, spawner_at: spawner_at});
|
species: species, spawner_at: spawner_at});
|
||||||
|
|||||||
@ -5,31 +5,9 @@ use lazy_static::lazy_static;
|
|||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
use crate::HashMap;
|
use crate::HashMap;
|
||||||
|
|
||||||
use crate::components::{self, CanvasParts, Creature, Direction, Element, Part, Species};
|
use crate::components::{self, directions_to_check, pos_alteration_by_direction, CanvasParts, Creature, DirectionX, DirectionY, Element, Part, Species};
|
||||||
const SIGHT_RADIUS: u16 = 4;
|
const SIGHT_RADIUS: u16 = 4;
|
||||||
|
|
||||||
lazy_static! {
|
|
||||||
pub static ref CHECK_DIRECTIONS: HashMap<(Direction, Direction), (Vec<Vec<Direction>>, Element)> = HashMap::from([
|
|
||||||
((Direction::Up, Direction::None), (vec![vec![Direction::Up, Direction::None], vec![Direction::Up, Direction::Right], vec![Direction::Up, Direction::Left]], Element::BodyPartHori)),
|
|
||||||
((Direction::Up, Direction::Right), (vec![vec![Direction::Up, Direction::Right], vec![Direction::Up, Direction::None], vec![Direction::None, Direction::Right]], Element::BodyPartLeftLean)),
|
|
||||||
((Direction::Up, Direction::Left), (vec![vec![Direction::Up, Direction::Left], vec![Direction::Up, Direction::None], vec![Direction::None, Direction::Left]], Element::BodyPartRightLean)),
|
|
||||||
((Direction::Down, Direction::None), (vec![vec![Direction::Down, Direction::None], vec![Direction::Down, Direction::Right], vec![Direction::Down, Direction::Left]], Element::BodyPartHori)),
|
|
||||||
((Direction::Down, Direction::Right), (vec![vec![Direction::Down, Direction::Right], vec![Direction::Down, Direction::None], vec![Direction::None, Direction::Right]], Element::BodyPartRightLean)),
|
|
||||||
((Direction::Down, Direction::Left), (vec![vec![Direction::Down, Direction::Left], vec![Direction::Down, Direction::None], vec![Direction::None, Direction::Left]], Element::BodyPartLeftLean)),
|
|
||||||
((Direction::None, Direction::Right), (vec![vec![Direction::None, Direction::Right], vec![Direction::Up, Direction::Right], vec![Direction::Down, Direction::Right]], Element::BodyPartVert)),
|
|
||||||
((Direction::None, Direction::Left), (vec![vec![Direction::None, Direction::Left], vec![Direction::Up, Direction::Left], vec![Direction::Down, Direction::Left]], Element::BodyPartVert)),
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
|
|
||||||
lazy_static!{
|
|
||||||
static ref DIRECTIONS: HashMap<components::Direction, i32> = HashMap::from([
|
|
||||||
(Direction::Up, -1),
|
|
||||||
(Direction::Down, 1),
|
|
||||||
(Direction::Right, 1),
|
|
||||||
(Direction::Left, -1),
|
|
||||||
(Direction::None, 0)
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
|
|
||||||
lazy_static!{
|
lazy_static!{
|
||||||
static ref ITERATION_TIMES: HashMap<components::Species, u8> = HashMap::from([
|
static ref ITERATION_TIMES: HashMap<components::Species, u8> = HashMap::from([
|
||||||
@ -71,13 +49,13 @@ pub fn spawner_handle(canvas: &mut CanvasParts) {
|
|||||||
let x: u16 = rand_gen.gen_range(0..=1000);
|
let x: u16 = rand_gen.gen_range(0..=1000);
|
||||||
if x < 10*active_spawn_count as u16 {
|
if x < 10*active_spawn_count as u16 {
|
||||||
let index = unused_spawns[rand_gen.gen_range(0..active_spawn_count)];
|
let index = unused_spawns[rand_gen.gen_range(0..active_spawn_count)];
|
||||||
let pos: (u16, u16, (Direction, Direction)) = match x % 4 {
|
let pos: (u16, u16, (DirectionX, DirectionY)) = match x % 4 {
|
||||||
// 0: up 1: right 2: down 3: left
|
// 0: up 1: right 2: down 3: left
|
||||||
0 => (canvas.interactable[index].position.0, canvas.interactable[index].position.1 - 1, (Direction::Up, Direction::None)),
|
0 => (canvas.interactable[index].position.0, canvas.interactable[index].position.1 - 1, (DirectionX::None, DirectionY::Up)),
|
||||||
1 => (canvas.interactable[index].position.0 + 1, canvas.interactable[index].position.1, (Direction::None, Direction::Right)),
|
1 => (canvas.interactable[index].position.0 + 1, canvas.interactable[index].position.1, (DirectionX::Right,DirectionY::None)),
|
||||||
2 => (canvas.interactable[index].position.0, canvas.interactable[index].position.1 + 1, (Direction::Down, Direction::None)),
|
2 => (canvas.interactable[index].position.0, canvas.interactable[index].position.1 + 1, (DirectionX::None, DirectionY::Down)),
|
||||||
3 => (canvas.interactable[index].position.0 - 1, canvas.interactable[index].position.1, (Direction::None, Direction::Left)),
|
3 => (canvas.interactable[index].position.0 - 1, canvas.interactable[index].position.1, (DirectionX::Left, DirectionY::None)),
|
||||||
_ => (1, 1, (Direction::Down, Direction::None)),
|
_ => (1, 1, (DirectionX::None, DirectionY::Down)),
|
||||||
};
|
};
|
||||||
|
|
||||||
if !check_collision(&canvas.environment, (pos.0, pos.1)) {
|
if !check_collision(&canvas.environment, (pos.0, pos.1)) {
|
||||||
@ -120,23 +98,24 @@ fn wesp_path_match(elem:Element) -> i64 {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn recursive_colision_check(path_to_match: fn(Element) -> i64, parts_in_sight: &Vec<Part>, position: &(u16, u16),
|
fn recursive_colision_check(path_to_match: fn(Element) -> i64, parts_in_sight: &Vec<Part>, fov: isize, position: &(u16, u16),
|
||||||
direction: &(Direction, Direction), iterations_left: u8, ) -> (i32, i32, (Direction, Direction), i64){
|
direction: &(DirectionX, DirectionY), iterations_left: u8, ) -> (i32, i32, (DirectionX, DirectionY), i64){
|
||||||
// Position to check x, Position to check y, Direction to walk, Vale of Direction
|
// Position to check x, Position to check y, Direction to walk, Vale of Direction
|
||||||
let mut dyn_pos_res: Vec<(i32, i32, (Direction, Direction), i64)> = Vec::new();
|
let mut dyn_pos_res: Vec<(i32, i32, (DirectionX, DirectionY), i64)> = Vec::new();
|
||||||
|
|
||||||
|
|
||||||
// Create a vector of tuples with the possible positions and their values(liklihood of beeing chosen)
|
// Create a vector of tuples with the possible positions and their values(liklihood of beeing chosen)
|
||||||
for direction_to_check in CHECK_DIRECTIONS[&direction].0.iter() {
|
for direction_to_check in directions_to_check(&direction, fov).iter() {
|
||||||
let (dir_x, dir_y): (Direction, Direction) = (direction_to_check[0], direction_to_check[1]);
|
let (dir_x, dir_y): (DirectionX, DirectionY) = ((*direction_to_check).0, (*direction_to_check).1);
|
||||||
dyn_pos_res.push((i32::from(position.0) + DIRECTIONS[&dir_x], i32::from(position.1) + DIRECTIONS[&dir_y], (dir_x, dir_y), 0));
|
let (x, y): (i32, i32) = pos_alteration_by_direction(Some(&dir_x), Some(&dir_y), position);
|
||||||
|
dyn_pos_res.push((x, y, (dir_x, dir_y), 0));
|
||||||
}
|
}
|
||||||
|
|
||||||
let (sender, receiver) = mpsc::channel();
|
let (sender, receiver) = mpsc::channel();
|
||||||
let mut threads: Vec<thread::JoinHandle<()>> = Vec::new();
|
let mut threads: Vec<thread::JoinHandle<()>> = Vec::new();
|
||||||
// Iterate over the vector and check if the position is in sight, what value the sight has
|
// Iterate over the vector and check if the position is in sight, what value the sight has
|
||||||
for index in 0..dyn_pos_res.len() {
|
for index in 0..dyn_pos_res.len() {
|
||||||
let (x, y, v_direction, mut val): (i32, i32, (Direction, Direction), i64) = dyn_pos_res[index].clone();
|
let (x, y, v_direction, mut val): (i32, i32, (DirectionX, DirectionY), i64) = dyn_pos_res[index].clone();
|
||||||
for part in parts_in_sight {
|
for part in parts_in_sight {
|
||||||
if part.position == (x as u16, y as u16) {
|
if part.position == (x as u16, y as u16) {
|
||||||
//When element in sight, part is element in sight
|
//When element in sight, part is element in sight
|
||||||
@ -149,7 +128,7 @@ fn recursive_colision_check(path_to_match: fn(Element) -> i64, parts_in_sight: &
|
|||||||
let sender = sender.clone();
|
let sender = sender.clone();
|
||||||
let parts_in_sight = parts_in_sight.clone();
|
let parts_in_sight = parts_in_sight.clone();
|
||||||
threads.push(thread::spawn(move || {
|
threads.push(thread::spawn(move || {
|
||||||
let t_val = recursive_colision_check(path_to_match, &parts_in_sight, &(x as u16, y as u16), &v_direction, iterations_left - 1).3;
|
let t_val = recursive_colision_check(path_to_match, &parts_in_sight, fov, &(x as u16, y as u16), &v_direction, iterations_left - 1).3;
|
||||||
sender.send((t_val, index)).unwrap();
|
sender.send((t_val, index)).unwrap();
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
@ -177,6 +156,10 @@ pub fn head_handle(canvas: &mut CanvasParts) {
|
|||||||
|
|
||||||
let cloned_canvas = canvas.clone();
|
let cloned_canvas = canvas.clone();
|
||||||
for creature in canvas.alive.iter_mut() {
|
for creature in canvas.alive.iter_mut() {
|
||||||
|
let fov = match creature.species {
|
||||||
|
Species::DetachedSnake => 2,
|
||||||
|
_ => 1,
|
||||||
|
};
|
||||||
for _ in 0..ITERATION_TIMES[&creature.species] {
|
for _ in 0..ITERATION_TIMES[&creature.species] {
|
||||||
let head = creature.parts.iter().filter(|elem| elem.element == Element::BodyPartHead).next();
|
let head = creature.parts.iter().filter(|elem| elem.element == Element::BodyPartHead).next();
|
||||||
if head.is_none() {
|
if head.is_none() {
|
||||||
@ -204,12 +187,12 @@ pub fn head_handle(canvas: &mut CanvasParts) {
|
|||||||
let sender = sender.clone();
|
let sender = sender.clone();
|
||||||
|
|
||||||
let thread = thread::spawn(move || {
|
let thread = thread::spawn(move || {
|
||||||
let result: (i32, i32, (Direction, Direction), i64) = recursive_colision_check(matcher, &parts_in_sight_thread,
|
let result = recursive_colision_check(matcher, &parts_in_sight_thread, fov,
|
||||||
&head.position, &dir_thread , SIGHT_RADIUS as u8);
|
&head.position, &dir_thread , SIGHT_RADIUS as u8);
|
||||||
sender.send(result).unwrap();
|
sender.send(result).unwrap();
|
||||||
});
|
});
|
||||||
|
|
||||||
let res: (i32, i32, (Direction, Direction), i64) = receiver.recv().unwrap();
|
let res: (i32, i32, (DirectionX, DirectionY), i64) = receiver.recv().unwrap();
|
||||||
thread.join().unwrap();
|
thread.join().unwrap();
|
||||||
|
|
||||||
// Check colisions of new position
|
// Check colisions of new position
|
||||||
@ -255,7 +238,7 @@ pub fn handle_killed(creatures: &mut Vec<Creature>, cleared_coords: &mut Vec<(u1
|
|||||||
if creature.species == Species::DetachedSnake && creature.parts.len() > 20 {
|
if creature.species == Species::DetachedSnake && creature.parts.len() > 20 {
|
||||||
cleared_coords.push(creature.parts[1].position); //0 Is head, 1 is oldes part
|
cleared_coords.push(creature.parts[1].position); //0 Is head, 1 is oldes part
|
||||||
creature.parts.remove(1);
|
creature.parts.remove(1);
|
||||||
} else if creature.species == Species::Wesp && creature.parts.len() > 5 {
|
} else if creature.species == Species::Wesp && creature.parts.len() > 3 {
|
||||||
cleared_coords.push(creature.parts[1].position); //
|
cleared_coords.push(creature.parts[1].position); //
|
||||||
cleared_coords.push(creature.parts[2].position);
|
cleared_coords.push(creature.parts[2].position);
|
||||||
creature.parts.remove(1); //0 Is head, 1 is oldes part, tow parts are removed
|
creature.parts.remove(1); //0 Is head, 1 is oldes part, tow parts are removed
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user