asteroids-genetic/src/asteroids.rs

100 lines
2.8 KiB
Rust
Raw Normal View History

2023-01-06 22:47:57 +00:00
use crate::{HEIGHT, WIDTH};
2022-10-08 13:15:07 +00:00
use macroquad::{prelude::*, rand::gen_range};
#[derive(Clone, PartialEq, Eq)]
2022-10-08 19:47:47 +00:00
pub enum AsteroidSize {
Large,
Medium,
Small,
}
2023-01-05 16:46:08 +00:00
#[derive(Clone)]
2022-10-08 13:15:07 +00:00
pub struct Asteroid {
2022-10-08 15:27:05 +00:00
pub pos: Vec2,
2022-10-08 20:17:06 +00:00
pub vel: Vec2,
pub size: AsteroidSize,
2022-10-08 13:15:07 +00:00
sides: u8,
2022-10-10 18:36:14 +00:00
pub radius: f32,
2022-10-08 13:15:07 +00:00
rot: f32,
omega: f32,
2022-10-08 19:47:47 +00:00
pub alive: bool,
2022-10-08 13:15:07 +00:00
}
const BASE_SIZE: f32 = 20.;
2022-10-08 13:15:07 +00:00
impl Asteroid {
2022-10-08 19:47:47 +00:00
pub fn new(size: AsteroidSize) -> Self {
let (sides, radius) = match size {
AsteroidSize::Large => (gen_range(6, 10), BASE_SIZE * 4.),
AsteroidSize::Medium => (gen_range(5, 6), BASE_SIZE * 2.),
AsteroidSize::Small => (gen_range(3, 5), BASE_SIZE),
2022-10-08 19:47:47 +00:00
};
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);
}
2023-01-06 22:47:57 +00:00
r *= vec2(WIDTH * 0.5 + radius, HEIGHT * 0.5 + radius);
2022-10-08 13:15:07 +00:00
Self {
pos: r,
2022-10-10 10:13:06 +00:00
vel: 0.001 * -r
2022-10-08 15:27:05 +00:00
+ vec2(
2022-10-10 10:13:06 +00:00
gen_range(0.3, 1.) * if gen_range(0., 1.) > 0.5 { -1. } else { 1. },
gen_range(0.3, 1.) * if gen_range(0., 1.) > 0.5 { -1. } else { 1. },
2022-10-08 15:27:05 +00:00
),
2022-10-08 19:47:47 +00:00
size: size,
sides: sides,
2022-10-08 15:27:05 +00:00
radius: radius,
2022-10-10 10:13:06 +00:00
omega: gen_range(0.8, 3.5) * if gen_range(0., 1.) > 0.5 { -1. } else { 1. },
2022-10-08 19:47:47 +00:00
rot: 0.,
alive: true,
2022-10-08 13:15:07 +00:00
}
}
2022-10-08 20:17:06 +00:00
pub fn new_from(pos: Vec2, vel: Vec2, size: AsteroidSize) -> Self {
let mut asteroid = Asteroid::new(size);
asteroid.pos = pos;
asteroid.vel = vel;
asteroid
}
pub fn new_to(pos: Vec2, speed: f32, size: AsteroidSize) -> Self {
let mut asteroid = Asteroid::new(size);
asteroid.vel = (pos - asteroid.pos) * 0.002 * speed;
asteroid
}
2023-01-05 16:46:08 +00:00
pub fn check_collision(&self, pos: Vec2, rad: f32) -> bool {
2022-10-09 05:40:56 +00:00
(pos.x - self.pos.x) * (pos.x - self.pos.x) + (pos.y - self.pos.y) * (pos.y - self.pos.y)
<= (self.radius + rad) * (self.radius + rad)
2022-10-08 13:15:07 +00:00
}
2022-10-08 19:47:47 +00:00
pub fn update(&mut self) {
2022-10-09 20:12:23 +00:00
self.pos += self.vel;
self.rot += self.omega;
2023-01-06 22:47:57 +00:00
if self.pos.x.abs() > WIDTH * 0.5 + self.radius {
2022-10-09 05:40:56 +00:00
self.pos.x *= -1.;
}
2023-01-06 22:47:57 +00:00
if self.pos.y.abs() > HEIGHT * 0.5 + self.radius {
2022-10-09 05:40:56 +00:00
self.pos.y *= -1.;
2022-10-08 19:47:47 +00:00
}
2022-10-08 15:27:05 +00:00
}
2023-01-11 18:56:03 +00:00
pub fn draw(&self, color: Color) {
2022-10-08 13:15:07 +00:00
draw_poly_lines(
self.pos.x,
self.pos.y,
self.sides,
self.radius,
self.rot,
2022-10-09 05:40:56 +00:00
match self.size {
AsteroidSize::Large => 2.,
AsteroidSize::Medium => 1.2,
2023-01-11 18:56:03 +00:00
AsteroidSize::Small => 1.,
2022-10-09 05:40:56 +00:00
},
2023-01-11 18:56:03 +00:00
color,
2022-10-08 13:15:07 +00:00
);
}
}