bullet collisions

This commit is contained in:
sparshg 2022-10-09 01:17:47 +05:30
parent 680f5c57d8
commit 7e2b07ca48
3 changed files with 65 additions and 23 deletions

View File

@ -1,18 +1,28 @@
use macroquad::{prelude::*, rand::gen_range};
#[derive(Default)]
pub enum AsteroidSize {
Large,
Medium,
Small,
}
pub struct Asteroid {
pub pos: Vec2,
vel: Vec2,
size: AsteroidSize,
sides: u8,
radius: f32,
rot: f32,
omega: f32,
pub alive: bool,
}
impl Asteroid {
pub fn new() -> Self {
let radius = gen_range(30., 50.);
pub fn new(size: AsteroidSize) -> Self {
let (sides, radius) = match size {
AsteroidSize::Large => (gen_range(6, 10), gen_range(40., 50.)),
AsteroidSize::Medium => (gen_range(5, 6), gen_range(20., 30.)),
AsteroidSize::Small => (gen_range(3, 5), gen_range(12., 18.)),
};
let mut r = vec2(
if gen_range(0., 1.) > 0.5 { -1. } else { 1. },
gen_range(-1., 1.),
@ -26,28 +36,42 @@ impl Asteroid {
);
Self {
pos: r,
vel: 0.04 * -r
vel: 0.1 * -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. },
),
sides: gen_range(3, 8),
size: size,
sides: sides,
radius: radius,
omega: gen_range(50., 200.) * if gen_range(0., 1.) > 0.5 { -1. } else { 1. },
..Default::default()
rot: 0.,
alive: true,
}
}
pub fn update(&mut self) {
self.pos += self.vel * get_frame_time();
self.rot += self.omega * get_frame_time();
pub fn check_collision(&mut self, pos: Vec2) -> bool {
let collided = (pos.x - self.pos.x) * (pos.x - self.pos.x)
+ (pos.y - self.pos.y) * (pos.y - self.pos.y)
<= self.radius * self.radius;
if collided {
self.alive = false;
}
return collided;
}
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
pub fn update(&mut self) {
if self.alive {
self.pos += self.vel * get_frame_time();
self.rot += self.omega * get_frame_time();
self.alive = self.pos.y.abs() < screen_height() * 0.51 + self.radius
&& self.pos.x.abs() < screen_width() * 0.51 + self.radius;
}
}
// pub fn is_visible(&self) -> bool {
// }
pub fn draw(&self) {
draw_poly_lines(
self.pos.x,

View File

@ -1,6 +1,8 @@
use std::f32::consts::PI;
use std::{f32::consts::PI, path::Iter};
use macroquad::{prelude::*, rand::gen_range};
use crate::asteroids::Asteroid;
#[derive(Default)]
pub struct Player {
pos: Vec2,
@ -24,6 +26,15 @@ impl Player {
}
}
pub fn check_bullet_collisions(&mut self, asteroid: &mut Asteroid) {
for bullet in &mut self.bullets {
if asteroid.check_collision(bullet.pos) {
bullet.alive = false;
return;
}
}
}
pub fn update(&mut self) {
let mut mag = 0.;
if is_key_down(KeyCode::Right) {
@ -43,15 +54,10 @@ impl Player {
self.bullets.push(Bullet {
pos: self.pos + self.dir.rotate(vec2(20., 0.)),
vel: self.dir.rotate(vec2(500., 0.)) + self.vel,
alive: true,
});
}
}
self.bullets.retain(|bullet| {
bullet.pos.x.abs() * 2. < screen_width() && bullet.pos.y.abs() * 2. < screen_height()
});
for bullet in &mut self.bullets {
bullet.update();
}
self.vel += (mag * self.dir - self.drag * self.vel.length() * self.vel) * get_frame_time();
self.pos += self.vel * get_frame_time();
@ -61,6 +67,13 @@ impl Player {
if self.pos.y.abs() > screen_height() / 2. + 10. {
self.pos.y *= -1.;
}
for bullet in &mut self.bullets {
bullet.update();
}
self.bullets.retain(|b| {
b.alive && b.pos.x.abs() * 2. < screen_width() && b.pos.y.abs() * 2. < screen_height()
});
}
pub fn draw(&self) {
@ -88,6 +101,7 @@ impl Player {
struct Bullet {
pos: Vec2,
vel: Vec2,
alive: bool,
}
impl Bullet {

View File

@ -1,4 +1,7 @@
use crate::{asteroids::Asteroid, player::Player};
use crate::{
asteroids::{Asteroid, AsteroidSize},
player::Player,
};
use macroquad::{prelude::*, rand::gen_range};
#[derive(Default)]
@ -18,13 +21,14 @@ impl World {
pub fn update(&mut self) {
self.player.update();
// println!("{}", self.asteroids.len());
self.asteroids.retain(|asteroid| asteroid.is_visible());
for asteroid in &mut self.asteroids {
asteroid.update();
self.player.check_bullet_collisions(asteroid);
}
self.asteroids.retain(|asteroid| asteroid.alive);
if self.asteroids.len() < 5 {
self.asteroids.push(Asteroid::new());
println!("Added {}", get_time());
self.asteroids.push(Asteroid::new(AsteroidSize::Large));
// println!("Added {}", get_time());
}
}