| Crates.io | bevy_gauge |
| lib.rs | bevy_gauge |
| version | 0.1.1 |
| created_at | 2025-03-21 17:45:36.859484+00 |
| updated_at | 2025-07-17 20:21:41.579942+00 |
| description | bevy_gauge - a flexible stats system for Bevy |
| homepage | https://github.com/DEMIURGE-studio/bevy_gauge |
| repository | https://github.com/DEMIURGE-studio/bevy_gauge |
| max_upload_size | |
| id | 1600912 |
| size | 375,539 |
bevy_gauge is a flexible stat and modifier system for the Bevy game engine, designed to manage complex character statistics, buffs, debuffs, and equipment effects with ease.
base * (1 + increased) * more).StatsProxy component provides change detection without ownership conflicts.Cargo.toml[dependencies]
bevy_gauge = "0.1" # Replace with the latest version
use bevy::prelude::*;
use bevy_gauge::prelude::*;
use bevy_gauge::stat_types::ModType;
use bevy_gauge_macros::define_tags;
// Define your game's tag system using the macro
define_tags!{
DamageTags,
damage_type {
elemental { fire, cold, lightning },
physical,
chaos,
},
weapon_type {
melee { sword, axe },
ranged { bow, wand },
},
}
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_plugins(bevy_gauge::plugin) // Essential plugin
.add_systems(Startup, (setup_game_config, spawn_player).chain())
.add_systems(Update, (apply_buff_system, display_stats_system))
.run();
}
// Define your game's stat configuration
fn setup_game_config() {
// Core attributes (modifiable)
Konfig::register_stat_type("Strength", "Modifiable");
Konfig::register_stat_type("Dexterity", "Modifiable");
Konfig::register_stat_type("Intelligence", "Modifiable");
// Complex stats (calculated from expressions)
Konfig::register_stat_type("Life", "Complex");
Konfig::register_stat_type("Mana", "Complex");
Konfig::register_stat_type("Accuracy", "Complex");
Konfig::register_stat_type("Evasion", "Complex");
// Tagged stats (filterable by tags)
Konfig::register_stat_type("Damage", "Tagged");
// Set default formula for complex stats (PoE-style)
Konfig::set_total_expression_default("added * (1.0 + increased) * more");
// Configure "more" modifiers as multiplicative
Konfig::register_relationship_type("more", ModType::Mul);
// Register tag resolver for damage calculations
Konfig::register_tag_set("Damage", Box::new(DamageTags));
}
#[derive(Component)]
struct Player;
fn spawn_player(mut commands: Commands) {
commands.spawn((
Player,
stats! {
// Core Attributes
"Strength" => 25.0,
"Dexterity" => 18.0,
"Intelligence" => 33.0,
// Complex Stats (PoE-style formulas)
"Life.added" => [100.0, "Strength / 2.0"], // Base + strength bonus
"Life.more" => 0.4, // 40% more life multiplier
"Mana.added" => [50.0, "Intelligence / 5.0"], // Base + intelligence bonus
"Accuracy.added" => 50.0,
"Accuracy.increased" => "Dexterity / 2.0", // Dexterity increases accuracy
// Tagged Stats (string-based tags)
"Damage.added.{MELEE|PHYSICAL}" => 50.0,
"Damage.increased.{MELEE|PHYSICAL}" => "Strength / 2.0 / 100.0",
// Percentage bonuses
"Evasion.increased" => "Dexterity / 2.0 / 100.0",
"EnergyShield.increased" => "Intelligence / 5.0 / 100.0",
},
));
}
fn apply_buff_system(
mut stats_mutator: StatsMutator,
player_query: Query<Entity, Added<Stats>>,
) {
if let Ok(player_entity) = player_query.single() {
// Add modifiers
stats_mutator.add_modifier(player_entity, "Strength", 5.0);
stats_mutator.add_modifier(player_entity, "Damage.added.{SWORD|PHYSICAL}", 15.0);
stats_mutator.add_modifier(player_entity, "Damage.increased.{FIRE|MELEE}", 0.2);
stats_mutator.add_modifier(player_entity, "Damage.more.{PHYSICAL}", 0.5);
}
}
fn display_stats_system(
player_query: Query<&Stats, With<Player>>,
) {
if let Ok(stats) = player_query.single() {
// Get stats with string-based tags
let strength = stats.get("Strength");
let life = stats.get("Life");
let axe_physical_damage = stats.get("Damage.{AXE|PHYSICAL}");
let fire_sword_damage = stats.get("Damage.{FIRE|SWORD}");
// Cross-entity dependencies
let weapon_damage = stats.get("Damage.added@weapon");
println!("Str: {:.1}, Life: {:.1}", strength, life);
println!("Axe Physical: {:.2}, Fire Sword: {:.2}", axe_physical_damage, fire_sword_damage);
}
}
Create Bevy components whose fields are automatically updated from stats:
stat_component!(
#[derive(Debug)]
pub struct Health {
max: f32 <- "Life", // Derived from Life stat
current: f32 -> $, // Writes Health.current value to "$[Health.current]" stat
}
);
// Write-back support for mutable state
impl WriteBack for Health {
fn write_back(&self, target_entity: Entity, stats_mutator: &mut StatsMutator) {
let _ = stats_mutator.set(target_entity, "$[Health.current]", self.current);
}
}
stat_component!(
#[derive(Clone, Debug)]
pub struct PlayerStats {
damage: f32 <- "Damage.{PHYSICAL}",
accuracy: f32 <- "Accuracy",
life: f32 <- "Life",
// Non-stat fields maintain their values independently
pub name: String,
pub level: u32,
// Optional stats (0.0 becomes None)
bonus: Option<f32> <- "BonusStat",
}
);
Components update automatically when their underlying stats change.
bevy_gauge includes a StatsProxy component that automatically tracks when an entity's Stats have been modified. This provides efficient change detection without the ownership conflicts that would occur if you tried to use Changed<Stats> directly in systems that also use StatsMutator. This is mostly used internally, but it is there if you need it.
For more examples, see the examples/ directory in the repository.
Contributions are welcome! Feel free to open an issue or submit a pull request.
| bevy_gauge_macros | bevy_gauge | bevy |
|---|---|---|
| 0.1.1 | 0.1.1 | 0.16 |
Note: This crate is in active development. APIs may change between versions.
bevy_gauge is dual-licensed under either
stats! macro but not in stats.get() calls)