From 680f5c57d82f1080f707e4ec43a3800a9377ef83 Mon Sep 17 00:00:00 2001 From: sparshg <43041139+sparshg@users.noreply.github.com> Date: Sat, 8 Oct 2022 20:57:05 +0530 Subject: [PATCH] filter -> retain --- src/asteroids.rs | 24 +++++++++++++++++------- src/main.rs | 15 ++++++++------- src/player.rs | 23 +++++++++++------------ src/utils.rs | 30 ++++++++++++++++++++++++++++++ src/world.rs | 37 +++++++++++++++++++++++++++++++++++++ 5 files changed, 103 insertions(+), 26 deletions(-) create mode 100644 src/utils.rs create mode 100644 src/world.rs diff --git a/src/asteroids.rs b/src/asteroids.rs index 9c403b1..e7111a4 100644 --- a/src/asteroids.rs +++ b/src/asteroids.rs @@ -2,7 +2,7 @@ use macroquad::{prelude::*, rand::gen_range}; #[derive(Default)] pub struct Asteroid { - pos: Vec2, + pub pos: Vec2, vel: Vec2, sides: u8, radius: f32, @@ -12,6 +12,7 @@ pub struct Asteroid { impl Asteroid { pub fn new() -> Self { + let radius = gen_range(30., 50.); let mut r = vec2( if gen_range(0., 1.) > 0.5 { -1. } else { 1. }, gen_range(-1., 1.), @@ -19,15 +20,19 @@ impl Asteroid { if gen_range(0., 1.) > 0.5 { r = vec2(r.y, r.x); } - r *= vec2(screen_width() / 2. + 100., screen_height() / 2. + 100.); + r *= vec2( + screen_width() * 0.5 + radius, + screen_height() * 0.5 + radius, + ); Self { pos: r, - vel: vec2( - gen_range(100., 200.) * if gen_range(0., 1.) > 0.5 { -1. } else { 1. }, - gen_range(100., 200.) * if gen_range(0., 1.) > 0.5 { -1. } else { 1. }, - ), + vel: 0.04 * -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), - radius: gen_range(10., 50.), + radius: radius, omega: gen_range(50., 200.) * if gen_range(0., 1.) > 0.5 { -1. } else { 1. }, ..Default::default() } @@ -38,6 +43,11 @@ impl Asteroid { self.rot += self.omega * get_frame_time(); } + 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 draw(&self) { draw_poly_lines( self.pos.x, diff --git a/src/main.rs b/src/main.rs index 96a527c..250a717 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,12 @@ mod asteroids; mod player; +mod utils; +mod world; use asteroids::Asteroid; use macroquad::prelude::*; use player::Player; +use world::World; #[macroquad::main("Camera")] async fn main() { @@ -12,16 +15,14 @@ async fn main() { zoom: vec2(2. / screen_width(), 2. / screen_height()), ..Default::default() }; - let mut asteroid = Asteroid::new(); set_camera(&cam); + let mut world = World::new(); + loop { clear_background(BLACK); - asteroid.update(); - asteroid.draw(); + world.update(); + world.draw(); + next_frame().await } } - -pub fn rotate_vec(vec: Vec2, angle: f32) -> Vec2 { - vec2(angle.cos(), angle.sin()).rotate(vec) -} diff --git a/src/player.rs b/src/player.rs index 2759d29..42fe346 100644 --- a/src/player.rs +++ b/src/player.rs @@ -46,17 +46,12 @@ impl Player { }); } } - - self.bullets - .iter_mut() - .filter(|bullet| { - bullet.pos.x.abs() * 2. < screen_width() - && bullet.pos.y.abs() * 2. < screen_height() - }) - .for_each(|bullet| { - bullet.update(); - bullet.draw(); - }); + 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(); @@ -80,9 +75,13 @@ impl Player { draw_line(p1.x, p1.y, p2.x, p2.y, 2., WHITE); draw_line(p1.x, p1.y, p3.x, p3.y, 2., WHITE); draw_line(p4.x, p4.y, p5.x, p5.y, 2., WHITE); - if is_key_down(KeyCode::Up) && gen_range(0., 1.) < 0.5 { + if is_key_down(KeyCode::Up) && gen_range(0., 1.) < 0.4 { draw_triangle_lines(p6, p7, p8, 2., WHITE); } + + for bullet in &self.bullets { + bullet.draw(); + } } } diff --git a/src/utils.rs b/src/utils.rs new file mode 100644 index 0000000..970206b --- /dev/null +++ b/src/utils.rs @@ -0,0 +1,30 @@ +use macroquad::prelude::*; + +pub fn rotate_vec(vec: Vec2, angle: f32) -> Vec2 { + vec2(angle.cos(), angle.sin()).rotate(vec) +} + +pub fn draw_polygon(x: f32, y: f32, points: Vec, color: Color) { + let points_length = points.len(); + let mut vertices = Vec::with_capacity(points_length as usize + 2); + let mut indices = Vec::::with_capacity(points_length as usize * 3); + + for (i, point) in points.iter().enumerate() { + let vertex = macroquad::models::Vertex { + position: Vec3::new(x + point.x, y + point.y, 0.0), + uv: Vec2::default(), + color, + }; + + vertices.push(vertex); + indices.extend_from_slice(&[0, i as u16 + 1, i as u16 + 2]); + } + + let mesh = Mesh { + vertices, + indices, + texture: None, + }; + + draw_mesh(&mesh); +} diff --git a/src/world.rs b/src/world.rs new file mode 100644 index 0000000..c22bb8f --- /dev/null +++ b/src/world.rs @@ -0,0 +1,37 @@ +use crate::{asteroids::Asteroid, player::Player}; +use macroquad::{prelude::*, rand::gen_range}; + +#[derive(Default)] +pub struct World { + player: Player, + asteroids: Vec, +} + +impl World { + pub fn new() -> Self { + Self { + player: Player::new(), + ..Default::default() + } + } + + 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(); + } + if self.asteroids.len() < 5 { + self.asteroids.push(Asteroid::new()); + println!("Added {}", get_time()); + } + } + + pub fn draw(&self) { + self.player.draw(); + for asteroid in &self.asteroids { + asteroid.draw(); + } + } +}