- commit
- 0a3ae6623d8bc7398ea15a9b9c35aacea1bca8fd
- parent
- 462f63afe1e0dfed51341f985f66435d051fe15e
- Author
- Tobias Bengfort <tobias.bengfort@posteo.de>
- Date
- 2023-03-26 21:19
refactor: Pos.in_radius()
Diffstat
| M | src/game.rs | 21 | ++++++++++----------- |
1 files changed, 10 insertions, 11 deletions
diff --git a/src/game.rs b/src/game.rs
@@ -29,6 +29,14 @@ pub struct Pos {
29 29 pub y: f32,
30 30 }
31 31
-1 32 impl Pos {
-1 33 pub fn in_radius(&self, other: &Self, d: f32) -> bool {
-1 34 let dx = self.x - other.x;
-1 35 let dy = self.y - other.y;
-1 36 return dx * dx + dy * dy < d * d;
-1 37 }
-1 38 }
-1 39
32 40 pub struct Player {
33 41 pub p: Pos,
34 42 pub dir: Option<Dir>,
@@ -248,14 +256,8 @@ impl Game {
248 256 enemy.health -= self.player.power * dt;
249 257 }
250 258 for projectile in self.projectiles.iter() {
251 -1 let projectile_dx = projectile.p.x - enemy.p.x;
252 -1 let projectile_dy = projectile.p.y - enemy.p.y;
253 259 let projectile_size = enemy.t.size + projectile.t.size;
254 -1 let projectile_dx2 = projectile_dx * projectile_dx;
255 -1 let projectile_dy2 = projectile_dy * projectile_dy;
256 -1 let projectile_size2 = projectile_size * projectile_size;
257 -1
258 -1 if projectile_dx2 + projectile_dy2 < projectile_size2 {
-1 260 if projectile.p.in_radius(&enemy.p, projectile_size) {
259 261 enemy.health -= projectile.t.damage * self.player.power * dt;
260 262 }
261 263 }
@@ -281,10 +283,7 @@ impl Game {
281 283 self.diamonds = std::mem::take(&mut self.diamonds)
282 284 .into_iter()
283 285 .filter(|diamond| {
284 -1 let dx = self.player.p.x - diamond.x;
285 -1 let dy = self.player.p.y - diamond.y;
286 -1 let d = dx * dx + dy * dy;
287 -1 if d < self.player.diamond_radius * self.player.diamond_radius {
-1 286 if self.player.p.in_radius(&diamond, self.player.diamond_radius) {
288 287 self.player.xp += self.player.xp_factor;
289 288 return false;
290 289 } else {