diff --git a/src/chat.rs b/src/chat.rs index ec33351..552aac4 100644 --- a/src/chat.rs +++ b/src/chat.rs @@ -9,14 +9,17 @@ pub const CHATS: &[&str] = &[ ]; pub const TOKEN_CHAT: &str = "chat"; +pub const TALKER_SPEED_FACTOR: f32 = 1.0 / 17.0; +pub const CHAT_SPEED_MIN_LEN: f32 = 40.0; 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), + handle_new_conversations.before(handle_chat_events), + handle_chat_events, + handle_chat_timer.before(handle_chat_events), )); app.add_event::(); app.add_event::(); @@ -29,6 +32,7 @@ pub struct Chat { pub id: usize, pub position: usize, pub timer: f64, + pub talker: Talker, } #[derive(Component)] @@ -66,9 +70,17 @@ impl ChatDB { pub fn advance_chat(&self, chat: &mut Chat, event: &mut EventWriter) { event.send(ChatEvent::DespawnAllChoices); - let conv = &self.0[chat.id]; + let conv = &self.0[chat.id].as_vec(); + if conv.is_none() { + return; + } + let conv = conv.unwrap(); chat.position += 1; - dbg!(chat.position); + if chat.position >= conv.len() { + event.send(ChatEvent::DisplayMessage("Disconnected.".to_string())); + event.send(ChatEvent::DespawnAllChats); + return; + } if let Some(message) = conv[chat.position].as_str() { event.send(ChatEvent::DisplayMessage(message.to_string())); } @@ -81,6 +93,7 @@ pub struct Talker { pub conv_id: String, pub name: Option, pub pronoun: Option, + pub talking_speed: f32, } #[derive(Event)] @@ -91,6 +104,7 @@ pub struct StartConversationEvent { #[derive(Event)] pub enum ChatEvent { DespawnAllChoices, + DespawnAllChats, DisplayMessage(String), //SpawnChoice(String), //Script(String, String, String), @@ -125,6 +139,7 @@ pub fn handle_new_conversations( id: chat_id, position: 0, timer: time.elapsed_seconds_f64(), + talker: event.talker.clone(), }; chatdb.advance_chat(&mut chat, &mut ew_chatevent); commands.spawn(( @@ -139,21 +154,48 @@ pub fn handle_new_conversations( } } +pub fn handle_chat_timer( + time: Res