summaryrefslogtreecommitdiff
path: root/src/sim
diff options
context:
space:
mode:
authorKai Stevenson <kai@kaistevenson.com>2026-08-27 00:26:35 -0700
committerKai Stevenson <kai@kaistevenson.com>2026-08-27 00:26:35 -0700
commit6fcf3b4e085ab8009ff1b0fd34e3f0ff8765465b (patch)
tree38e451957e95f3063332883e37a05d31e80e245b /src/sim
parenta272aa9d9b2c642871abe2dc3379e6ffcce8bf56 (diff)
chair, performance improvement
Diffstat (limited to 'src/sim')
-rw-r--r--src/sim/entity/mod.rs20
-rw-r--r--src/sim/sim_manager/utils.rs3
2 files changed, 22 insertions, 1 deletions
diff --git a/src/sim/entity/mod.rs b/src/sim/entity/mod.rs
index af4f0dd..fda0a79 100644
--- a/src/sim/entity/mod.rs
+++ b/src/sim/entity/mod.rs
@@ -10,6 +10,7 @@ use crate::{
sim::{
cell::Cell, lib::marching_squares::Marchable, rb_manager::RbManager, sim_manager::SimCtx,
},
+ sprite_loader::load_sprite_to_cells,
};
fn mass_from_cells(cells: &[Cell]) -> f32 {
@@ -103,6 +104,25 @@ impl EntityDef {
behaviour,
}
}
+
+ pub fn from_sprite(
+ position: Vec2,
+ path: &str,
+ behaviour: Option<Box<dyn EntityBehaviour>>,
+ ) -> Self {
+ let sprite_cells = load_sprite_to_cells(path);
+ let mut entity_cells = EntityCells {
+ cells: sprite_cells.cells,
+ size: IVec2::new(sprite_cells.width as i32, sprite_cells.height as i32),
+ };
+
+ entity_cells
+ .cells
+ .iter_mut()
+ .for_each(|c| c.set_entity_integrated(true));
+
+ EntityDef::from_cells(position, entity_cells, behaviour)
+ }
}
#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)]
diff --git a/src/sim/sim_manager/utils.rs b/src/sim/sim_manager/utils.rs
index 75ef71d..d744ec0 100644
--- a/src/sim/sim_manager/utils.rs
+++ b/src/sim/sim_manager/utils.rs
@@ -117,7 +117,7 @@ pub fn read_back_entities_from_world(
for (lx, ly, x, y) in cells_written {
// update the entity
// TODO optimize
- let new_local_cell = sim.cell_manager.get_cell_from_game_position(x, y).unwrap();
+ let mut new_local_cell = sim.cell_manager.get_cell_from_game_position(x, y).unwrap();
if !new_local_cell.entity_integrated() {
// this means that the entity changed in some way, so we should recompute its shape
// TODO wait N frames to debounce this
@@ -127,6 +127,7 @@ pub fn read_back_entities_from_world(
let entity_cells = entity.data.cells.as_mut().unwrap();
// we do this unconditionally because it's cheaper than checking if it actually needs to be updated
// and because we don't have a good way to track changes to cell state
+ new_local_cell.set_entity_integrated(true);
entity_cells
.set_cell_at_local_position(IVec2::new(lx as i32, ly as i32), new_local_cell);
}