From 9b789f83e054ec16cbd104a355c8f3136f476baf Mon Sep 17 00:00:00 2001 From: Jonni Liljamo Date: Tue, 10 Jun 2025 21:24:55 +0300 Subject: [PATCH] feat: allow setting the die weight/mass --- crates/bevy_dice/src/lib.rs | 8 ++++++++ crates/bevy_dice_demo/src/main.rs | 9 +++++++++ 2 files changed, 17 insertions(+) diff --git a/crates/bevy_dice/src/lib.rs b/crates/bevy_dice/src/lib.rs index 303f625..ef52148 100644 --- a/crates/bevy_dice/src/lib.rs +++ b/crates/bevy_dice/src/lib.rs @@ -55,6 +55,8 @@ pub struct DicePluginConfig { pub override_face_positions: HashMap>, /// Scale overrides, used to size the dice. pub override_scales: HashMap, + /// Weight overrides, used to set die weights (mass). + pub override_weights: HashMap, } #[derive(Reflect, Debug, Clone, Copy, PartialEq, Eq, Hash)] @@ -154,6 +156,11 @@ fn throw_die( None => Vec3::splat(1.0), }; + let mass = match r_config.override_weights.get(&trigger.variant) { + Some(mass) => Mass(*mass), + None => Mass(1.0), + }; + let mut die = commands.spawn(( Visibility::Inherited, // Place the mesh in a child to allow correcting the rotation. @@ -175,6 +182,7 @@ fn throw_die( RigidBody::Dynamic, Collider::convex_hull_from_mesh(mesh).unwrap(), angular_velocity, + mass, Die::new(trigger.id), )); diff --git a/crates/bevy_dice_demo/src/main.rs b/crates/bevy_dice_demo/src/main.rs index d5f882c..ce821e0 100644 --- a/crates/bevy_dice_demo/src/main.rs +++ b/crates/bevy_dice_demo/src/main.rs @@ -165,6 +165,15 @@ fn setup( (DieVariant::D12, 0.8), (DieVariant::D20, 0.8), ]), + override_weights: HashMap::from([ + (DieVariant::D4, 10.0), + (DieVariant::D6, 10.0), + (DieVariant::D8, 10.0), + (DieVariant::D10, 10.0), + (DieVariant::D10P, 10.0), + (DieVariant::D12, 10.0), + (DieVariant::D20, 10.0), + ]), }); } -- 2.44.1