From 54ae26e7c41f6c0a465a55c3cc5ee16ff81a1d70 Mon Sep 17 00:00:00 2001 From: Kai Stevenson Date: Mon, 17 Aug 2026 18:18:25 -0700 Subject: refactor/UX --- src/main.rs | 47 +++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 39 insertions(+), 8 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index ccc5ec5..043d125 100644 --- a/src/main.rs +++ b/src/main.rs @@ -24,11 +24,9 @@ use crate::{ renderer::RendererState, sim::{ cell::{cell::Cell, materials::MaterialId}, - cell_sim::{ - sim::{sim_tick, write_rb_entity_to_world}, - world::World, - }, + cell_sim::{sim::sim_tick, world::World}, rb_sim::RbSimManager, + write_rb_entity_to_world, }, }; @@ -146,9 +144,39 @@ impl App { // will be called before the render and before the physics update(s) // may be called multiple times if the sim time is behind fn sim_update(&mut self) { - if let Some(world) = &mut self.world { + if let Some(world) = &mut self.world + && let Some(rbsm) = &mut self.rb_sim_manager + { + // before we tick, write all the rb entities into the sim world + // TODO optimize + let entity_ids: Vec = rbsm.rb_entities.keys().copied().collect(); + let mut cells_written_by_entity: Vec<(u32, Vec<(u8, u8, i32, i32)>)> = Vec::new(); + + for entity_id in entity_ids { + let cells_written = + write_rb_entity_to_world(world, rbsm, entity_id, self.sim_seqno); + cells_written_by_entity.push((entity_id, cells_written)); + } + sim_tick(world, self.sim_seqno, self.config.use_threading); self.sim_seqno += 1; + + // after we tick, remove the written rb cells and update the entities + // TODO optimize + for (entity_id, cells_written) in cells_written_by_entity { + let rb_entity = rbsm.rb_entities.get_mut(&entity_id).unwrap(); + for (lx, ly, x, y) in cells_written { + // update the entity + // TODO we should skip cells that weren't changed? + let new_local_cell = world.get_cell_from_game_position(x, y).unwrap(); + // if new_local_cell.material != MaterialId::Void && !new_local_cell.rb() { + // panic!("Someone swapped into this rb's cell!"); + // } + rb_entity.set_cell_at_local_position(lx, ly, new_local_cell); + // update the world + world.set_cell_from_game_position(x, y, Cell::void(), false); + } + } } } // called PHYSICS_FPS times per second @@ -270,9 +298,12 @@ impl ApplicationHandler for App { KeyCode::KeyD => self.input.is_right_pressed = pressed, KeyCode::KeyC => self.world = Some(World::from_default_size()), KeyCode::KeyV => { - self.rb_sim_manager - .as_mut() - .map(|rbsm| rbsm.rb_entities = FxHashMap::default()); + if let Some(rbsm) = self.rb_sim_manager.as_mut() { + let entity_ids: Vec = rbsm.rb_entities.keys().copied().collect(); + for entity_id in entity_ids { + rbsm.destroy_rb_entity(entity_id); + } + } } KeyCode::KeyP if pressed && !repeat => { if let Some(rbsm) = self.rb_sim_manager.as_mut() { -- cgit v1.3.1