From 7e2b07ca48f91ae183333e7050117128289178fd Mon Sep 17 00:00:00 2001 From: sparshg <43041139+sparshg@users.noreply.github.com> Date: Sun, 9 Oct 2022 01:17:47 +0530 Subject: [PATCH] bullet collisions --- src/asteroids.rs | 48 ++++++++++++++++++++++++++++++++++++------------ src/player.rs | 28 +++++++++++++++++++++------- src/world.rs | 12 ++++++++---- 3 files changed, 65 insertions(+), 23 deletions(-) diff --git a/src/asteroids.rs b/src/asteroids.rs index e7111a4..bb1d826 100644 --- a/src/asteroids.rs +++ b/src/asteroids.rs @@ -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, diff --git a/src/player.rs b/src/player.rs index 42fe346..9a4bf94 100644 --- a/src/player.rs +++ b/src/player.rs @@ -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 { diff --git a/src/world.rs b/src/world.rs index c22bb8f..e95e08b 100644 --- a/src/world.rs +++ b/src/world.rs @@ -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()); } }