Skip to content

rmvermeulen/bevy-state-plugin-generator

Repository files navigation

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

bevy this thing
0.15.0 1.2.0
0.16.0 1.3.0
0.17.0 1.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 no longer 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() {
  let config = PluginConfig::default()
        .with_plugin_struct_name("MyStatePlugin")
        .with_root_state_name("RootState")
        .with_states_module_name("inner_states")
        .with_naming_scheme(NamingScheme::Short)
        .with_additional_derives(["PartialOrd", "Ord"]);
  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 }
}
none full merge
PlayerState PlayerState PlayerState
Good PlayerStateGood PlayerGoodState
BadState PlayerStateBadState PlayerBadState
OnFire PlayerStateBadStateOnFire PlayerBadOnFireState
InWater PlayerStateBadStateInWater PlayerBadInWaterState

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •  

Languages