filter -> retain

This commit is contained in:
sparshg 2022-10-08 20:57:05 +05:30
parent c4cfed13ec
commit 680f5c57d8
5 changed files with 103 additions and 26 deletions

View File

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

View File

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

View File

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

30
src/utils.rs Normal file
View File

@ -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<Vec2>, color: Color) {
let points_length = points.len();
let mut vertices = Vec::with_capacity(points_length as usize + 2);
let mut indices = Vec::<u16>::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);
}

37
src/world.rs Normal file
View File

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