From 1af4d32c27494ff62607390ec2552a0d8934c571 Mon Sep 17 00:00:00 2001 From: Giuseppe De Palma Date: Wed, 17 Jan 2024 19:51:46 +0100 Subject: [PATCH] Add new example with a custom node event --- Cargo.toml | 4 ++ examples/custom_node_event.rs | 75 +++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 examples/custom_node_event.rs diff --git a/Cargo.toml b/Cargo.toml index 3f8e638..c774441 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -68,6 +68,10 @@ path = "examples/full.rs" name = "ingame" path = "examples/ingame.rs" +[[example]] +name = "custom_node_event" +path = "examples/custom_node_event.rs" + [lib] name = "bevy_talks" path = "src/lib.rs" diff --git a/examples/custom_node_event.rs b/examples/custom_node_event.rs new file mode 100644 index 0000000..938d830 --- /dev/null +++ b/examples/custom_node_event.rs @@ -0,0 +1,75 @@ +//! Example to show how to add an event emitter component to a node to define custom nodes. +use std::vec; + +use bevy::prelude::*; +use bevy_talks::prelude::*; + +#[derive(Component, Reflect, NodeEventEmitter, Default)] +#[reflect(Component)] +struct DanceStart { + pub moves: Vec, +} + +fn main() { + App::new() + .add_plugins((DefaultPlugins, TalksPlugin)) + .register_node_event::() + .add_systems(Startup, setup_talk) + .add_systems( + Update, + ( + interact, + print_text, + react_to_dancing, + bevy::window::close_on_esc, + ), + ) + .run(); +} + +/// Spawn the dialogue graph using the builder. +fn setup_talk(mut commands: Commands) { + commands.spawn_talk( + Talk::builder() + .say("Oh lord he dancing") + .add_component(DanceStart { + moves: vec![ + "dabs".to_string(), + "whips".to_string(), + "trips and fall".to_string(), + ], + }), + ); + + println!("-----------------------------------------"); + println!("Press space to advance the conversation."); + println!("-----------------------------------------"); +} + +/// Advance the talk when the space key is pressed. +fn interact( + input: Res>, + mut next_action_events: EventWriter, + talks: Query>, +) { + if input.just_pressed(KeyCode::Space) { + next_action_events.send(NextNodeRequest::new(talks.single())); + } +} + +fn print_text(mut text_events: EventReader) { + for txt_ev in text_events.read() { + let mut speaker = "Narrator"; + if !txt_ev.actors.is_empty() { + speaker = &txt_ev.actors[0]; + } + + println!("{speaker}: {}", txt_ev.text); + } +} + +fn react_to_dancing(mut dance_events: EventReader) { + for dance in dance_events.read() { + println!("He: {:?}", dance.moves); + } +}