Skip to content

Commit de43d4c

Browse files
authored
Merge pull request #266 from GlennMacdonald/event-refactor
Event refactor
2 parents 35066aa + 7043b81 commit de43d4c

15 files changed

+421
-470
lines changed

crates/bevy_plugin/src/commands/execution.rs

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,29 @@
11
use crate::commands::UntypedYarnCommand;
2-
use crate::dialogue_runner::DialogueExecutionSystemSet;
3-
use crate::events::ExecuteCommandEvent;
2+
use crate::events::ExecuteCommand;
43
use crate::prelude::*;
5-
use bevy::ecs::message::MessageCursor;
64
use bevy::prelude::*;
75

86
pub(crate) fn command_execution_plugin(app: &mut App) {
9-
app.add_systems(
10-
Update,
11-
execute_commands
12-
.after(DialogueExecutionSystemSet)
13-
.in_set(YarnSpinnerSystemSet),
14-
);
7+
app.add_observer(execute_commands);
158
}
169

17-
fn execute_commands(world: &mut World, mut cursor: Local<MessageCursor<ExecuteCommandEvent>>) {
18-
let events = clone_events(world, &mut cursor);
19-
for event in events {
10+
fn execute_commands(event: On<ExecuteCommand>, mut commands: Commands) {
11+
let event = event.clone();
12+
commands.queue(move |world: &mut World| {
2013
let Some(mut command) = clone_command(world, &event) else {
21-
continue;
14+
return;
2215
};
16+
2317
let params = event.command.parameters;
2418
let task_finished_indicator = command.call(params, world);
2519
if !task_finished_indicator.is_finished() {
26-
get_dialogue_runner_mut(world, event.source).add_command_task(task_finished_indicator);
20+
get_dialogue_runner_mut(world, event.entity).add_command_task(task_finished_indicator);
2721
}
28-
}
29-
}
30-
31-
fn clone_events(
32-
world: &World,
33-
cursor: &mut MessageCursor<ExecuteCommandEvent>,
34-
) -> Vec<ExecuteCommandEvent> {
35-
let events = world.resource::<Messages<ExecuteCommandEvent>>();
36-
cursor.read(events).cloned().collect()
22+
});
3723
}
3824

39-
fn clone_command(
40-
world: &mut World,
41-
event: &ExecuteCommandEvent,
42-
) -> Option<Box<dyn UntypedYarnCommand>> {
43-
let dialogue_runner = get_dialogue_runner(world, event.source);
25+
fn clone_command(world: &mut World, event: &ExecuteCommand) -> Option<Box<dyn UntypedYarnCommand>> {
26+
let dialogue_runner = get_dialogue_runner(world, event.entity);
4427
let command_name = event.command.name.as_str();
4528
dialogue_runner
4629
.commands

crates/bevy_plugin/src/dialogue_runner.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
pub use self::events::{
2-
DialogueCompleteEvent, DialogueStartEvent, ExecuteCommandEvent, LineHintsEvent,
3-
NodeCompleteEvent, NodeStartEvent, PresentLineEvent, PresentOptionsEvent,
2+
DialogueCompleted, DialogueStarted, ExecuteCommand, LineHints, NodeCompleted, NodeStarted,
3+
PresentLine, PresentOptions,
44
};
55
pub use self::{
66
builder::DialogueRunnerBuilder,
@@ -18,7 +18,6 @@ use bevy::{
1818
platform::collections::{HashMap, HashSet},
1919
prelude::*,
2020
};
21-
pub(crate) use runtime_interaction::DialogueExecutionSystemSet;
2221
use std::any::TypeId;
2322
use std::fmt::Debug;
2423
use yarnspinner::core::Library;
@@ -33,7 +32,6 @@ mod runtime_interaction;
3332
pub(crate) fn dialogue_plugin(app: &mut App) {
3433
app.add_plugins(runtime_interaction::runtime_interaction_plugin)
3534
.add_plugins(localized_line::localized_line_plugin)
36-
.add_plugins(events::dialogue_runner_events_plugin)
3735
.add_plugins(dialogue_option::dialogue_option_plugin)
3836
.add_plugins(builder::dialogue_runner_builder_plugin)
3937
.add_plugins(inner::inner_dialogue_runner_plugin);
@@ -90,7 +88,7 @@ impl DialogueRunner {
9088
pub fn select_option(&mut self, option: OptionId) -> Result<&mut Self> {
9189
if !self.is_running {
9290
bail!(
93-
"Can't select option {option}: the dialogue is currently not running. Please call `DialogueRunner::continue_in_next_update()` only after receiving a `PresentOptionsEvent`."
91+
"Can't select option {option}: the dialogue is currently not running. Please call `DialogueRunner::continue_in_next_update()` only after receiving a `PresentOptions` event."
9492
)
9593
}
9694
self.inner_mut()
@@ -138,7 +136,7 @@ impl DialogueRunner {
138136
self.inner().0.is_waiting_for_option_selection()
139137
}
140138

141-
/// If set, every line the user selects will emit a [`PresentLineEvent`]. Defaults to `false`.
139+
/// If set, every line the user selects will trigger a [`PresentLine`] event. Defaults to `false`.
142140
pub fn run_selected_options_as_lines(
143141
&mut self,
144142
run_selected_options_as_lines: bool,
@@ -147,13 +145,13 @@ impl DialogueRunner {
147145
self
148146
}
149147

150-
/// If set, every line the user selects will emit a [`PresentLineEvent`]. Defaults to `false`.
148+
/// If set, every line the user selects will trigger a [`PresentLine`] event. Defaults to `false`.
151149
#[must_use]
152150
pub fn runs_selected_options_as_lines(&self) -> bool {
153151
self.run_selected_options_as_lines
154152
}
155153

156-
/// Stops the execution of the dialogue. Any pending dialogue events will still be sent in the next update, including a [`DialogueCompleteEvent`].
154+
/// Stops the execution of the dialogue. Any pending dialogue events will still be sent in the next update, including a [`DialogueCompleted`] event.
157155
/// After this, [`DialogueRunner::start_node`] must be called before the dialogue can be advanced again.
158156
pub fn stop(&mut self) -> &mut Self {
159157
self.is_running = false;

crates/bevy_plugin/src/dialogue_runner/dialogue_option.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use bevy::prelude::*;
55
pub(crate) fn dialogue_option_plugin(_app: &mut App) {}
66

77
/// An option that can be presented to the user during a dialogue.
8-
/// Given to you by a [`PresentOptionsEvent`](crate::events::PresentOptionsEvent).
8+
/// Given to you by a [`PresentOptions`](crate::events::PresentOptions) event.
99
#[derive(Debug, Clone, PartialEq)]
1010
pub struct DialogueOption {
1111
/// The [`LocalizedLine`] that should be presented to the user for this option.

crates/bevy_plugin/src/dialogue_runner/events.rs

Lines changed: 24 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,27 @@ use crate::UnderlyingYarnCommand;
22
use crate::prelude::*;
33
use bevy::prelude::*;
44

5-
pub(crate) fn dialogue_runner_events_plugin(app: &mut App) {
6-
app.add_message::<PresentLineEvent>()
7-
.add_message::<PresentOptionsEvent>()
8-
.add_message::<ExecuteCommandEvent>()
9-
.add_message::<NodeCompleteEvent>()
10-
.add_message::<NodeStartEvent>()
11-
.add_message::<LineHintsEvent>()
12-
.add_message::<DialogueCompleteEvent>()
13-
.add_message::<DialogueStartEvent>();
14-
}
15-
165
/// An event that is fired after a dialogue advances and wishes to present a line to the user.
176
/// A dialogue view should listen for this event and draw it to the screen.
187
/// Handling this event is **mandatory** for dialogue views.
19-
#[derive(Debug, Clone, PartialEq, Message)]
20-
pub struct PresentLineEvent {
8+
#[derive(Debug, Clone, PartialEq, EntityEvent)]
9+
pub struct PresentLine {
2110
/// The line to present to the user.
2211
pub line: LocalizedLine,
2312
/// The [`DialogueRunner`] that is presenting this line.
24-
pub source: Entity,
13+
pub entity: Entity,
2514
}
2615

2716
/// An event that is fired after a dialogue advances and wishes to present a set of options to the user.
2817
/// A dialogue view should listen for this event and draw it to the screen.
2918
/// You need to handle this event by calling [`DialogueRunner::select_option`] with the ID found in the provided [`DialogueOption`]s.
3019
/// Handling this event is **mandatory** for dialogue views.
31-
#[derive(Debug, Clone, PartialEq, Message)]
32-
pub struct PresentOptionsEvent {
20+
#[derive(Debug, Clone, PartialEq, EntityEvent)]
21+
pub struct PresentOptions {
3322
/// The options to present to the user.
3423
pub options: Vec<DialogueOption>,
3524
/// The [`DialogueRunner`] that is presenting these options.
36-
pub source: Entity,
25+
pub entity: Entity,
3726
}
3827

3928
/// An event that is fired after a dialogue advances and wishes to execute a command.
@@ -42,56 +31,56 @@ pub struct PresentOptionsEvent {
4231
/// However, a command is allowed much more freedom in its syntax than one might think, and as such, not all commands are registerable.
4332
/// Thus, you can listen for this event and handle it yourself if you wish to build your own command syntax for e.g. a DSL.
4433
/// Handling this event is optional for dialogue views.
45-
#[derive(Debug, Clone, PartialEq, Message)]
46-
pub struct ExecuteCommandEvent {
34+
#[derive(Debug, Clone, PartialEq, EntityEvent)]
35+
pub struct ExecuteCommand {
4736
/// The command to execute.
4837
pub command: UnderlyingYarnCommand,
4938
/// The [`DialogueRunner`] that is executing this command.
50-
pub source: Entity,
39+
pub entity: Entity,
5140
}
5241

5342
/// An event that is fired after a node has been completed, i.e. all of its lines, commands, options, etc. have been exhausted.
5443
/// Handling this event is **optional** for dialogue views.
55-
#[derive(Debug, Clone, PartialEq, Message)]
56-
pub struct NodeCompleteEvent {
44+
#[derive(Debug, Clone, PartialEq, EntityEvent)]
45+
pub struct NodeCompleted {
5746
/// The name of the node that has been completed.
5847
pub node_name: String,
5948
/// The [`DialogueRunner`] that has completed this node.
60-
pub source: Entity,
49+
pub entity: Entity,
6150
}
6251

6352
/// An event that is fired after a node has been started, i.e. the first line, command, option, etc. has been executed.
6453
/// Handling this event is **optional** for dialogue views
65-
#[derive(Debug, Clone, PartialEq, Message)]
66-
pub struct NodeStartEvent {
54+
#[derive(Debug, Clone, PartialEq, EntityEvent)]
55+
pub struct NodeStarted {
6756
/// The name of the node that has been started.
6857
pub node_name: String,
6958
/// The [`DialogueRunner`] that has started this node.
70-
pub source: Entity,
59+
pub entity: Entity,
7160
}
7261

7362
/// An event that is fired when a new node has been started. Contains the IDs of all lines in the node as a general hint
7463
/// for asset providing systems to pre-load the lines. The lines are not guaranteed to be presented in the order of the IDs or at all.
7564
/// Handling this event is **optional** for dialogue views.
76-
#[derive(Debug, Clone, PartialEq, Message)]
77-
pub struct LineHintsEvent {
65+
#[derive(Debug, Clone, PartialEq, EntityEvent)]
66+
pub struct LineHints {
7867
/// The IDs of all lines in the node.
7968
pub line_ids: Vec<LineId>,
8069
/// The [`DialogueRunner`] that has started this node.
81-
pub source: Entity,
70+
pub entity: Entity,
8271
}
8372

8473
/// An event that is fired when a dialogue has been started via [`DialogueRunner::start_node`]/
8574
/// Handling this event is **optional** for dialogue views.
86-
#[derive(Debug, Clone, PartialEq, Message)]
87-
pub struct DialogueStartEvent {
75+
#[derive(Debug, Clone, PartialEq, EntityEvent)]
76+
pub struct DialogueStarted {
8877
/// The [`DialogueRunner`] that has started this dialogue.
89-
pub source: Entity,
78+
pub entity: Entity,
9079
}
9180

9281
/// An event that is fired when a dialogue has been completed or stopped via [`DialogueRunner::stop`].
93-
#[derive(Debug, Clone, PartialEq, Message)]
94-
pub struct DialogueCompleteEvent {
82+
#[derive(Debug, Clone, PartialEq, EntityEvent)]
83+
pub struct DialogueCompleted {
9584
/// The [`DialogueRunner`] that has completed this dialogue.
96-
pub source: Entity,
85+
pub entity: Entity,
9786
}

0 commit comments

Comments
 (0)