split off chat logic from hud.rs into chat.rs

This commit is contained in:
yuni 2024-04-04 13:39:49 +02:00
parent ca7d2facd9
commit 8f380a6773
2 changed files with 33 additions and 32 deletions

View file

@ -11,6 +11,7 @@ impl Plugin for ChatPlugin {
handle_conversations, handle_conversations,
handle_chat_scripts, handle_chat_scripts,
)); ));
app.add_systems(PostUpdate, despawn_old_choices);
app.add_event::<StartConversationEvent>(); app.add_event::<StartConversationEvent>();
app.add_event::<SendMessageEvent>(); app.add_event::<SendMessageEvent>();
app.add_event::<ChatScriptEvent>(); app.add_event::<ChatScriptEvent>();
@ -61,6 +62,15 @@ pub struct Chat {
pub timer: f64, 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(Component)]
#[derive(Clone)] #[derive(Clone)]
pub struct Talker { pub struct Talker {
@ -154,7 +164,7 @@ pub fn handle_send_messages(
.collect(); .collect();
for choice in choices { for choice in choices {
if choice.choice.as_str() != hud::CHOICE_NONE { if choice.choice.as_str() != hud::CHOICE_NONE {
commands.spawn(hud::ChoiceAvailable { commands.spawn(ChoiceAvailable {
conv_id: choice.id.clone(), conv_id: choice.id.clone(),
conv_label: choice.label.clone(), conv_label: choice.label.clone(),
recipient: choice.name.clone(), recipient: choice.name.clone(),
@ -230,7 +240,7 @@ pub fn handle_conversations(
.collect(); .collect();
for choice in choices { for choice in choices {
if choice.choice.as_str() != hud::CHOICE_NONE { if choice.choice.as_str() != hud::CHOICE_NONE {
commands.spawn(hud::ChoiceAvailable { commands.spawn(ChoiceAvailable {
conv_id: choice.id.clone(), conv_id: choice.id.clone(),
conv_label: choice.label.clone(), conv_label: choice.label.clone(),
recipient: choice.name.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();
}
}

View file

@ -21,7 +21,6 @@ impl Plugin for HudPlugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
app.add_systems(Startup, setup); app.add_systems(Startup, setup);
app.add_systems(Update, (update, handle_input)); app.add_systems(Update, (update, handle_input));
app.add_systems(PostUpdate, despawn_old_choices);
app.insert_resource(Log { app.insert_resource(Log {
logs: VecDeque::with_capacity(LOG_MAX), logs: VecDeque::with_capacity(LOG_MAX),
needs_rerendering: true, needs_rerendering: true,
@ -39,14 +38,6 @@ impl Plugin for HudPlugin {
#[derive(Resource)] #[derive(Resource)]
struct FPSUpdateTimer(Timer); 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 { pub enum LogLevel {
Warning, Warning,
//Error, //Error,
@ -389,7 +380,7 @@ fn update(
q_camera: Query<(&Position, &LinearVelocity), With<actor::PlayerCamera>>, q_camera: Query<(&Position, &LinearVelocity), With<actor::PlayerCamera>>,
mut timer: ResMut<FPSUpdateTimer>, mut timer: ResMut<FPSUpdateTimer>,
mut query: Query<&mut Text, With<GaugesText>>, mut query: Query<&mut Text, With<GaugesText>>,
q_choices: Query<&ChoiceAvailable>, q_choices: Query<&chat::ChoiceAvailable>,
mut query_chat: Query<&mut Text, (With<ChatText>, Without<GaugesText>)>, mut query_chat: Query<&mut Text, (With<ChatText>, Without<GaugesText>)>,
query_all_actors: Query<&actor::Actor>, query_all_actors: Query<&actor::Actor>,
settings: Res<settings::Settings>, settings: Res<settings::Settings>,
@ -486,7 +477,7 @@ fn handle_input(
keyboard_input: Res<ButtonInput<KeyCode>>, keyboard_input: Res<ButtonInput<KeyCode>>,
mut settings: ResMut<settings::Settings>, mut settings: ResMut<settings::Settings>,
mut q_hud: Query<&mut Visibility, With<ToggleableHudElement>>, mut q_hud: Query<&mut Visibility, With<ToggleableHudElement>>,
q_choices: Query<&ChoiceAvailable>, q_choices: Query<&chat::ChoiceAvailable>,
mut evwriter_sendmsg: EventWriter<chat::SendMessageEvent>, mut evwriter_sendmsg: EventWriter<chat::SendMessageEvent>,
mut evwriter_sfx: EventWriter<audio::PlaySfxEvent>, mut evwriter_sfx: EventWriter<audio::PlaySfxEvent>,
mut evwriter_togglemusic: EventWriter<audio::ToggleMusicEvent>, mut evwriter_togglemusic: EventWriter<audio::ToggleMusicEvent>,
@ -531,22 +522,3 @@ fn handle_input(
selected_choice += 1; 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();
}
}