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;
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<u32, String> {
let mut found: Option<u32> = None;
pub fn get_chat_by_id(&self, id: &String) -> Result<usize, String> {
let mut found: Option<usize> = 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<ChatEvent>) {
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<ChatEvent>,
mut log: ResMut<hud::Log>,
q_choices: Query<Entity, With<Choice>>,
) {
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());
}
}
}
}

View file

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

View file

@ -413,7 +413,7 @@ fn update_hud(
mut timer: ResMut<FPSUpdateTimer>,
mut query: Query<&mut Text, With<GaugesText>>,
//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>,
settings: Res<settings::Settings>,
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
// let mut choices: Vec<String> = 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<String> = 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<String> = 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();
}