Crate bevy_state_plugin_generator

Crate bevy_state_plugin_generator 

Source
Expand description

§State-Plugin generator

Generates a bunch of hierarchical states (structs and enums, using State and SubState) from a simple DSL and sets up their relationships in a Plugin.

§Probably do not use

I made this without checking if and/or how it would be a good idea.

§versions

bevythis thing
0.15.01.2.0
0.16.01.3.0
0.17.01.4.0

§usage

§single-file mode

Create a states.rs in src/ and add the following comment at the top:

// bspg:
// Loading
// Ready { Menu Game }
// Exiting

Set up your build.rs like this:

use bevy_state_plugin_generator::prelude::*;
fn main() {
  /// The [Default::default] configuration is:
  let config = PluginConfig {
    plugin_name: PluginName::new_struct("GeneratedStatesPlugin"),
    root_state_name: Some(Cow::from("GameState")),
    states_module_name: Cow::from("states"),
    naming_scheme: NamingScheme::Full,
    additional_derives: vec![],
  };
  update_template("src/states.rs", config)
    .expect("Failed to update template!");
}

§separate-file mode

Create a states.txt in src/:

// the root is implicit
Loading // comments go until the end of the line
Ready { Menu Game }                     // enum state
Exiting                                 // singleton

Set up your build.rs like this:

use bevy_state_plugin_generator::prelude::*;
fn main() {
  /// The [Default::default] configuration is:
  let config = PluginConfig {
    plugin_name: PluginName::new_struct("GeneratedStatesPlugin"),
    root_state_name: Some(Cow::from("GameState")),
    states_module_name: Cow::from("states"),
    naming_scheme: NamingScheme::Full,
    additional_derives: vec![],
  };
  generate_plugin("src/states.txt", "src/generated_states.rs", config)
    .expect("Failed to generate plugin!");
}

And it will generate something like the following:

use bevy::prelude::{App, AppExtStates};
pub mod states {
    use bevy::prelude::{States, SubStates, StateSet};
    #[derive(States, Hash, Default, Debug, Clone, PartialEq, Eq)]
    pub enum GameState { #[default] Loading, Ready, Exiting }
    #[derive(SubStates, Hash, Default, Debug, Clone, PartialEq, Eq)]
    #[source(GameState = GameState::Loading)]
    pub struct GameStateLoading;
    #[derive(SubStates, Hash, Default, Debug, Clone, PartialEq, Eq)]
    #[source(GameStateLoading = GameStateLoading)]
    pub struct GameStateLoadingConfigs;
    #[derive(SubStates, Hash, Default, Debug, Clone, PartialEq, Eq)]
    #[source(GameStateLoading = GameStateLoading)]
    pub struct GameStateLoadingAssets;
    #[derive(SubStates, Hash, Default, Debug, Clone, PartialEq, Eq)]
    #[source(GameState = GameState::Ready)]
    pub enum GameStateReady { #[default] Menu, Game }
}
pub struct GeneratedStatesPlugin;
impl bevy::app::Plugin for GeneratedStatesPlugin {
    fn build(&self, app: &mut App) {
        app.init_state::<states::GameState>();
    }
}

§naming

Consider the following sample:

PlayerState {
    Good
    BadState { OnFire InWater }
}
nonefullmerge
PlayerStatePlayerStatePlayerState
GoodPlayerStateGoodPlayerGoodState
BadStatePlayerStateBadStatePlayerBadState
OnFirePlayerStateBadStateOnFirePlayerBadOnFireState
InWaterPlayerStateBadStateInWaterPlayerBadInWaterState

Modules§

prelude
The types and functions required to use this library