From b41891ba3b51cc9167249564cf85f9b178d2843f Mon Sep 17 00:00:00 2001 From: hut Date: Sat, 13 Apr 2024 00:39:21 +0200 Subject: [PATCH] implement showing the first message of a chat --- src/chat.rs | 25 +++++++++++++++------- src/chats/serenity.yaml | 1 + src/hud.rs | 46 ++++++++++++++++++++--------------------- 3 files changed, 41 insertions(+), 31 deletions(-) diff --git a/src/chat.rs b/src/chat.rs index c34fa0b..ec33351 100644 --- a/src/chat.rs +++ b/src/chat.rs @@ -1,7 +1,7 @@ extern crate yaml_rust; use bevy::prelude::*; use yaml_rust::{Yaml, YamlLoader}; -use crate::{audio, world}; +use crate::{audio, hud, world}; pub const CHATS: &[&str] = &[ include_str!("chats/serenity.yaml"), @@ -26,8 +26,8 @@ impl Plugin for ChatPlugin { #[derive(Component)] pub struct Chat { - pub id: u32, - pub position: u32, + pub id: usize, + pub position: usize, pub timer: f64, } @@ -46,15 +46,15 @@ impl ChatDB { return Err(()); } - pub fn get_chat_by_id(&self, id: &String) -> Result { - let mut found: Option = None; + pub fn get_chat_by_id(&self, id: &String) -> Result { + let mut found: Option = None; for (index, object_yaml) in self.0.iter().enumerate() { if let Some(chat_id) = object_yaml[0][TOKEN_CHAT].as_str() { if chat_id == id { if found.is_some() { return Err("Found multiple chats with the same id!".to_string()); } - found = Some(index as u32); + found = Some(index); } } } @@ -66,7 +66,12 @@ impl ChatDB { pub fn advance_chat(&self, chat: &mut Chat, event: &mut EventWriter) { event.send(ChatEvent::DespawnAllChoices); + let conv = &self.0[chat.id]; chat.position += 1; + dbg!(chat.position); + if let Some(message) = conv[chat.position].as_str() { + event.send(ChatEvent::DisplayMessage(message.to_string())); + } } } @@ -86,7 +91,7 @@ pub struct StartConversationEvent { #[derive(Event)] pub enum ChatEvent { DespawnAllChoices, - //ShowMessage(String), + DisplayMessage(String), //SpawnChoice(String), //Script(String, String, String), } @@ -118,7 +123,7 @@ pub fn handle_new_conversations( ew_sfx.send(audio::PlaySfxEvent(audio::Sfx::Ping)); let mut chat = Chat { id: chat_id, - position: 1, // not 0, since the first item is always the chat name + position: 0, timer: time.elapsed_seconds_f64(), }; chatdb.advance_chat(&mut chat, &mut ew_chatevent); @@ -137,6 +142,7 @@ pub fn handle_new_conversations( pub fn handle_chat_events( mut commands: Commands, mut er_chatevent: EventReader, + mut log: ResMut, q_choices: Query>, ) { for event in er_chatevent.read() { @@ -146,6 +152,9 @@ pub fn handle_chat_events( commands.entity(entity).despawn(); } } + ChatEvent::DisplayMessage(message) => { + log.chat(message.into(), "Someone".to_string()); + } } } } diff --git a/src/chats/serenity.yaml b/src/chats/serenity.yaml index 73a13dd..c9bb364 100644 --- a/src/chats/serenity.yaml +++ b/src/chats/serenity.yaml @@ -17,6 +17,7 @@ - chat: Icarus +- TEST MESSAGE - if talkcount > 0: - "Oh, hi again! How are you?" - goto: howru diff --git a/src/hud.rs b/src/hud.rs index 73397a8..c9951cf 100644 --- a/src/hud.rs +++ b/src/hud.rs @@ -413,7 +413,7 @@ fn update_hud( mut timer: ResMut, mut query: Query<&mut Text, With>, //q_choices: Query<&chat::ChoiceAvailable>, - //mut query_chat: Query<&mut Text, (With, Without)>, + mut query_chat: Query<&mut Text, (With, Without)>, query_all_actors: Query<&actor::Actor>, settings: Res, q_target: Query<(&IsClickable, Option<&Position>, Option<&LinearVelocity>), With>, @@ -514,7 +514,7 @@ fn update_hud( } } -// if let Ok(mut chat) = query_chat.get_single_mut() { + if let Ok(mut chat) = query_chat.get_single_mut() { // // Choices // let mut choices: Vec = Vec::new(); // let mut count = 0; @@ -535,27 +535,27 @@ fn update_hud( // } // } // chat.sections[2].value = choices.join("\n"); -// -// // Chat Log and System Log -// let logfilter = if settings.hud_active { -// |_msg: &&Message| { true } -// } else { -// |msg: &&Message| { match msg.level { -// LogLevel::Chat => true, -// LogLevel::Warning => true, -// LogLevel::Info => true, -// _ => false -// }} -// }; -// let logs_vec: Vec = log.logs.iter() -// .filter(logfilter) -// .map(|s| if s.sender.is_empty() { -// format!("{}", s.text) -// } else { -// format!("{}: {}", s.sender, s.text) -// }).collect(); -// chat.sections[0].value = logs_vec.join("\n"); -// } + + // Chat Log and System Log + let logfilter = if settings.hud_active { + |_msg: &&Message| { true } + } else { + |msg: &&Message| { match msg.level { + LogLevel::Chat => true, + LogLevel::Warning => true, + LogLevel::Info => true, + _ => false + }} + }; + let logs_vec: Vec = log.logs.iter() + .filter(logfilter) + .map(|s| if s.sender.is_empty() { + format!("{}", s.text) + } else { + format!("{}: {}", s.sender, s.text) + }).collect(); + chat.sections[0].value = logs_vec.join("\n"); + } log.needs_rerendering = false; log.remove_old(); }