final tweaking

This commit is contained in:
sparshg 2023-01-06 01:40:35 +05:30
parent 1c036ec097
commit edf1db25e0
3 changed files with 74 additions and 48 deletions

View File

@ -27,7 +27,7 @@ async fn main() {
speedup = !speedup; speedup = !speedup;
} }
if speedup { if speedup {
for _ in 0..100 { for _ in 0..1000 {
pop.update(); pop.update();
} }
} else { } else {

View File

@ -1,4 +1,4 @@
use std::{f32::consts::PI, f64::consts::TAU}; use std::{f32::consts::PI, f64::consts::TAU, iter};
use macroquad::{prelude::*, rand::gen_range}; use macroquad::{prelude::*, rand::gen_range};
use nalgebra::{max, partial_max, partial_min}; use nalgebra::{max, partial_max, partial_min};
@ -14,6 +14,7 @@ pub struct Player {
drag: f32, drag: f32,
bullets: Vec<Bullet>, bullets: Vec<Bullet>,
asteroid: Option<Asteroid>, asteroid: Option<Asteroid>,
asteroid_data: Vec<(f32, f32, f32)>,
last_shot: u8, last_shot: u8,
shot_interval: u8, shot_interval: u8,
pub brain: Option<NN>, pub brain: Option<NN>,
@ -51,30 +52,41 @@ impl Player {
// ); // );
p.brain = Some(brain); p.brain = Some(brain);
} else { } else {
p.brain = Some(NN::new(vec![3, 8, 8, 4])); p.brain = Some(NN::new(vec![4, 8, 8, 4]));
} }
p p
} }
pub fn check_player_collision(&mut self, asteroid: &Asteroid) -> bool { pub fn check_player_collision(&mut self, asteroid: &Asteroid) -> bool {
// self.raycasts.extend([ // let directions = [
if self.asteroid.is_none() // vec2(0., -screen_height()),
|| (asteroid.pos).distance_squared(self.pos) // vec2(0., screen_height()),
< self // vec2(-screen_width(), 0.),
.asteroid // vec2(screen_width(), 0.),
.as_ref() // ];
.unwrap() // let mut nearest = asteroid.pos - self.pos;
.pos // for dir in directions {
.distance_squared(self.pos) // if (asteroid.pos - self.pos + dir).length_squared() < nearest.length_squared() {
{ // nearest = asteroid.pos - self.pos + dir;
self.asteroid = Some(asteroid.clone()); // }
}
// ]);
// if self.raycasts[0] > (asteroid.pos - self.pos).length_squared() {
// self.raycasts[0] = (asteroid.pos - self.pos).length_squared();
// self.raycasts[1] = Vec2::angle_between(asteroid.pos - self.pos, self.dir).sin();
// self.raycasts[2] = Vec2::angle_between(asteroid.pos - self.pos, self.dir).cos();
// } // }
self.asteroid_data.push((
((asteroid.pos - self.pos).length() - asteroid.radius) / screen_width(),
self.dir.angle_between(asteroid.pos - self.pos),
(asteroid.vel - self.vel).length(),
));
// if self.asteroid.is_none()
// || (asteroid.pos).distance_squared(self.pos)
// < self
// .asteroid
// .as_ref()
// .unwrap()
// .pos
// .distance_squared(self.pos)
// {
// self.asteroid = Some(asteroid.clone());
// }
// ]);
// let v = asteroid.pos - self.pos; // let v = asteroid.pos - self.pos;
// for i in 0..4 { // for i in 0..4 {
// let dir = Vec2::from_angle(PI / 4. * i as f32).rotate(self.dir); // let dir = Vec2::from_angle(PI / 4. * i as f32).rotate(self.dir);
@ -89,7 +101,7 @@ impl Player {
// .unwrap(); // .unwrap();
// } // }
// } // }
if asteroid.check_collision(self.pos, 8.) || self.lifespan > 2000 { if asteroid.check_collision(self.pos, 8.) || self.lifespan > 4000 {
self.alive = false; self.alive = false;
return true; return true;
} }
@ -114,21 +126,28 @@ impl Player {
self.acc = 0.; self.acc = 0.;
let mut keys = vec![false, false, false, false]; let mut keys = vec![false, false, false, false];
let mut inputs = vec![ let mut inputs = vec![
(self.asteroid.as_ref().unwrap().pos - self.pos).length() * 0.707 / screen_width(), // (self.asteroid.as_ref().unwrap().pos - self.pos).length() / screen_width(),
// self.raycasts[0] - self.pos.x / screen_width(), // self.dir
// self.raycasts[1] - self.pos.y / screen_height(), // .angle_between(self.asteroid.as_ref().unwrap().pos - self.pos),
self.dir
.angle_between(self.asteroid.as_ref().unwrap().pos - self.pos),
// self.vel.x / 11., // self.vel.x / 11.,
// self.vel.y / 11., // self.vel.y / 11.,
self.rot, // self.rot.sin(), self.rot / TAU as f32,
// self.rot.cos(), // self.rot.sin(),
// self.rot.cos(),
]; ];
// self.raycasts.resize(3, 0.); self.asteroid_data
// inputs.append(self.raycasts.clone().as_mut()); .sort_by(|a, b| a.0.partial_cmp(&b.0).unwrap());
// println!("inputs: {:?}", inputs); self.asteroid_data.resize(1, (0., 0., 0.));
inputs.append(
&mut self
.asteroid_data
.iter()
.map(|(d, a, h)| vec![*d, *a, *h])
.flatten()
.collect::<Vec<_>>(),
);
// println!("{:?}", inputs);
// let inputs = self.raycasts.clone(); // let inputs = self.raycasts.clone();
// inputs.append(self.asteroids_data.as_mut()); // inputs.append(self.asteroids_data.as_mut());
if let Some(brain) = &self.brain { if let Some(brain) = &self.brain {
@ -179,7 +198,9 @@ impl Player {
self.bullets.retain(|b| { self.bullets.retain(|b| {
b.alive && b.pos.x.abs() * 2. < screen_width() && b.pos.y.abs() * 2. < screen_height() b.alive && b.pos.x.abs() * 2. < screen_width() && b.pos.y.abs() * 2. < screen_height()
}); });
// self.draw();
self.asteroid = None; self.asteroid = None;
self.asteroid_data.clear();
} }
pub fn draw(&self) { pub fn draw(&self) {
@ -201,21 +222,25 @@ impl Player {
if self.acc > 0. && gen_range(0., 1.) < 0.4 { if self.acc > 0. && gen_range(0., 1.) < 0.4 {
draw_triangle_lines(p6, p7, p8, 2., color); draw_triangle_lines(p6, p7, p8, 2., color);
} }
if self.debug && self.asteroid.is_some() { if self.debug {
draw_circle_lines( // if self.asteroid.is_some() {
self.asteroid.as_ref().unwrap().pos.x, // draw_circle_lines(
self.asteroid.as_ref().unwrap().pos.y, // self.asteroid.as_ref().unwrap().pos.x,
self.asteroid.as_ref().unwrap().radius, // self.asteroid.as_ref().unwrap().pos.y,
1., // self.asteroid.as_ref().unwrap().radius,
GRAY, // 1.,
); // GRAY,
// );
// }
let p = self.pos
+ self.dir.rotate(Vec2::from_angle(self.asteroid_data[0].1))
* self.asteroid_data[0].0
* screen_width();
draw_line( draw_line(
self.pos.x, self.pos.x, self.pos.y,
self.pos.y, // self.asteroid.as_ref().unwrap().pos.x,
self.asteroid.as_ref().unwrap().pos.x, // self.asteroid.as_ref().unwrap().pos.y,
self.asteroid.as_ref().unwrap().pos.y, p.x, p.y, 1., GRAY,
1.,
GRAY,
); );
// for (i, r) in self.raycasts.iter().enumerate() { // for (i, r) in self.raycasts.iter().enumerate() {

View File

@ -20,7 +20,7 @@ impl World {
Self { Self {
player: Player::new(), player: Player::new(),
max_asteroids: 28, max_asteroids: 28,
score: 0., score: 1.,
..Default::default() ..Default::default()
} }
} }
@ -29,7 +29,7 @@ impl World {
Self { Self {
player: Player::simulate(brain), player: Player::simulate(brain),
max_asteroids: 28, max_asteroids: 28,
score: 0., score: 1.,
asteroids: vec![ asteroids: vec![
Asteroid::new_to(vec2(0., 0.), 1.5, AsteroidSize::Large), Asteroid::new_to(vec2(0., 0.), 1.5, AsteroidSize::Large),
Asteroid::new(AsteroidSize::Large), Asteroid::new(AsteroidSize::Large),
@ -128,6 +128,7 @@ impl World {
// AsteroidSize::Small => 1, // AsteroidSize::Small => 1,
// } // }
// }) < self.max_asteroids // }) < self.max_asteroids
// || self.player.lifespan % 200 == 0
// { // {
if self.player.lifespan % 200 == 0 { if self.player.lifespan % 200 == 0 {
self.asteroids self.asteroids