asteroids-genetic/src/asteroids.rs

63 lines
1.6 KiB
Rust
Raw Normal View History

2022-10-08 13:15:07 +00:00
use macroquad::{prelude::*, rand::gen_range};
#[derive(Default)]
pub struct Asteroid {
2022-10-08 15:27:05 +00:00
pub pos: Vec2,
2022-10-08 13:15:07 +00:00
vel: Vec2,
sides: u8,
radius: f32,
rot: f32,
omega: f32,
}
impl Asteroid {
pub fn new() -> Self {
2022-10-08 15:27:05 +00:00
let radius = gen_range(30., 50.);
2022-10-08 13:15:07 +00:00
let mut r = vec2(
if gen_range(0., 1.) > 0.5 { -1. } else { 1. },
gen_range(-1., 1.),
);
if gen_range(0., 1.) > 0.5 {
r = vec2(r.y, r.x);
}
2022-10-08 15:27:05 +00:00
r *= vec2(
screen_width() * 0.5 + radius,
screen_height() * 0.5 + radius,
);
2022-10-08 13:15:07 +00:00
Self {
pos: r,
2022-10-08 15:27:05 +00:00
vel: 0.04 * -r
+ vec2(
gen_range(20., 60.) * if gen_range(0., 1.) > 0.5 { -1. } else { 1. },
gen_range(20., 60.) * if gen_range(0., 1.) > 0.5 { -1. } else { 1. },
),
2022-10-08 13:15:07 +00:00
sides: gen_range(3, 8),
2022-10-08 15:27:05 +00:00
radius: radius,
2022-10-08 13:15:07 +00:00
omega: gen_range(50., 200.) * if gen_range(0., 1.) > 0.5 { -1. } else { 1. },
..Default::default()
}
}
pub fn update(&mut self) {
self.pos += self.vel * get_frame_time();
self.rot += self.omega * get_frame_time();
}
2022-10-08 15:27:05 +00:00
pub fn is_visible(&self) -> bool {
self.pos.y.abs() < screen_height() * 0.51 + self.radius
&& self.pos.x.abs() < screen_width() * 0.51 + self.radius
}
2022-10-08 13:15:07 +00:00
pub fn draw(&self) {
draw_poly_lines(
self.pos.x,
self.pos.y,
self.sides,
self.radius,
self.rot,
2.,
WHITE,
);
}
}