base game done

This commit is contained in:
sparshg 2022-10-09 01:47:06 +05:30
parent 7e2b07ca48
commit d5a0fd4374
3 changed files with 47 additions and 9 deletions

View File

@ -7,8 +7,8 @@ pub enum AsteroidSize {
}
pub struct Asteroid {
pub pos: Vec2,
vel: Vec2,
size: AsteroidSize,
pub vel: Vec2,
pub size: AsteroidSize,
sides: u8,
radius: f32,
rot: f32,
@ -20,8 +20,8 @@ impl Asteroid {
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.)),
AsteroidSize::Medium => (gen_range(5, 6), gen_range(30., 40.)),
AsteroidSize::Small => (gen_range(3, 5), 20.),
};
let mut r = vec2(
if gen_range(0., 1.) > 0.5 { -1. } else { 1. },
@ -50,6 +50,13 @@ impl Asteroid {
}
}
pub fn new_from(pos: Vec2, vel: Vec2, size: AsteroidSize) -> Self {
let mut asteroid = Asteroid::new(size);
asteroid.pos = pos;
asteroid.vel = vel;
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)

View File

@ -26,13 +26,14 @@ impl Player {
}
}
pub fn check_bullet_collisions(&mut self, asteroid: &mut Asteroid) {
pub fn check_bullet_collisions(&mut self, asteroid: &mut Asteroid) -> bool {
for bullet in &mut self.bullets {
if asteroid.check_collision(bullet.pos) {
bullet.alive = false;
return;
return true;
}
}
false
}
pub fn update(&mut self) {

View File

@ -20,15 +20,45 @@ impl World {
pub fn update(&mut self) {
self.player.update();
// println!("{}", self.asteroids.len());
let mut to_add: Vec<Asteroid> = Vec::new();
for asteroid in &mut self.asteroids {
asteroid.update();
self.player.check_bullet_collisions(asteroid);
if self.player.check_bullet_collisions(asteroid) {
match asteroid.size {
AsteroidSize::Large => {
let rand = vec2(gen_range(-50., 50.), gen_range(-50., 50.));
to_add.push(Asteroid::new_from(
asteroid.pos,
asteroid.vel + rand,
AsteroidSize::Medium,
));
to_add.push(Asteroid::new_from(
asteroid.pos,
asteroid.vel - rand,
AsteroidSize::Medium,
));
}
AsteroidSize::Medium => {
let rand = vec2(gen_range(-30., 30.), gen_range(-30., 30.));
to_add.push(Asteroid::new_from(
asteroid.pos,
asteroid.vel + rand,
AsteroidSize::Small,
));
to_add.push(Asteroid::new_from(
asteroid.pos,
asteroid.vel - rand,
AsteroidSize::Small,
));
}
_ => {}
}
}
}
self.asteroids.append(&mut to_add);
self.asteroids.retain(|asteroid| asteroid.alive);
if self.asteroids.len() < 5 {
self.asteroids.push(Asteroid::new(AsteroidSize::Large));
// println!("Added {}", get_time());
}
}