Minor cleanup

master
Mark 2024-01-01 16:38:12 -08:00
parent 3784aa3e08
commit 2c953b95a3
Signed by: Mark
GPG Key ID: C6D63995FE72FD80
3 changed files with 18 additions and 14 deletions

View File

@ -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 { pub fn handle_projectile_collision(&mut self, ct: &content::Content, p: &Projectile) -> bool {
let f = ct.get_faction(self.faction); let f = ct.get_faction(self.faction);
let r = f.relationships.get(&p.faction).unwrap(); let r = f.relationships.get(&p.faction).unwrap();

View File

@ -49,8 +49,8 @@ impl ShipWorldObject {
} }
} }
/// Apply the effects of all active controls /// Step this ship's state by t seconds
pub fn tick(&mut self, r: &mut RigidBody, t: f32) { pub fn step(&mut self, r: &mut RigidBody, t: f32) {
let ship_rot = util::rigidbody_rotation(r); let ship_rot = util::rigidbody_rotation(r);
let engine_force = ship_rot * t; let engine_force = ship_rot * t;

View File

@ -133,7 +133,6 @@ impl<'a> World {
} }
/// Add a ship to this physics system /// Add a ship to this physics system
/// TODO: decouple from Ship::new()
pub fn add_ship( pub fn add_ship(
&mut self, &mut self,
ct: &content::Content, ct: &content::Content,
@ -149,6 +148,7 @@ impl<'a> World {
// (Collider starts pointing east, sprite starts pointing north.) // (Collider starts pointing east, sprite starts pointing north.)
.rotation(PI / -2.0) .rotation(PI / -2.0)
.mass(ship_content.mass); .mass(ship_content.mass);
// TODO: only build colliders once
let rb = RigidBodyBuilder::dynamic() let rb = RigidBodyBuilder::dynamic()
.angular_damping(ship_content.angular_drag) .angular_damping(ship_content.angular_drag)
@ -171,26 +171,25 @@ impl<'a> World {
/// Step this physics system by `t` seconds /// Step this physics system by `t` seconds
pub fn step(&mut self, t: f32, ct: &content::Content) { pub fn step(&mut self, t: f32, ct: &content::Content) {
// Run ship updates // Run ship updates
// TODO: Clean this mess // TODO: maybe reorganize projectile creation?
let mut ps = Vec::new(); let mut projectiles = Vec::new();
let mut to_remove = Vec::new(); let mut to_remove = Vec::new();
for (_, s) in &mut self.ships { for (_, s) in &mut self.ships {
if s.ship.hull <= 0.0 { if s.ship.is_destroyed() {
to_remove.push(s.physics_handle); to_remove.push(s.physics_handle);
continue; continue;
} }
let r = &mut self.wrapper.rigid_body_set[s.physics_handle.0]; let r = &mut self.wrapper.rigid_body_set[s.physics_handle.0];
s.tick(r, t); s.step(r, t);
if s.controls.guns { 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 { let _ = projectiles
self.remove_ship(r); .into_iter()
} .map(|(s, p)| self.add_projectiles(&s, p));
for (s, p) in ps { let _ = to_remove.into_iter().map(|s| self.remove_ship(s));
self.add_projectiles(&s, p);
}
// Update physics // Update physics
self.wrapper.step(t, &self.collision_handler); self.wrapper.step(t, &self.collision_handler);