From 2c953b95a3d51408e859867d211d4ea97457ae08 Mon Sep 17 00:00:00 2001 From: Mark Date: Mon, 1 Jan 2024 16:38:12 -0800 Subject: [PATCH] Minor cleanup --- crates/gameobject/src/ship.rs | 5 +++++ crates/world/src/objects/ship.rs | 4 ++-- crates/world/src/world.rs | 23 +++++++++++------------ 3 files changed, 18 insertions(+), 14 deletions(-) diff --git a/crates/gameobject/src/ship.rs b/crates/gameobject/src/ship.rs index 0dc9875..01db982 100644 --- a/crates/gameobject/src/ship.rs +++ b/crates/gameobject/src/ship.rs @@ -30,6 +30,11 @@ impl Ship { } } + /// Has this ship been destroyed? + pub fn is_destroyed(&self) -> bool { + self.hull <= 0.0 + } + pub fn handle_projectile_collision(&mut self, ct: &content::Content, p: &Projectile) -> bool { let f = ct.get_faction(self.faction); let r = f.relationships.get(&p.faction).unwrap(); diff --git a/crates/world/src/objects/ship.rs b/crates/world/src/objects/ship.rs index b15edb7..67e6851 100644 --- a/crates/world/src/objects/ship.rs +++ b/crates/world/src/objects/ship.rs @@ -49,8 +49,8 @@ impl ShipWorldObject { } } - /// Apply the effects of all active controls - pub fn tick(&mut self, r: &mut RigidBody, t: f32) { + /// Step this ship's state by t seconds + pub fn step(&mut self, r: &mut RigidBody, t: f32) { let ship_rot = util::rigidbody_rotation(r); let engine_force = ship_rot * t; diff --git a/crates/world/src/world.rs b/crates/world/src/world.rs index d5d69e4..99b33bf 100644 --- a/crates/world/src/world.rs +++ b/crates/world/src/world.rs @@ -133,7 +133,6 @@ impl<'a> World { } /// Add a ship to this physics system - /// TODO: decouple from Ship::new() pub fn add_ship( &mut self, ct: &content::Content, @@ -149,6 +148,7 @@ impl<'a> World { // (Collider starts pointing east, sprite starts pointing north.) .rotation(PI / -2.0) .mass(ship_content.mass); + // TODO: only build colliders once let rb = RigidBodyBuilder::dynamic() .angular_damping(ship_content.angular_drag) @@ -171,26 +171,25 @@ impl<'a> World { /// Step this physics system by `t` seconds pub fn step(&mut self, t: f32, ct: &content::Content) { // Run ship updates - // TODO: Clean this mess - let mut ps = Vec::new(); + // TODO: maybe reorganize projectile creation? + let mut projectiles = Vec::new(); let mut to_remove = Vec::new(); for (_, s) in &mut self.ships { - if s.ship.hull <= 0.0 { + if s.ship.is_destroyed() { to_remove.push(s.physics_handle); continue; } + let r = &mut self.wrapper.rigid_body_set[s.physics_handle.0]; - s.tick(r, t); + s.step(r, t); if s.controls.guns { - ps.push((s.physics_handle, s.ship.fire_guns())); + projectiles.push((s.physics_handle, s.ship.fire_guns())); } } - for r in to_remove { - self.remove_ship(r); - } - for (s, p) in ps { - self.add_projectiles(&s, p); - } + let _ = projectiles + .into_iter() + .map(|(s, p)| self.add_projectiles(&s, p)); + let _ = to_remove.into_iter().map(|s| self.remove_ship(s)); // Update physics self.wrapper.step(t, &self.collision_handler);