summaryrefslogtreecommitdiff
path: root/src/sim
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 /src/sim
parent80824a8b69b70e6e40577e4f0236c28c4ad5c15b (diff)
entityID newtype
Diffstat (limited to 'src/sim')
-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
5 files changed, 24 insertions, 24 deletions
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)