From a4fe37add1ee6e7f0db3f811d285c350745735fc Mon Sep 17 00:00:00 2001 From: Carter Anderson Date: Sun, 3 May 2020 01:30:10 -0700 Subject: [PATCH] add plugin example --- Cargo.toml | 4 ++ .../example_plugin/src/lib.rs | 4 +- examples/app/plugin.rs | 49 +++++++++++++++++++ 3 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 examples/app/plugin.rs diff --git a/Cargo.toml b/Cargo.toml index 1601b07866..ceeb9c2860 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -101,6 +101,10 @@ path = "examples/app/empty.rs" name = "headless" path = "examples/app/headless.rs" +[[example]] +name = "plugin" +path = "examples/app/plugin.rs" + [[example]] name = "event" path = "examples/ecs/event.rs" diff --git a/examples/app/dynamic_plugin_loading/example_plugin/src/lib.rs b/examples/app/dynamic_plugin_loading/example_plugin/src/lib.rs index 5bd2b91613..ebaeaa6362 100644 --- a/examples/app/dynamic_plugin_loading/example_plugin/src/lib.rs +++ b/examples/app/dynamic_plugin_loading/example_plugin/src/lib.rs @@ -4,8 +4,8 @@ use bevy::prelude::*; pub struct ExamplePlugin; impl AppPlugin for ExamplePlugin { - fn build(&self, app_builder: &mut AppBuilder) { - app_builder.add_startup_system(setup); + fn build(&self, app: &mut AppBuilder) { + app.add_startup_system(setup); } } diff --git a/examples/app/plugin.rs b/examples/app/plugin.rs new file mode 100644 index 0000000000..445c4c0bd1 --- /dev/null +++ b/examples/app/plugin.rs @@ -0,0 +1,49 @@ +use bevy::prelude::*; +use std::time::Duration; + +// Plugins are the foundation of Bevy. They are scoped sets of components, resources, and systems +// that provide a specific piece of functionality (generally the smaller the scope, the better). +fn main() { + App::build() + .add_default_plugins() + // plugins are registered as part of the "app building" process + .add_plugin(PrintMessagePlugin { + wait_duration: Duration::from_secs(1), + message: "This is an example plugin".to_string(), + }) + .run(); +} + +#[derive(Default)] +pub struct PrintMessagePlugin { + // Put your plugin configuration here + wait_duration: Duration, + message: String, +} + +impl AppPlugin for PrintMessagePlugin { + // this is where we set up our plugin + fn build(&self, app: &mut AppBuilder) { + let state = PrintMessageState { + message: self.message.clone(), + elapsed_time: 0.0, + duration: self.wait_duration, + }; + app.add_resource(state) + .add_system(print_message_system.system()); + } +} + +struct PrintMessageState { + message: String, + duration: Duration, + elapsed_time: f32, +} + +fn print_message_system(time: Resource