Skip to content

Commit

Permalink
Use slimmer plugin syntax (#3)
Browse files Browse the repository at this point in the history
I've started using this style for https://github.com/janhohenheim/foxtrot and also introduced it to https://github.com/kaosat-dev/Blender_bevy_components_workflow. IMO it makes it much cleaner and easier to split your stuff into sub-plugins :)
  • Loading branch information
janhohenheim authored Jul 4, 2024
1 parent d5e00fb commit c42c8b3
Showing 1 changed file with 17 additions and 23 deletions.
40 changes: 17 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -286,54 +286,48 @@ Bevy [Plugins](https://docs.rs/bevy/latest/bevy/app/trait.Plugin.html) enable gr

By constructing your game out of plugins you make it easier to find, work with, and debug subsystems. It also contextualises the setup and configuration of 3rd party crates to where they belong. For example, setting up the resources, plugins, and systems to utilise a 3rd party terrain library would go in your `TerrainPlugin`. That way, disabling your own terrain plugin will also disable the library you've imported, and any other resources that only it needed.

Note that while internal plugins and binaries use a simple function as a plugin, library authors are expected to expose a struct implementing `Plugin` instead. The reason is that this way, authors can add internal state like configuration to the plugin in the future without breaking changes.

> **Note**
>
> Your mileage may vary with "enabling"/"disabling" plugins in your game. Bevy implements it in engine because it's valuable to disable chunks of the engine. However to achieve this in the game itself is not only more difficult, but the payoff is lower. How often will you realistically want to remove physics or audio from your game?

`src/audio.rs`
```rust
pub(crate) struct AudioPlugin;
impl Plugin for AudioPlugin {
fn build(&self, app: &mut App) {
app.add_plugins(some_audio_library::AudioFXPlugin)
.init_resource::<MyAudioSettings>()
.add_systems(...);
pub(super) fn plugin(app: &mut App) {
app.add_plugins(some_audio_library::AudioFXPlugin)
.init_resource::<MyAudioSettings>()
.add_systems(...);
}
}
```

`src/physics.rs`
```rust
pub(crate) struct PhysicsPlugin;
impl Plugin for PhysicsPlugin {
fn build(&self, app: &mut App) {
app.add_plugins(some_physics_library::BouncyPhysicsPlugin)
.init_resource::<MyPhysicsSettings>()
.add_systems(...);
}
pub(super) fn plugin(app: &mut App) {
app.add_plugins(some_physics_library::BouncyPhysicsPlugin)
.init_resource::<MyPhysicsSettings>()
.add_systems(...);
}
```

`src/game.rs`
```rust
pub(crate) struct GamePlugin;
impl Plugin for GamePlugin {
fn build(&self, app: &mut App) {
app.add_plugins((
DefaultPlugins,
crate::AudioPlugin,
crate::PhysicsPlugin,
));
}
pub(super) fn plugin(app: &mut App) {
app.add_plugins((
DefaultPlugins,
crate::audio::plugin,
crate::physics::plugin,
));
}
```

`src/main.rs`
```rust
fn main() {
bevy::prelude::App::new()
.add_plugins(crate::game::GamePlugin)
.add_plugins(crate::game::plugin)
.run();
}
```
Expand Down

0 comments on commit c42c8b3

Please sign in to comment.