summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKai Stevenson <kai@kaistevenson.com>2026-08-23 11:25:20 -0700
committerKai Stevenson <kai@kaistevenson.com>2026-08-23 11:25:20 -0700
commit5f137b41ebeadfca3907221713f85f2c9fe431bf (patch)
tree80e271c7ad5fd074f27b33dcdcdafafc391d2d13
parent80824a8b69b70e6e40577e4f0236c28c4ad5c15b (diff)
entityID newtype
-rw-r--r--README.md3
-rw-r--r--src/main.rs3
-rw-r--r--src/renderer/mod.rs3
-rw-r--r--src/sim/cell.rs1
-rw-r--r--src/sim/cell_manager/manager.rs1
-rw-r--r--src/sim/entity/mod.rs13
-rw-r--r--src/sim/sim_manager/mod.rs26
-rw-r--r--src/sim/sim_manager/utils.rs7
8 files changed, 29 insertions, 28 deletions
diff --git a/README.md b/README.md
index 23d529b..ca85936 100644
--- a/README.md
+++ b/README.md
@@ -2,7 +2,6 @@
# TODO:
-- Add particles
- Check for partitioning rigidbodies
- Add tools: laser, gun, blower
- Add player character
@@ -11,7 +10,7 @@
- Could use this to have metal melt, sand turn to glass
- Looks nice for laser effects
- Should burning wood behave differently than burning oil?
-- Handle `rb` flag correctly
+- Handle `entity` flag correctly
- Turning a rigidbody into sand should unset all of its flags and kill the body
- Melting a rigidbody (see above) is more complicated, melting metal may shift slightly and reform, in which case it should stay attached, or melt off entirely, it which case it should not stay attached
- Optimize renderer to uncap rigidbody count
diff --git a/src/main.rs b/src/main.rs
index 7620f92..0daef91 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -30,6 +30,7 @@ use crate::{
sim::{
cell::Cell,
cell_manager::manager::CellManager,
+ entity::EntityId,
lib::force::{apply_bullet, apply_explosion},
rb_manager::DebugRenderMode,
sim_manager::{SimCtx, SimManager},
@@ -310,7 +311,7 @@ impl ApplicationHandler for App {
KeyCode::KeyC => sim.cell_manager = CellManager::from_default_size(),
KeyCode::KeyP => sim.particle_manager.particles = Vec::new(),
KeyCode::KeyV => {
- let ids: Vec<u32> = sim.entities.keys().cloned().collect();
+ let ids: Vec<EntityId> = sim.entities.keys().cloned().collect();
for id in ids {
sim.destroy_entity(id);
}
diff --git a/src/renderer/mod.rs b/src/renderer/mod.rs
index 29bd779..54a6667 100644
--- a/src/renderer/mod.rs
+++ b/src/renderer/mod.rs
@@ -13,6 +13,7 @@ use crate::{
renderer::ui::draw_egui,
sim::{
cell_manager::manager::CellManager,
+ entity::EntityId,
rb_manager::debug_render::DebugVertex,
sim_manager::{SimCtx, SimManager},
},
@@ -100,7 +101,7 @@ pub struct RendererState {
debug_vertex_buffer: wgpu::Buffer,
debug_vertex_capacity: usize,
- renderer_rb_entities: FxHashMap<u32, usize>,
+ renderer_rb_entities: FxHashMap<EntityId, usize>,
}
impl RendererState {
diff --git a/src/sim/cell.rs b/src/sim/cell.rs
index 5a9fc1f..ca0b969 100644
--- a/src/sim/cell.rs
+++ b/src/sim/cell.rs
@@ -4,6 +4,7 @@ use crate::{config::SETTLED_THRESOHLD, content::materials::MaterialId};
pub struct Cell {
pub material: MaterialId,
pub flags: u8,
+ // TODO move this to a per-chunk hashmap
pub data: u16,
}
diff --git a/src/sim/cell_manager/manager.rs b/src/sim/cell_manager/manager.rs
index 8706caa..6fa5f37 100644
--- a/src/sim/cell_manager/manager.rs
+++ b/src/sim/cell_manager/manager.rs
@@ -11,7 +11,6 @@ use crate::{
pub struct CellManager {
pub seqno: u64,
pub chunks: Vec<Chunk>,
- // TODO FxFxHashMap?
pub chunk_position_to_chunk_idx: FxHashMap<(i32, i32), usize>,
}
diff --git a/src/sim/entity/mod.rs b/src/sim/entity/mod.rs
index 0d8ae71..044d07c 100644
--- a/src/sim/entity/mod.rs
+++ b/src/sim/entity/mod.rs
@@ -89,20 +89,23 @@ impl EntityDef {
}
}
+#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)]
+pub struct EntityId(pub u32);
+
pub struct EntityData {
- pub id: u32,
+ pub id: EntityId,
pub rb_h: Option<RigidBodyHandle>,
pub collider_h: Option<ColliderHandle>,
pub cells: Option<EntityCells>,
}
pub struct EntityUpdateResult {
- pub deferred_destructions: Vec<u32>,
+ pub deferred_destructions: Vec<EntityId>,
}
pub struct EntityUpdateCtx<'a> {
pub entity_data: &'a mut EntityData,
- deferred_destructions: Vec<u32>,
+ deferred_destructions: Vec<EntityId>,
}
impl<'a> EntityUpdateCtx<'a> {
@@ -112,7 +115,7 @@ impl<'a> EntityUpdateCtx<'a> {
deferred_destructions: Vec::new(),
}
}
- pub fn deferred_destroy(&mut self, entity_id: u32) {
+ pub fn deferred_destroy(&mut self, entity_id: EntityId) {
self.deferred_destructions.push(entity_id);
}
pub fn to_result(self) -> EntityUpdateResult {
@@ -177,7 +180,7 @@ impl Entity {
}
pub fn new(
- id: u32,
+ id: EntityId,
rb_h: Option<RigidBodyHandle>,
collider_h: Option<ColliderHandle>,
cells: Option<EntityCells>,
diff --git a/src/sim/sim_manager/mod.rs b/src/sim/sim_manager/mod.rs
index 855b398..97f542b 100644
--- a/src/sim/sim_manager/mod.rs
+++ b/src/sim/sim_manager/mod.rs
@@ -9,7 +9,7 @@ use crate::{
sim::{
cell::Cell,
cell_manager::manager::CellManager,
- entity::{Entity, EntityDef},
+ entity::{Entity, EntityDef, EntityId},
particle_manager::ParticleManager,
rb_manager::RbManager,
sim_manager::utils::write_entity_to_world,
@@ -35,7 +35,7 @@ pub struct SimManager {
// entities
// TODO entity manager?
next_entity_id: u32,
- pub entities: FxHashMap<u32, Entity>,
+ pub entities: FxHashMap<EntityId, Entity>,
}
pub struct SimCtx<'a> {
@@ -45,7 +45,7 @@ pub struct SimCtx<'a> {
}
impl SimManager {
- pub fn create_entity(&mut self, def: EntityDef) -> u32 {
+ pub fn create_entity(&mut self, def: EntityDef) -> EntityId {
let (rb_h, collider_h) = if let Some(rb) = def.rb {
let rb_h = self.rb_manager.physics_manager.world.insert_body(rb);
@@ -70,21 +70,15 @@ impl SimManager {
(None, None)
};
- let entity = Entity::new(
- self.next_entity_id,
- rb_h,
- collider_h,
- def.cells,
- def.behaviour,
- );
+ let id = EntityId(self.next_entity_id);
+ let entity = Entity::new(id, rb_h, collider_h, def.cells, def.behaviour);
- let id = self.next_entity_id;
self.entities.insert(id, entity);
self.next_entity_id += 1;
id
}
- pub fn destroy_entity(&mut self, id: u32) {
+ pub fn destroy_entity(&mut self, id: EntityId) {
if let Some(entity) = self.entities.get(&id) {
if let Some(rb_h) = entity.data.rb_h {
self.rb_manager.physics_manager.world.remove_body(rb_h);
@@ -102,8 +96,8 @@ impl SimManager {
fn cell_update(&mut self, config: &Config, delta_time: f32) {
// before we tick, write all the entities into the sim world
// TODO optimize
- let entity_ids: Vec<u32> = self.entities.keys().copied().collect();
- let mut cells_written_by_entity: Vec<(u32, Vec<(u8, u8, i32, i32)>)> = Vec::new();
+ let entity_ids: Vec<EntityId> = self.entities.keys().copied().collect();
+ let mut cells_written_by_entity: Vec<(EntityId, Vec<(u8, u8, i32, i32)>)> = Vec::new();
for entity_id in entity_ids {
let cells_written = write_entity_to_world(self, entity_id, self.cell_manager.seqno);
@@ -112,7 +106,7 @@ impl SimManager {
self.cell_manager.tick(config.use_threading);
- let entity_ids: Vec<u32> = self.entities.keys().cloned().collect();
+ let entity_ids: Vec<EntityId> = self.entities.keys().cloned().collect();
for id in entity_ids {
let entity = self.entities.get_mut(&id);
let mut ctx = SimCtx {
@@ -180,7 +174,7 @@ impl SimManager {
}
fn physics_update(&mut self, delta_time: f32) {
- let entity_ids: Vec<u32> = self.entities.keys().cloned().collect();
+ let entity_ids: Vec<EntityId> = self.entities.keys().cloned().collect();
for id in entity_ids {
let entity = self.entities.get_mut(&id);
let mut ctx = SimCtx {
diff --git a/src/sim/sim_manager/utils.rs b/src/sim/sim_manager/utils.rs
index b668359..13d7891 100644
--- a/src/sim/sim_manager/utils.rs
+++ b/src/sim/sim_manager/utils.rs
@@ -1,10 +1,13 @@
use glam::IVec2;
-use crate::{content::materials::MaterialId, sim::sim_manager::SimManager};
+use crate::{
+ content::materials::MaterialId,
+ sim::{entity::EntityId, sim_manager::SimManager},
+};
pub fn write_entity_to_world(
sim: &mut SimManager,
- entity_id: u32,
+ entity_id: EntityId,
// make sure these cells will be simulated
seqno: u64,
// (entity_x, entity_y, cell_x, cell_y)