extern crate yaml_rust; use bevy::prelude::*; use yaml_rust::{Yaml, YamlLoader}; use crate::{audio, world}; pub const CHATS: &[&str] = &[ include_str!("chats/serenity.yaml"), include_str!("chats/startrans.yaml"), ]; pub const TOKEN_CHAT: &str = "chat"; pub struct ChatPlugin; impl Plugin for ChatPlugin { fn build(&self, app: &mut App) { app.add_systems(Startup, load_chats); app.add_systems(Update, ( handle_new_conversations, handle_chat_events.after(handle_new_conversations), )); app.add_event::(); app.add_event::(); app.insert_resource(ChatDB(Vec::new())); } } #[derive(Component)] pub struct Chat { pub id: u32, pub position: u32, pub timer: f64, } #[derive(Component)] pub struct Choice; // This is the only place where any YAML interaction should be happening. #[derive(Resource)] pub struct ChatDB(Vec); impl ChatDB { pub fn load_from_str(&mut self, yaml_string: &str) -> Result<(), ()> { if let Ok(mut yaml_data) = YamlLoader::load_from_str(yaml_string) { self.0.append(&mut yaml_data); return Ok(()); } return Err(()); } 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); } } } if let Some(result) = found { return Ok(result); } return Err(format!("No chat with the conversation ID `{id}` was found.")); } pub fn advance_chat(&self, chat: &mut Chat, event: &mut EventWriter) { event.send(ChatEvent::DespawnAllChoices); chat.position += 1; } } #[derive(Component)] #[derive(Clone)] pub struct Talker { pub conv_id: String, pub name: Option, pub pronoun: Option, } #[derive(Event)] pub struct StartConversationEvent { pub talker: Talker, } #[derive(Event)] pub enum ChatEvent { DespawnAllChoices, //ShowMessage(String), //SpawnChoice(String), //Script(String, String, String), } pub fn load_chats(mut chatdb: ResMut) { for chat_yaml in CHATS { if chatdb.load_from_str(chat_yaml).is_err() { error!("Could not load chat definitions. Validate files in `src/chats/` path."); } } } pub fn handle_new_conversations( mut commands: Commands, mut er_conv: EventReader, mut ew_sfx: EventWriter, mut ew_chatevent: EventWriter, chatdb: Res, q_chats: Query<&Chat>, time: Res