summaryrefslogtreecommitdiff
path: root/src/sim/entity
diff options
context:
space:
mode:
Diffstat (limited to 'src/sim/entity')
-rw-r--r--src/sim/entity/mod.rs23
1 files changed, 11 insertions, 12 deletions
diff --git a/src/sim/entity/mod.rs b/src/sim/entity/mod.rs
index 4a8eda4..59479a2 100644
--- a/src/sim/entity/mod.rs
+++ b/src/sim/entity/mod.rs
@@ -2,7 +2,6 @@ use glam::{IVec2, Vec2};
use rapier2d::{
dynamics::{RigidBody, RigidBodyBuilder, RigidBodyHandle},
geometry::{Collider, ColliderHandle},
- math::Pose2,
};
use crate::{
@@ -14,13 +13,6 @@ use crate::{
sprite_loader::load_sprite_to_cells,
};
-fn mass_from_cells(cells: &[Cell]) -> f32 {
- cells
- .iter()
- .fold(0.0, |acc, cur| acc + cur.material.def().density as f32)
- * MASS_SCALING
-}
-
pub struct EntityCells {
pub size: IVec2,
pub cells: Vec<Cell>,
@@ -35,6 +27,13 @@ impl EntityCells {
pub fn set_cell_at_local_position(&mut self, pos: IVec2, cell: Cell) {
self.cells[pos.x as usize + pos.y as usize * self.size.x as usize] = cell;
}
+
+ pub fn mass(&self) -> f32 {
+ self.cells
+ .iter()
+ .fold(0.0, |acc, cur| acc + cur.material.def().density as f32)
+ * MASS_SCALING
+ }
}
impl Marchable for EntityCells {
@@ -76,7 +75,7 @@ impl EntityDef {
behaviour: Option<Box<dyn EntityBehaviour>>,
) -> Self {
let collider = RbManager::convex_hull_collider_from_marchable(&cells, Some(10))
- .mass(mass_from_cells(&cells.cells))
+ .mass(cells.mass())
.build();
EntityDef {
@@ -95,7 +94,7 @@ impl EntityDef {
let rb = RigidBodyBuilder::dynamic().translation(position).build();
let collider = RbManager::convex_hull_collider_from_marchable(&cells, Some(10))
- .mass(mass_from_cells(&cells.cells))
+ .mass(cells.mass())
.build();
EntityDef {
@@ -135,7 +134,7 @@ impl EntityDef {
.build();
let collider = RbManager::convex_hull_collider_from_marchable(&cells, Some(10))
- .mass(mass_from_cells(&cells.cells))
+ .mass(cells.mass())
.build();
EntityDef {
@@ -259,7 +258,7 @@ impl Entity {
pub fn compute_collider(&self) -> Option<Collider> {
self.data.cells.as_ref().map(|cells| {
RbManager::convex_hull_collider_from_marchable(cells, Some(10))
- .mass(mass_from_cells(&cells.cells))
+ .mass(cells.mass())
.build()
})
}