Expand description
A Minecraft-inspired stat system for Bevy.
To get started, derive StatType on a struct and register it to your app with AppExt::add_stat_type.
Then, impl StatModifierAdd or StatModifierMul on a component and
register it to your app with AppExt::add_stat_modifier_add or AppExt::add_stat_modifier_mul.
To use the stats, add a Stat component to an entity, and any stat modifiers added to it will be reflected in Stat::total.
Stat datatypes are flexible, hence the separation of Add and Mul. If you need multiplication for a datatype that either
can’t multiply or does it in an unwanted way, consider using a wrapper type. At minimum, a stat datatype requires Add.
If you need something more comprehensive than a simple member of your modifier, you can add a system directly to StatSystems::Op
and register the type of operation with AppExt::configure_stat_type_add or AppExt::configure_stat_type_mul.
If you’re using bevy_auto_plugin, build hooks such as [StatTypeHook], [StatModifierAddHook], and [StatModifierMulHook] are available.
§Example
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_stat_type::<Speed>()
.add_stat_modifier_add::<Speed, SpeedBoost>()
.add_systems(Startup, |mut commands: Commands| {
commands.spawn((Stat::<Speed>::new(1.0), SpeedBoost));
});
}
#[derive(StatType)]
struct Speed;
#[derive(Component)]
struct SpeedBoost;
impl StatModifierAdd<Speed> for SpeedBoost {
fn add(&self) -> f32 {
0.2
}
}Structs§
- Stat
- Component containing the
StatType’s values. - Stats
Systems SystemSetcontaining all stat systems.
Enums§
- Data
Type Op - Representations of possible stat modifier operations.
- Stat
Systems SystemSetfor each stat’s system.
Traits§
- Add
- Stat datatypes that can do addition, eg
f32andstd::time::Duration. - AppExt
- Extension trait for
Appfor stat registration methods. - Mul
- Stat datatypes that can do multiplication, eg
f32. - Stat
Modifier Add - A modifier to a
Statthat adds to it. - Stat
Modifier Mul - A modifier to a
Statthat multiplies to it. - Stat
Type - Marker trait for defining a unique stat.