From 020a67107b0fb78f053fa9f45205c093dfbaaeb3 Mon Sep 17 00:00:00 2001 From: Kai Stevenson Date: Sat, 22 Aug 2026 17:52:57 -0700 Subject: entity behavour, grenades --- src/sim/entity/mod.rs | 85 +++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 65 insertions(+), 20 deletions(-) (limited to 'src/sim/entity/mod.rs') diff --git a/src/sim/entity/mod.rs b/src/sim/entity/mod.rs index b648f1d..c6c350e 100644 --- a/src/sim/entity/mod.rs +++ b/src/sim/entity/mod.rs @@ -10,7 +10,7 @@ use crate::{ cell::{cell::Cell, materials::MaterialId}, lib::marching_squares::Marchable, rb_manager::RbManager, - sim_manager::SimManager, + sim_manager::SimCtx, }, }; @@ -46,12 +46,17 @@ impl Marchable for EntityCells { } pub trait EntityBehaviour { - fn update(&mut self, sim: &mut SimManager, delta_time: f32) -> (); - fn physics_update(&mut self, sim: &mut SimManager, 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, + ctx: &mut SimCtx, + delta_time: f32, + ) -> (); } pub struct EntityDef { - pub position: Vec2, pub rb: Option, pub collider: Option, pub cells: Option, @@ -71,7 +76,6 @@ impl EntityDef { let collider = RbManager::convex_hull_collider_from_marchable(&cells, None).build(); EntityDef { - position, rb: Some(rb), collider: Some(collider), cells: Some(cells), @@ -80,19 +84,44 @@ impl EntityDef { } } -pub struct Entity { +pub struct EntityData { pub id: u32, pub rb_h: Option, pub collider_h: Option, pub cells: Option, - behaviour: Option>, } -impl Entity { - pub fn transform(&self, sim: &SimManager) -> Option<(Vec2, (f32, f32))> { +pub struct EntityUpdateResult { + pub deferred_destructions: Vec, +} + +pub struct EntityUpdateCtx<'a> { + pub entity_data: &'a mut EntityData, + deferred_destructions: Vec, +} + +impl<'a> EntityUpdateCtx<'a> { + pub fn from_entity_data(entity_data: &'a mut EntityData) -> Self { + EntityUpdateCtx { + entity_data, + deferred_destructions: Vec::new(), + } + } + pub fn deferred_destroy(&mut self, entity_id: u32) { + self.deferred_destructions.push(entity_id); + } + pub fn to_result(self) -> EntityUpdateResult { + EntityUpdateResult { + deferred_destructions: self.deferred_destructions, + } + } +} + +impl EntityData { + pub fn transform(&self, ctx: &SimCtx) -> Option<(Vec2, (f32, f32))> { self.rb_h .map(|rb_h| { - sim.rb_manager + ctx.rb_manager .physics_manager .rigid_body_set .get(rb_h) @@ -105,20 +134,34 @@ impl Entity { }) .flatten() } +} - // TODO use drop? - pub fn destroy(&mut self, sim: &mut SimManager) -> () {} +pub struct Entity { + pub data: EntityData, + behaviour: Option>, +} - pub fn update(&mut self, sim: &mut SimManager, delta_time: f32) -> () { +impl Entity { + pub fn update(&mut self, ctx: &mut SimCtx, delta_time: f32) -> Option { if let Some(behaviour) = &mut self.behaviour { - behaviour.update(sim, delta_time); + let mut ectx = EntityUpdateCtx::from_entity_data(&mut self.data); + behaviour.update(&mut ectx, ctx, delta_time); + return Some(ectx.to_result()); } + None } - pub fn physics_update(&mut self, sim: &mut SimManager, delta_time: f32) -> () { + pub fn physics_update( + &mut self, + ctx: &mut SimCtx, + delta_time: f32, + ) -> Option { if let Some(behaviour) = &mut self.behaviour { - behaviour.physics_update(sim, delta_time); + let mut ectx = EntityUpdateCtx::from_entity_data(&mut self.data); + behaviour.physics_update(&mut ectx, ctx, delta_time); + return Some(ectx.to_result()); } + None } pub fn new( @@ -129,10 +172,12 @@ impl Entity { behaviour: Option>, ) -> Self { Entity { - id, - rb_h, - collider_h, - cells, + data: EntityData { + id, + rb_h, + collider_h, + cells, + }, behaviour, } } -- cgit v1.3.1