player collisions

This commit is contained in:
sparshg 2022-10-09 11:10:56 +05:30
parent d5a0fd4374
commit 3d633c435d
4 changed files with 58 additions and 24 deletions

View File

@ -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,
);
}

View File

@ -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
}
}

View File

@ -13,6 +13,7 @@ pub struct Player {
bullets: Vec<Bullet>,
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) {

View File

@ -8,6 +8,8 @@ use macroquad::{prelude::*, rand::gen_range};
pub struct World {
player: Player,
asteroids: Vec<Asteroid>,
pub score: i32,
pub over: bool,
}
impl World {
@ -23,7 +25,11 @@ impl World {
let mut to_add: Vec<Asteroid> = 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,
);
}
}