Skip to main content

Crate sbepistats

Crate sbepistats 

Source
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.
StatsSystems
SystemSet containing all stat systems.

Enums§

DataTypeOp
Representations of possible stat modifier operations.
StatSystems
SystemSet for each stat’s system.

Traits§

Add
Stat datatypes that can do addition, eg f32 and std::time::Duration.
AppExt
Extension trait for App for stat registration methods.
Mul
Stat datatypes that can do multiplication, eg f32.
StatModifierAdd
A modifier to a Stat that adds to it.
StatModifierMul
A modifier to a Stat that multiplies to it.
StatType
Marker trait for defining a unique stat.

Derive Macros§

StatType
Derive macro for StatType.