Skip to content

Commit

Permalink
Add new example with a custom node event
Browse files Browse the repository at this point in the history
  • Loading branch information
giusdp committed Jan 17, 2024
1 parent 7af8c9d commit 1af4d32
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
75 changes: 75 additions & 0 deletions examples/custom_node_event.rs
Original file line number Diff line number Diff line change
@@ -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<String>,
}

fn main() {
App::new()
.add_plugins((DefaultPlugins, TalksPlugin))
.register_node_event::<DanceStart, DanceStartEvent>()
.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<Input<KeyCode>>,
mut next_action_events: EventWriter<NextNodeRequest>,
talks: Query<Entity, With<Talk>>,
) {
if input.just_pressed(KeyCode::Space) {
next_action_events.send(NextNodeRequest::new(talks.single()));
}
}

fn print_text(mut text_events: EventReader<TextNodeEvent>) {
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<DanceStartEvent>) {
for dance in dance_events.read() {
println!("He: {:?}", dance.moves);
}
}

0 comments on commit 1af4d32

Please sign in to comment.