diff options
Diffstat (limited to 'src/sim/sim_manager/mod.rs')
| -rw-r--r-- | src/sim/sim_manager/mod.rs | 49 |
1 files changed, 45 insertions, 4 deletions
diff --git a/src/sim/sim_manager/mod.rs b/src/sim/sim_manager/mod.rs index b6d2528..2b8bead 100644 --- a/src/sim/sim_manager/mod.rs +++ b/src/sim/sim_manager/mod.rs @@ -38,6 +38,12 @@ pub struct SimManager { pub entities: FxHashMap<u32, Entity>, } +pub struct SimCtx<'a> { + pub cell_manager: &'a mut CellManager, + pub rb_manager: &'a mut RbManager, + pub particle_manager: &'a mut ParticleManager, +} + impl SimManager { pub fn create_entity(&mut self, def: EntityDef) -> u32 { let (rb_h, collider_h) = if let Some(rb) = def.rb { @@ -84,7 +90,7 @@ impl SimManager { pub fn destroy_entity(&mut self, id: u32) { if let Some(entity) = self.entities.get(&id) { - if let Some(rb_h) = entity.rb_h { + if let Some(rb_h) = entity.data.rb_h { self.rb_manager.physics_manager.rigid_body_set.remove( rb_h, &mut self.rb_manager.physics_manager.island_manager, @@ -93,7 +99,7 @@ impl SimManager { &mut self.rb_manager.physics_manager.multibody_joint_set, true, ); - } else if let Some(collider_h) = entity.collider_h { + } else if let Some(collider_h) = entity.data.collider_h { self.rb_manager.physics_manager.collider_set.remove( collider_h, &mut self.rb_manager.physics_manager.island_manager, @@ -135,7 +141,7 @@ impl SimManager { f = new_local_cell.flags ); } - if let Some(cells) = &mut entity.cells { + if let Some(cells) = &mut entity.data.cells { cells.set_cell_at_local_position( IVec2::new(lx as i32, ly as i32), new_local_cell, @@ -149,6 +155,23 @@ impl SimManager { } fn physics_update(&mut self, delta_time: f32) { + let entity_ids: Vec<u32> = self.entities.keys().cloned().collect(); + for id in entity_ids { + let entity = self.entities.get_mut(&id); + let mut ctx = SimCtx { + cell_manager: &mut self.cell_manager, + rb_manager: &mut self.rb_manager, + particle_manager: &mut self.particle_manager, + }; + if let Some(entity) = entity { + if let Some(result) = entity.physics_update(&mut ctx, delta_time) { + for d in result.deferred_destructions { + self.destroy_entity(d); + } + } + } + } + // before we move the rigidbodies, upsert the current terrain state // TODO use the chunk sleeping, and make this range dynamic for cx in -5..5 { @@ -173,7 +196,25 @@ impl SimManager { .tick(&mut self.cell_manager, delta_time); } - pub fn update(&mut self, config: &Config, _delta_time: f32) -> () { + pub fn update(&mut self, config: &Config, delta_time: f32) -> () { + let entity_ids: Vec<u32> = self.entities.keys().cloned().collect(); + for id in entity_ids { + if let Some(entity) = self.entities.get_mut(&id) { + let mut ctx = SimCtx { + cell_manager: &mut self.cell_manager, + rb_manager: &mut self.rb_manager, + particle_manager: &mut self.particle_manager, + }; + + entity.update(&mut ctx, delta_time); + if let Some(result) = entity.update(&mut ctx, delta_time) { + for d in result.deferred_destructions { + self.destroy_entity(d); + } + } + } + } + let now = Instant::now(); let secs_since_last_cell_update = (now - self.last_cell_update).as_secs_f32(); let expected_secs_since_last_cell_update = 1.0 / SIM_FPS as f32; |
