Let bullets wrap around the screen

They are now killed once having travelled the entire diagonal dimension
of the screen, which removes the last dependence on "true" x/y position.
Now, only relative position matters.
This commit is contained in:
Leonora Tindall 2023-04-03 22:54:02 -05:00
parent 007eb03050
commit 179b503634
1 changed files with 13 additions and 2 deletions

View File

@ -196,6 +196,7 @@ impl Player {
pos: self.pos + self.dir * 20., pos: self.pos + self.dir * 20.,
vel: self.dir * 8.5 + self.vel, vel: self.dir * 8.5 + self.vel,
alive: true, alive: true,
travelled: Vec2::new(0., 0.),
}); });
} }
} }
@ -212,8 +213,7 @@ impl Player {
for bullet in &mut self.bullets { for bullet in &mut self.bullets {
bullet.update(); bullet.update();
} }
self.bullets self.bullets.retain(|b| b.alive);
.retain(|b| b.alive && b.pos.x.abs() * 2. < WIDTH && b.pos.y.abs() * 2. < HEIGHT);
self.asteroids = vec![]; self.asteroids = vec![];
} }
@ -277,11 +277,22 @@ struct Bullet {
pos: Vec2, pos: Vec2,
vel: Vec2, vel: Vec2,
alive: bool, alive: bool,
travelled: Vec2,
} }
impl Bullet { impl Bullet {
fn update(&mut self) { fn update(&mut self) {
self.pos += self.vel; self.pos += self.vel;
if self.pos.x.abs() > WIDTH * 0.5 {
self.pos.x *= -1.;
}
if self.pos.y.abs() > HEIGHT * 0.5 {
self.pos.y *= -1.;
}
self.travelled += self.vel;
if self.travelled.length() >= (WIDTH * WIDTH + HEIGHT * HEIGHT).sqrt() / 2. {
self.alive = false;
}
} }
fn draw(&self, c: Color) { fn draw(&self, c: Color) {
draw_circle(self.pos.x, self.pos.y, 2., Color::new(c.r, c.g, c.b, 0.9)); draw_circle(self.pos.x, self.pos.y, 2., Color::new(c.r, c.g, c.b, 0.9));