From 8f380a67732ef095e59f86f6376faf3e3061df50 Mon Sep 17 00:00:00 2001 From: hut Date: Thu, 4 Apr 2024 13:39:49 +0200 Subject: [PATCH] split off chat logic from hud.rs into chat.rs --- src/chat.rs | 33 +++++++++++++++++++++++++++++++-- src/hud.rs | 32 ++------------------------------ 2 files changed, 33 insertions(+), 32 deletions(-) diff --git a/src/chat.rs b/src/chat.rs index a14e440..a797462 100644 --- a/src/chat.rs +++ b/src/chat.rs @@ -11,6 +11,7 @@ impl Plugin for ChatPlugin { handle_conversations, handle_chat_scripts, )); + app.add_systems(PostUpdate, despawn_old_choices); app.add_event::(); app.add_event::(); app.add_event::(); @@ -61,6 +62,15 @@ pub struct Chat { pub timer: f64, } +#[derive(Component)] +pub struct ChoiceAvailable { + pub conv_id: String, + pub conv_label: String, + pub recipient: String, + pub text: String, +} + + #[derive(Component)] #[derive(Clone)] pub struct Talker { @@ -154,7 +164,7 @@ pub fn handle_send_messages( .collect(); for choice in choices { if choice.choice.as_str() != hud::CHOICE_NONE { - commands.spawn(hud::ChoiceAvailable { + commands.spawn(ChoiceAvailable { conv_id: choice.id.clone(), conv_label: choice.label.clone(), recipient: choice.name.clone(), @@ -230,7 +240,7 @@ pub fn handle_conversations( .collect(); for choice in choices { if choice.choice.as_str() != hud::CHOICE_NONE { - commands.spawn(hud::ChoiceAvailable { + commands.spawn(ChoiceAvailable { conv_id: choice.id.clone(), conv_label: choice.label.clone(), recipient: choice.name.clone(), @@ -290,3 +300,22 @@ pub fn handle_chat_scripts( } } } + +fn despawn_old_choices( + mut commands: Commands, + q_conv: Query<&Chat>, + q_choices: Query<(Entity, &ChoiceAvailable)>, +) { + let chats: Vec<&Chat> = q_conv.iter().collect(); + 'outer: for (entity, choice) in &q_choices { + // Let's see if this choice still has a chat in the appropriate state + for chat in &chats { + if choice.conv_id == chat.id && choice.conv_label == chat.label { + continue 'outer; + } + } + + // Despawn the choice, since no matching chat was found + commands.entity(entity).despawn(); + } +} diff --git a/src/hud.rs b/src/hud.rs index 43ed756..c467b22 100644 --- a/src/hud.rs +++ b/src/hud.rs @@ -21,7 +21,6 @@ impl Plugin for HudPlugin { fn build(&self, app: &mut App) { app.add_systems(Startup, setup); app.add_systems(Update, (update, handle_input)); - app.add_systems(PostUpdate, despawn_old_choices); app.insert_resource(Log { logs: VecDeque::with_capacity(LOG_MAX), needs_rerendering: true, @@ -39,14 +38,6 @@ impl Plugin for HudPlugin { #[derive(Resource)] struct FPSUpdateTimer(Timer); -#[derive(Component)] -pub struct ChoiceAvailable { - pub conv_id: String, - pub conv_label: String, - pub recipient: String, - pub text: String, -} - pub enum LogLevel { Warning, //Error, @@ -389,7 +380,7 @@ fn update( q_camera: Query<(&Position, &LinearVelocity), With>, mut timer: ResMut, mut query: Query<&mut Text, With>, - q_choices: Query<&ChoiceAvailable>, + q_choices: Query<&chat::ChoiceAvailable>, mut query_chat: Query<&mut Text, (With, Without)>, query_all_actors: Query<&actor::Actor>, settings: Res, @@ -486,7 +477,7 @@ fn handle_input( keyboard_input: Res>, mut settings: ResMut, mut q_hud: Query<&mut Visibility, With>, - q_choices: Query<&ChoiceAvailable>, + q_choices: Query<&chat::ChoiceAvailable>, mut evwriter_sendmsg: EventWriter, mut evwriter_sfx: EventWriter, mut evwriter_togglemusic: EventWriter, @@ -531,22 +522,3 @@ fn handle_input( selected_choice += 1; } } - -fn despawn_old_choices( - mut commands: Commands, - q_conv: Query<&chat::Chat>, - q_choices: Query<(Entity, &ChoiceAvailable)>, -) { - let chats: Vec<&chat::Chat> = q_conv.iter().collect(); - 'outer: for (entity, choice) in &q_choices { - // Let's see if this choice still has a chat in the appropriate state - for chat in &chats { - if choice.conv_id == chat.id && choice.conv_label == chat.label { - continue 'outer; - } - } - - // Despawn the choice, since no matching chat was found - commands.entity(entity).despawn(); - } -}