From 9698dcdfeee28ab0d765b0dfaa7ef885f709d2ce Mon Sep 17 00:00:00 2001 From: hut Date: Sat, 13 Apr 2024 15:26:45 +0200 Subject: [PATCH] render choices --- src/chat.rs | 28 +++++++++++++++++++------- src/chats/serenity.yaml | 3 ++- src/hud.rs | 44 ++++++++++++++++++++--------------------- 3 files changed, 45 insertions(+), 30 deletions(-) diff --git a/src/chat.rs b/src/chat.rs index 64fd2ea..164d87f 100644 --- a/src/chat.rs +++ b/src/chat.rs @@ -57,7 +57,10 @@ pub struct Chat { } #[derive(Component)] -pub struct Choice; +pub struct Choice { + pub text: String, + pub key: usize, +} // This is the only place where any YAML interaction should be happening. #[derive(Resource)] @@ -116,7 +119,7 @@ impl ChatDB { let mut is_skipping_through = false; chat.position += 1; if chat.position >= conv.len() { - event.send(ChatEvent::DisplayMessage("Disconnected.".to_string())); + event.send(ChatEvent::SpawnMessage("Disconnected.".to_string())); event.send(ChatEvent::DespawnAllChats); return; } @@ -124,17 +127,19 @@ impl ChatDB { is_skipping_through = true; } else if let Some(message) = conv[chat.position].as_str() { - event.send(ChatEvent::DisplayMessage(message.to_string())); + event.send(ChatEvent::SpawnMessage(message.to_string())); } // Check if the immediately following entries are choices let mut pos = chat.position + 1; + let mut key: usize = 0; loop { if is_skipping_through || pos >= conv.len() { break; } if let Some(choice) = self.search_choice(&conv[pos]) { - event.send(ChatEvent::DisplayMessage(choice)); + event.send(ChatEvent::SpawnChoice(choice, key)); + key += 1; } else { break; @@ -162,8 +167,8 @@ pub struct StartConversationEvent { pub enum ChatEvent { DespawnAllChoices, DespawnAllChats, - DisplayMessage(String), - //SpawnChoice(String), + SpawnMessage(String), + SpawnChoice(String, usize), //Script(String, String, String), } @@ -251,10 +256,19 @@ pub fn handle_chat_events( ChatEvent::DespawnAllChats => { commands.entity(chat_entity).despawn(); } - ChatEvent::DisplayMessage(message) => { + ChatEvent::SpawnMessage(message) => { log.chat(message.into(), chat.talker.name.clone().unwrap_or(NAME_FALLBACK.to_string())); chat.timer = now + ((message.len() as f32).max(CHAT_SPEED_MIN_LEN) * TALKER_SPEED_FACTOR * chat.talker.talking_speed / settings.chat_speed) as f64; } + ChatEvent::SpawnChoice(replytext, key) => { + commands.spawn(( + world::DespawnOnPlayerDeath, + Choice { + text: replytext.into(), + key: *key, + } + )); + } } } } diff --git a/src/chats/serenity.yaml b/src/chats/serenity.yaml index 5cadda7..ed317f0 100644 --- a/src/chats/serenity.yaml +++ b/src/chats/serenity.yaml @@ -26,9 +26,10 @@ - Thank you!: - label: thx - No worries. Folks are stretched thin around this corner, we gotta watch out for each other. - - How talkcount are you feeling? + - How are you feeling? - goto: howru - I didn't ask for this.: + - label: didntask - "Sure, 'cause you were unconscious. I just did what felt right. Is there a problem?" - Nevermind. Thank you.: - goto: thx diff --git a/src/hud.rs b/src/hud.rs index c9951cf..ef9cabc 100644 --- a/src/hud.rs +++ b/src/hud.rs @@ -1,4 +1,4 @@ -use crate::{actor, audio, camera, nature, settings, world}; +use crate::{actor, audio, camera, chat, nature, settings, world}; use bevy::prelude::*; use bevy::diagnostic::{DiagnosticsStore, FrameTimeDiagnosticsPlugin}; use bevy::transform::TransformSystem; @@ -412,7 +412,7 @@ fn update_hud( q_camera: Query<(&Position, &LinearVelocity), With>, mut timer: ResMut, mut query: Query<&mut Text, With>, - //q_choices: Query<&chat::ChoiceAvailable>, + q_choices: Query<&chat::Choice>, mut query_chat: Query<&mut Text, (With, Without)>, query_all_actors: Query<&actor::Actor>, settings: Res, @@ -515,26 +515,26 @@ fn update_hud( } if let Ok(mut chat) = query_chat.get_single_mut() { -// // Choices -// let mut choices: Vec = Vec::new(); -// let mut count = 0; -// for choice in &q_choices { -// if count > 9 { -// break; -// } -// let press_this = REPLY_NUMBERS[count]; -// let reply = &choice.text; -// //let recipient = &choice.recipient; -// // TODO: indicate recipients if there's more than one -// choices.push(format!("{press_this} {reply}")); -// count += 1; -// } -// if count < 4 { -// for _padding in 0..(4-count) { -// choices.push(" ".to_string()); -// } -// } -// chat.sections[2].value = choices.join("\n"); + // Choices + let mut choices: Vec = Vec::new(); + let mut count = 0; + for choice in &q_choices { + if count > 9 { + break; + } + let press_this = REPLY_NUMBERS[choice.key]; + let reply = &choice.text; + //let recipient = &choice.recipient; + // TODO: indicate recipients if there's more than one + choices.push(format!("{press_this} {reply}")); + count += 1; + } + if count < 4 { + for _padding in 0..(4-count) { + choices.push(" ".to_string()); + } + } + chat.sections[2].value = choices.join("\n"); // Chat Log and System Log let logfilter = if settings.hud_active {