From 3d633c435d42e80c96cebebe5b0ae5c0f793193f Mon Sep 17 00:00:00 2001 From: sparshg <43041139+sparshg@users.noreply.github.com> Date: Sun, 9 Oct 2022 11:10:56 +0530 Subject: [PATCH] player collisions --- src/asteroids.rs | 36 +++++++++++++++++++----------------- src/main.rs | 7 ++++--- src/player.rs | 17 ++++++++++++++--- src/world.rs | 22 +++++++++++++++++++++- 4 files changed, 58 insertions(+), 24 deletions(-) diff --git a/src/asteroids.rs b/src/asteroids.rs index 332033d..35d9a60 100644 --- a/src/asteroids.rs +++ b/src/asteroids.rs @@ -57,28 +57,26 @@ impl Asteroid { asteroid } - 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 check_collision(&mut self, pos: Vec2, rad: f32) -> bool { + (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) } 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; + // if self.alive { + self.pos += self.vel * get_frame_time(); + self.rot += self.omega * get_frame_time(); + if self.pos.x.abs() > screen_width() * 0.5 + self.radius { + self.pos.x *= -1.; } + if self.pos.y.abs() > screen_height() * 0.5 + self.radius { + self.pos.y *= -1.; + } + // 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, @@ -86,7 +84,11 @@ impl Asteroid { self.sides, self.radius, self.rot, - 2., + match self.size { + AsteroidSize::Large => 2., + AsteroidSize::Medium => 1.2, + AsteroidSize::Small => 0.8, + }, WHITE, ); } diff --git a/src/main.rs b/src/main.rs index 250a717..f492157 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,7 +12,7 @@ use world::World; async fn main() { rand::srand(macroquad::miniquad::date::now() as _); let cam = Camera2D { - zoom: vec2(2. / screen_width(), 2. / screen_height()), + zoom: vec2(2. / screen_width(), -2. / screen_height()), ..Default::default() }; set_camera(&cam); @@ -20,9 +20,10 @@ async fn main() { loop { clear_background(BLACK); - world.update(); + if !world.over { + world.update(); + } world.draw(); - next_frame().await } } diff --git a/src/player.rs b/src/player.rs index 5bdb6b0..d67c816 100644 --- a/src/player.rs +++ b/src/player.rs @@ -13,6 +13,7 @@ pub struct Player { bullets: Vec, last_shot: f32, shot_interval: f32, + alive: bool, } impl Player { @@ -22,13 +23,23 @@ impl Player { rot: PI / 2., drag: 0.001, shot_interval: 0.3, + alive: true, ..Default::default() } } + pub fn check_player_collision(&mut self, asteroid: &mut Asteroid) -> bool { + if asteroid.check_collision(self.pos, 8.) { + self.alive = false; + return true; + } + false + } + pub fn check_bullet_collisions(&mut self, asteroid: &mut Asteroid) -> bool { for bullet in &mut self.bullets { - if asteroid.check_collision(bullet.pos) { + if asteroid.check_collision(bullet.pos, 0.) { + asteroid.alive = false; bullet.alive = false; return true; } @@ -39,11 +50,11 @@ impl Player { pub fn update(&mut self) { let mut mag = 0.; if is_key_down(KeyCode::Right) { - self.rot -= 5. * get_frame_time(); + self.rot += 5. * get_frame_time(); self.dir = vec2(self.rot.cos(), self.rot.sin()); } if is_key_down(KeyCode::Left) { - self.rot += 5. * get_frame_time(); + self.rot -= 5. * get_frame_time(); self.dir = vec2(self.rot.cos(), self.rot.sin()); } if is_key_down(KeyCode::Up) { diff --git a/src/world.rs b/src/world.rs index c0440dc..5f510ef 100644 --- a/src/world.rs +++ b/src/world.rs @@ -8,6 +8,8 @@ use macroquad::{prelude::*, rand::gen_range}; pub struct World { player: Player, asteroids: Vec, + pub score: i32, + pub over: bool, } impl World { @@ -23,7 +25,11 @@ impl World { let mut to_add: Vec = Vec::new(); for asteroid in &mut self.asteroids { asteroid.update(); + if self.player.check_player_collision(asteroid) { + self.over = true; + } if self.player.check_bullet_collisions(asteroid) { + self.score += 1; match asteroid.size { AsteroidSize::Large => { let rand = vec2(gen_range(-50., 50.), gen_range(-50., 50.)); @@ -57,7 +63,14 @@ impl World { } self.asteroids.append(&mut to_add); self.asteroids.retain(|asteroid| asteroid.alive); - if self.asteroids.len() < 5 { + if self.asteroids.iter().fold(0, |acc, x| { + acc + match x.size { + AsteroidSize::Large => 4, + AsteroidSize::Medium => 2, + AsteroidSize::Small => 1, + } + }) < 20 + { self.asteroids.push(Asteroid::new(AsteroidSize::Large)); } } @@ -67,5 +80,12 @@ impl World { for asteroid in &self.asteroids { asteroid.draw(); } + draw_text( + &format!("Score {}", self.score), + 20. - screen_width() * 0.5, + 30. - screen_height() * 0.5, + 32., + WHITE, + ); } }