summaryrefslogtreecommitdiff
path: root/src
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
parenta272aa9d9b2c642871abe2dc3379e6ffcce8bf56 (diff)
chair, performance improvement
Diffstat (limited to 'src')
-rw-r--r--src/content/entities/entity_tnt.rs23
-rw-r--r--src/main.rs10
-rw-r--r--src/sim/entity/mod.rs20
-rw-r--r--src/sim/sim_manager/utils.rs3
-rw-r--r--src/sprite_loader.rs6
5 files changed, 43 insertions, 19 deletions
diff --git a/src/content/entities/entity_tnt.rs b/src/content/entities/entity_tnt.rs
index 2e4cef5..595ebd4 100644
--- a/src/content/entities/entity_tnt.rs
+++ b/src/content/entities/entity_tnt.rs
@@ -1,12 +1,9 @@
-use glam::{IVec2, Vec2};
+use glam::Vec2;
-use crate::{
- sim::{
- entity::{EntityBehaviour, EntityCells, EntityDef, EntityUpdateCtx},
- lib::force::apply_explosion,
- sim_manager::SimCtx,
- },
- sprite_loader::load_sprite_to_cells,
+use crate::sim::{
+ entity::{EntityBehaviour, EntityDef, EntityUpdateCtx},
+ lib::force::apply_explosion,
+ sim_manager::SimCtx,
};
struct TntEntityBehaviour {
@@ -30,15 +27,9 @@ impl EntityBehaviour for TntEntityBehaviour {
}
pub fn entity_tnt_def(position: Vec2) -> EntityDef {
- let sprite_cells = load_sprite_to_cells("assets/sprites/tnt");
- let entity_cells = EntityCells {
- cells: sprite_cells.cells,
- size: IVec2::new(sprite_cells.width as i32, sprite_cells.height as i32),
- };
-
- EntityDef::from_cells(
+ EntityDef::from_sprite(
position,
- entity_cells,
+ "assets/sprites/tnt",
Some(Box::new(TntEntityBehaviour { fuse: 3.0 })),
)
}
diff --git a/src/main.rs b/src/main.rs
index 00f2cbc..21460bd 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -31,7 +31,7 @@ use crate::{
InputManager,
},
renderer::RendererState,
- sim::{rb_manager::DebugRenderMode, sim_manager::SimManager},
+ sim::{entity::EntityDef, rb_manager::DebugRenderMode, sim_manager::SimManager},
vfx::VfxManager,
};
@@ -100,6 +100,14 @@ impl App {
sim.create_entity(entity_tnt_def(self.input_manager.world_mouse_pos));
}
+ if self.input_manager.pressed(Input::Action5) {
+ sim.create_entity(EntityDef::from_sprite(
+ self.input_manager.world_mouse_pos,
+ "assets/sprites/chair",
+ None,
+ ));
+ }
+
if self.input_manager.pressed(Input::Action3) {
sim.create_entity(entity_bullet_emitter_def(
self.input_manager.world_mouse_pos,
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);
}
diff --git a/src/sprite_loader.rs b/src/sprite_loader.rs
index eccf066..01d13aa 100644
--- a/src/sprite_loader.rs
+++ b/src/sprite_loader.rs
@@ -58,7 +58,11 @@ pub fn load_sprite_to_cells(path: &str) -> SpriteCells {
Ok(SpriteManifest { palette }) => {
let cells: Vec<Cell> = indices
.iter()
- .map(|i| Cell::from_material(palette.get(i).unwrap().material))
+ .map(|i| {
+ Cell::from_material(
+ palette.get(i).map_or(MaterialId::Void, |v| v.material),
+ )
+ })
.collect();
SpriteCells {