diff options
| author | Kai Stevenson <kai@kaistevenson.com> | 2026-08-23 01:07:07 -0700 |
|---|---|---|
| committer | Kai Stevenson <kai@kaistevenson.com> | 2026-08-23 01:07:07 -0700 |
| commit | d6664b9b5a9a3aab500b547e4d7448a8bc4ad416 (patch) | |
| tree | 688d30258e1e3b06d038ab40ea9c3d3a561c6426 /src/sim/entity/mod.rs | |
| parent | 260d5b0056f1a7177bcc98316592811577a0a565 (diff) | |
allow entity damage
Diffstat (limited to 'src/sim/entity/mod.rs')
| -rw-r--r-- | src/sim/entity/mod.rs | 56 |
1 files changed, 29 insertions, 27 deletions
diff --git a/src/sim/entity/mod.rs b/src/sim/entity/mod.rs index d8a03e0..3a3fb1d 100644 --- a/src/sim/entity/mod.rs +++ b/src/sim/entity/mod.rs @@ -13,6 +13,13 @@ use crate::{ }, }; +fn mass_from_cells(cells: &[Cell]) -> f32 { + cells + .iter() + .fold(0, |acc, cur| acc + cur.material.def().density) as f32 + * MASS_SCALING +} + pub struct EntityCells { pub size: IVec2, pub cells: Vec<Cell>, @@ -43,13 +50,7 @@ impl Marchable for EntityCells { } pub trait EntityBehaviour { - fn update( - &mut self, - _update_ctx: &mut EntityUpdateCtx, - _ctx: &mut SimCtx, - _delta_time: f32, - ) { - } + fn update(&mut self, _update_ctx: &mut EntityUpdateCtx, _ctx: &mut SimCtx, _delta_time: f32) {} fn physics_update( &mut self, _update_ctx: &mut EntityUpdateCtx, @@ -76,14 +77,8 @@ impl EntityDef { .translation(position / CELLS_TO_METRES) .build(); - let mass = cells - .cells - .iter() - .fold(0, |acc, cur| acc + cur.material.def().density) as f32 - * MASS_SCALING; - let collider = RbManager::convex_hull_collider_from_marchable(&cells, None) - .mass(mass) + .mass(mass_from_cells(&cells.cells)) .build(); EntityDef { @@ -130,19 +125,18 @@ impl<'a> EntityUpdateCtx<'a> { impl EntityData { pub fn transform(&self, ctx: &SimCtx) -> Option<(Vec2, (f32, f32))> { - self.rb_h - .and_then(|rb_h| { - ctx.rb_manager - .physics_manager - .rigid_body_set - .get(rb_h) - .map(|rb| { - ( - rb.translation() * CELLS_TO_METRES, - (rb.rotation().cos(), rb.rotation().sin()), - ) - }) - }) + self.rb_h.and_then(|rb_h| { + ctx.rb_manager + .physics_manager + .rigid_body_set + .get(rb_h) + .map(|rb| { + ( + rb.translation() * CELLS_TO_METRES, + (rb.rotation().cos(), rb.rotation().sin()), + ) + }) + }) } } @@ -174,6 +168,14 @@ impl Entity { None } + pub fn compute_collider(&self) -> Option<Collider> { + self.data.cells.as_ref().map(|cells| { + RbManager::convex_hull_collider_from_marchable(cells, None) + .mass(mass_from_cells(&cells.cells)) + .build() + }) + } + pub fn new( id: u32, rb_h: Option<RigidBodyHandle>, |
