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_chat_scripts,
));
app.add_systems(PostUpdate, despawn_old_choices);
app.add_event::<StartConversationEvent>();
app.add_event::<SendMessageEvent>();
app.add_event::<ChatScriptEvent>();
@ -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();
}
}

View file

@ -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<actor::PlayerCamera>>,
mut timer: ResMut<FPSUpdateTimer>,
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>)>,
query_all_actors: Query<&actor::Actor>,
settings: Res<settings::Settings>,
@ -486,7 +477,7 @@ fn handle_input(
keyboard_input: Res<ButtonInput<KeyCode>>,
mut settings: ResMut<settings::Settings>,
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_sfx: EventWriter<audio::PlaySfxEvent>,
mut evwriter_togglemusic: EventWriter<audio::ToggleMusicEvent>,
@ -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();
}
}