use crate::array;
use crate::simulation::{Particle, Point, Vector};
use rand::Rng;
use ratatui::style::Color;
use std::f64::consts::PI;
const FRAMES_PER_SECOND: f64 = 30.0;
const NUM_PARTICLES: usize = 50;
const COLORS: &[&str] = &["#a864fd", "#29cdff", "#78ff44", "#ff718d", "#fdff6a"];
const CHARACTERS: &[&str] = &["+", "*", "•"];
const HEAD: &str = "▄";
const TAIL: &str = "│";
fn parse_color(hex: &str) -> Color {
if hex.starts_with('#') && hex.len() == 7 {
let r = u8::from_str_radix(&hex[1..3], 16).unwrap_or(255);
let g = u8::from_str_radix(&hex[3..5], 16).unwrap_or(255);
let b = u8::from_str_radix(&hex[5..7], 16).unwrap_or(255);
Color::Rgb(r, g, b)
} else {
Color::White
}
}
pub fn spawn_shoot(width: i32, height: i32) -> Particle {
let mut rng = rand::thread_rng();
let color_str = array::sample(COLORS);
let color = parse_color(color_str);
let v = (rng.gen_range(15..30)) as f64;
let x = rng.gen::<f64>() * width as f64;
let position = Point {
x,
y: height as f64,
z: 0.0,
};
let velocity = Vector {
x: 0.0,
y: -v,
z: 0.0,
};
let mut particle = Particle::new(
HEAD.to_string(),
color,
position,
velocity,
FRAMES_PER_SECOND,
);
particle.tail_char = TAIL.to_string();
particle.shooting = true;
particle.explosion_call = Some(spawn_explosion);
particle
}
pub fn spawn_explosion(color: Color, x: f64, y: f64, _width: i32, _height: i32) -> Vec<Particle> {
let mut rng = rand::thread_rng();
let v = (rng.gen_range(20..30)) as f64;
let mut particles = Vec::new();
for i in 0..NUM_PARTICLES {
let angle = (i as f64 / NUM_PARTICLES as f64) * 2.0 * PI;
let position = Point { x, y, z: 0.0 };
let velocity = Vector {
x: angle.cos() * v,
y: angle.sin() * v / 2.0,
z: 0.0,
};
let character = array::sample(CHARACTERS);
let particle = Particle::new(
character.to_string(),
color,
position,
velocity,
FRAMES_PER_SECOND,
);
particles.push(particle);
}
particles
}