implement showing the first message of a chat

This commit is contained in:
yuni 2024-04-13 00:39:21 +02:00
parent 45d3408f0c
commit b41891ba3b
3 changed files with 41 additions and 31 deletions

View file

@ -1,7 +1,7 @@
extern crate yaml_rust; extern crate yaml_rust;
use bevy::prelude::*; use bevy::prelude::*;
use yaml_rust::{Yaml, YamlLoader}; use yaml_rust::{Yaml, YamlLoader};
use crate::{audio, world}; use crate::{audio, hud, world};
pub const CHATS: &[&str] = &[ pub const CHATS: &[&str] = &[
include_str!("chats/serenity.yaml"), include_str!("chats/serenity.yaml"),
@ -26,8 +26,8 @@ impl Plugin for ChatPlugin {
#[derive(Component)] #[derive(Component)]
pub struct Chat { pub struct Chat {
pub id: u32, pub id: usize,
pub position: u32, pub position: usize,
pub timer: f64, pub timer: f64,
} }
@ -46,15 +46,15 @@ impl ChatDB {
return Err(()); return Err(());
} }
pub fn get_chat_by_id(&self, id: &String) -> Result<u32, String> { pub fn get_chat_by_id(&self, id: &String) -> Result<usize, String> {
let mut found: Option<u32> = None; let mut found: Option<usize> = None;
for (index, object_yaml) in self.0.iter().enumerate() { for (index, object_yaml) in self.0.iter().enumerate() {
if let Some(chat_id) = object_yaml[0][TOKEN_CHAT].as_str() { if let Some(chat_id) = object_yaml[0][TOKEN_CHAT].as_str() {
if chat_id == id { if chat_id == id {
if found.is_some() { if found.is_some() {
return Err("Found multiple chats with the same id!".to_string()); 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<ChatEvent>) { pub fn advance_chat(&self, chat: &mut Chat, event: &mut EventWriter<ChatEvent>) {
event.send(ChatEvent::DespawnAllChoices); event.send(ChatEvent::DespawnAllChoices);
let conv = &self.0[chat.id];
chat.position += 1; 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)] #[derive(Event)]
pub enum ChatEvent { pub enum ChatEvent {
DespawnAllChoices, DespawnAllChoices,
//ShowMessage(String), DisplayMessage(String),
//SpawnChoice(String), //SpawnChoice(String),
//Script(String, String, String), //Script(String, String, String),
} }
@ -118,7 +123,7 @@ pub fn handle_new_conversations(
ew_sfx.send(audio::PlaySfxEvent(audio::Sfx::Ping)); ew_sfx.send(audio::PlaySfxEvent(audio::Sfx::Ping));
let mut chat = Chat { let mut chat = Chat {
id: chat_id, id: chat_id,
position: 1, // not 0, since the first item is always the chat name position: 0,
timer: time.elapsed_seconds_f64(), timer: time.elapsed_seconds_f64(),
}; };
chatdb.advance_chat(&mut chat, &mut ew_chatevent); chatdb.advance_chat(&mut chat, &mut ew_chatevent);
@ -137,6 +142,7 @@ pub fn handle_new_conversations(
pub fn handle_chat_events( pub fn handle_chat_events(
mut commands: Commands, mut commands: Commands,
mut er_chatevent: EventReader<ChatEvent>, mut er_chatevent: EventReader<ChatEvent>,
mut log: ResMut<hud::Log>,
q_choices: Query<Entity, With<Choice>>, q_choices: Query<Entity, With<Choice>>,
) { ) {
for event in er_chatevent.read() { for event in er_chatevent.read() {
@ -146,6 +152,9 @@ pub fn handle_chat_events(
commands.entity(entity).despawn(); commands.entity(entity).despawn();
} }
} }
ChatEvent::DisplayMessage(message) => {
log.chat(message.into(), "Someone".to_string());
}
} }
} }
} }

View file

@ -17,6 +17,7 @@
- chat: Icarus - chat: Icarus
- TEST MESSAGE
- if talkcount > 0: - if talkcount > 0:
- "Oh, hi again! How are you?" - "Oh, hi again! How are you?"
- goto: howru - goto: howru

View file

@ -413,7 +413,7 @@ fn update_hud(
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<&chat::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>,
q_target: Query<(&IsClickable, Option<&Position>, Option<&LinearVelocity>), With<IsTargeted>>, q_target: Query<(&IsClickable, Option<&Position>, Option<&LinearVelocity>), With<IsTargeted>>,
@ -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 // // Choices
// let mut choices: Vec<String> = Vec::new(); // let mut choices: Vec<String> = Vec::new();
// let mut count = 0; // let mut count = 0;
@ -535,27 +535,27 @@ fn update_hud(
// } // }
// } // }
// chat.sections[2].value = choices.join("\n"); // chat.sections[2].value = choices.join("\n");
//
// // Chat Log and System Log // Chat Log and System Log
// let logfilter = if settings.hud_active { let logfilter = if settings.hud_active {
// |_msg: &&Message| { true } |_msg: &&Message| { true }
// } else { } else {
// |msg: &&Message| { match msg.level { |msg: &&Message| { match msg.level {
// LogLevel::Chat => true, LogLevel::Chat => true,
// LogLevel::Warning => true, LogLevel::Warning => true,
// LogLevel::Info => true, LogLevel::Info => true,
// _ => false _ => false
// }} }}
// }; };
// let logs_vec: Vec<String> = log.logs.iter() let logs_vec: Vec<String> = log.logs.iter()
// .filter(logfilter) .filter(logfilter)
// .map(|s| if s.sender.is_empty() { .map(|s| if s.sender.is_empty() {
// format!("{}", s.text) format!("{}", s.text)
// } else { } else {
// format!("{}: {}", s.sender, s.text) format!("{}: {}", s.sender, s.text)
// }).collect(); }).collect();
// chat.sections[0].value = logs_vec.join("\n"); chat.sections[0].value = logs_vec.join("\n");
// } }
log.needs_rerendering = false; log.needs_rerendering = false;
log.remove_old(); log.remove_old();
} }