From d6664b9b5a9a3aab500b547e4d7448a8bc4ad416 Mon Sep 17 00:00:00 2001 From: Kai Stevenson Date: Sun, 23 Aug 2026 01:07:07 -0700 Subject: allow entity damage --- src/sim/entity/mod.rs | 56 ++++++++++++++++++++++++++------------------------- 1 file changed, 29 insertions(+), 27 deletions(-) (limited to 'src/sim/entity') 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, @@ -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 { + 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, -- cgit v1.3.1