Skip to content

Commit

Permalink
Fix clippy warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
giusdp committed Jan 15, 2024
1 parent f9cc510 commit a83881b
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 10 deletions.
5 changes: 5 additions & 0 deletions src/events/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ impl Plugin for TalksEventsPlugin {
}
}

/// Extension trait for [`App`] to register dialogue node events.
trait AppExt {
/// Registers a node event for a component.
fn register_node_event<
C: Component + NodeEventEmitter,
T: Event + bevy::reflect::GetTypeRegistration,
Expand Down Expand Up @@ -162,11 +164,14 @@ pub trait NodeEventEmitter {
fn make(&self, actors: &[Actor]) -> Box<dyn Reflect>;
}

/// Internal event used to trigger the emission of a node event.
#[derive(Event)]
pub(crate) struct EmissionTrigger<T: Event> {
/// The event to be emitted.
pub(crate) event: T,
}

/// System that relays node events to their respective event channels.
fn relay_node_event<T: Event>(mut t: ResMut<Events<EmissionTrigger<T>>>, mut w: EventWriter<T>) {
t.drain().for_each(|EmissionTrigger { event }| {
w.send(event);
Expand Down
17 changes: 12 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ impl Plugin for TalksPlugin {
}
}

/// The `SystemSet` for the `TalksPlugin`.
#[derive(SystemSet, Debug, Default, Clone, PartialEq, Eq, Hash)]
struct TalksSet;

Expand All @@ -57,6 +58,7 @@ fn error_logger(In(result): In<Result<(), NextActionError>>) {
}
}

/// Handles the `RefireNodeRequest` events. It will emit the events in the current node.
fn refire_handler(
mut cmd: Commands,
mut reqs: EventReader<RefireNodeRequest>,
Expand All @@ -70,7 +72,7 @@ fn refire_handler(
mut start_ev_writer: EventWriter<StartEvent>,
mut end_ev_writer: EventWriter<EndEvent>,
) -> Result<(), NextActionError> {
for event in reqs.read() {
if let Some(event) = reqs.read().next() {
for (current_node, talk_parent) in &current_nodes {
let this_talk = talk_parent.get();
// if this is the talk we want to advance
Expand Down Expand Up @@ -99,30 +101,34 @@ fn refire_handler(
}
Ok(())
}

/// Emits the start event if the current node is a start node.
#[inline]
pub(crate) fn maybe_emit_start_event(
start: &Query<Entity, With<StartNode>>,
current_node: Entity,
start_ev_writer: &mut EventWriter<StartEvent>,
requested_talk: Entity,
) {
if let Ok(_) = start.get(current_node) {
if start.get(current_node).is_ok() {
start_ev_writer.send(StartEvent(requested_talk));
}
}

/// Emit the end event if the current node is an end node.
#[inline]
pub(crate) fn maybe_emit_end_event(
end: &Query<Entity, With<EndNode>>,
next_node: Entity,
end_ev_writer: &mut EventWriter<EndEvent>,
requested_talk: Entity,
) {
if let Ok(_) = end.get(next_node) {
if end.get(next_node).is_ok() {
end_ev_writer.send(EndEvent(requested_talk));
}
}

/// Retrieves the actors connected to the given node.
#[inline]
pub(crate) fn retrieve_actors(
performers: &Query<Relations<PerformedBy>>,
Expand All @@ -138,6 +144,7 @@ pub(crate) fn retrieve_actors(
actors_in_node
}

/// Iterates over the `NodeEventEmitter` in the current node and emits the events.
#[inline]
pub(crate) fn emit_events(
cmd: &mut Commands,
Expand Down Expand Up @@ -217,13 +224,13 @@ mod tests {
};
let mut app = setup_and_next(&TalkData::new(script, vec![Actor::new("actor_1", "Actor")]));
let evs = app.world.resource::<Events<TextNodeEvent>>();
assert!(evs.get_reader().read(evs).len() == 1);
assert_eq!(evs.get_reader().read(evs).len(), 1);

let (talk_ent, _) = single::<(Entity, With<Talk>)>(&mut app.world);
app.world.send_event(RefireNodeRequest::new(talk_ent));
app.update();

let evs = app.world.resource::<Events<TextNodeEvent>>();
assert!(evs.get_reader().read(evs).len() == 2);
assert_eq!(evs.get_reader().read(evs).len(), 2);
}
}
2 changes: 1 addition & 1 deletion src/talk_asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ pub struct TalkData {
}

impl TalkData {
/// Creates a new TalkData with the given script and actors.
/// Creates a new `TalkData` with the given script and actors.
#[allow(dead_code)]
pub(crate) fn new(script: IndexMap<ActionId, Action>, actors: Vec<Actor>) -> Self {
Self { script, actors }
Expand Down
14 changes: 10 additions & 4 deletions src/traverse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@ use crate::{
use aery::{prelude::*, tuple_traits::RelationEntries};
use bevy::prelude::*;

/// Sets the `has_started` field of the `Talk` component to true when a `StartEvent` is received.
pub(crate) fn set_has_started(mut talks: Query<&mut Talk>, mut start_evs: EventReader<StartEvent>) {
for event in start_evs.read() {
let mut talk = talks.get_mut(event.0).expect("Talk");
talk.has_started = true;
}
}

/// Handles `NextActionRequest` events by moving the current node of the given `Talk` to the next one
/// and emitting the events in the next node.
pub(crate) fn next_handler(
mut cmd: Commands,
mut reqs: EventReader<NextNodeRequest>,
Expand All @@ -26,7 +29,7 @@ pub(crate) fn next_handler(
mut start_ev_writer: EventWriter<StartEvent>,
mut end_ev_writer: EventWriter<EndEvent>,
) -> Result<(), NextActionError> {
for event in reqs.read() {
if let Some(event) = reqs.read().next() {
for (current_node, talk_parent, edges) in &current_nodes {
let this_talk = talk_parent.get();
// if this is the talk we want to advance
Expand All @@ -36,7 +39,7 @@ pub(crate) fn next_handler(

let followings = edges.targets(FollowedBy);

let next_node = validate_next_node(&followings)?;
let next_node = validate_next_node(followings)?;

// send end event if next node is an end node
maybe_emit_end_event(&end, next_node, &mut end_ev_writer, event.talk);
Expand Down Expand Up @@ -80,7 +83,7 @@ pub(crate) fn choice_handler(
mut start_ev_writer: EventWriter<StartEvent>,
mut end_ev_writer: EventWriter<EndEvent>,
) -> Result<(), NextActionError> {
for event in reqs.read() {
if let Some(event) = reqs.read().next() {
for (current_node, talk_parent, edges) in &current_nodes {
let this_talk = talk_parent.get();
// if this is the talk we want to advance
Expand All @@ -90,7 +93,7 @@ pub(crate) fn choice_handler(

let followings = edges.targets(FollowedBy);

let next_node = validate_chosen_node(&followings, event.next)?;
let next_node = validate_chosen_node(followings, event.next)?;

// send end event if next node is an end node
maybe_emit_end_event(&end, next_node, &mut end_ev_writer, event.talk);
Expand All @@ -117,12 +120,14 @@ pub(crate) fn choice_handler(
Ok(())
}

/// Moves the `CurrentNode` component from the current node to the next node.
#[inline]
fn move_current(cmd: &mut Commands<'_, '_>, current_node: Entity, next_node: Entity) {
cmd.entity(current_node).remove::<CurrentNode>();
cmd.entity(next_node).insert(CurrentNode);
}

/// Validates that there is only one next node.
#[inline]
fn validate_next_node(followings: &[Entity]) -> Result<Entity, NextActionError> {
if followings.len() > 1 {
Expand All @@ -134,6 +139,7 @@ fn validate_next_node(followings: &[Entity]) -> Result<Entity, NextActionError>
Ok(followings[0])
}

/// Validates that the chosen next node is connected to the current node.
fn validate_chosen_node(
followings: &[Entity],
chosen_node: Entity,
Expand Down

0 comments on commit a83881b

Please sign in to comment.