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