From 57f0e94bc624e146aa640e1153d2be8b44c6feff Mon Sep 17 00:00:00 2001 From: hut Date: Sat, 13 Apr 2024 16:03:15 +0200 Subject: [PATCH] WIP stack-based conversation position --- src/chat.rs | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/src/chat.rs b/src/chat.rs index 8d2f711..3ada57f 100644 --- a/src/chat.rs +++ b/src/chat.rs @@ -54,7 +54,7 @@ impl Plugin for ChatPlugin { #[derive(Component)] pub struct Chat { pub id: usize, - pub position: usize, + pub position: Vec, pub timer: f64, pub talker: Talker, } @@ -110,6 +110,21 @@ impl ChatDB { return None; } + // returns false if the advanced pointer is out of bounds + fn advance_pointer(&self, chat: &mut Chat) -> bool { + chat.position[0] += 1; + return true; + } + + fn pointer_lookahead(&self, position: &mut Vec) { + let index = position.len() - 1; + position[index] += 1; + } + + fn at(&self, id: usize, position: Vec) -> Option { + return None; + } + pub fn advance_chat(&self, chat: &mut Chat, event: &mut EventWriter) { event.send(ChatEvent::DespawnAllChoices); let conv = &self.0[chat.id].as_vec(); @@ -120,34 +135,34 @@ impl ChatDB { // Handle next entry in the chat list let mut is_skipping_through = false; - chat.position += 1; - if chat.position >= conv.len() { + if !self.advance_pointer(chat) { event.send(ChatEvent::SpawnMessage("Disconnected.".to_string())); event.send(ChatEvent::DespawnAllChats); return; } - else if let Some(_) = self.search_choice(&conv[chat.position]) { + else if let Some(_) = self.search_choice(&conv[chat.position[0]]) { is_skipping_through = true; } - else if let Some(message) = conv[chat.position].as_str() { + else if let Some(message) = conv[chat.position[0]].as_str() { event.send(ChatEvent::SpawnMessage(message.to_string())); } // Check if the immediately following entries are choices - let mut pos = chat.position + 1; + let mut pos = chat.position.clone(); + self.pointer_lookahead(&mut pos); let mut key: usize = 0; loop { - if is_skipping_through || pos >= conv.len() { + if is_skipping_through || pos[0] >= conv.len() { break; } - if let Some(choice) = self.search_choice(&conv[pos]) { + if let Some(choice) = self.search_choice(&conv[pos[0]]) { event.send(ChatEvent::SpawnChoice(choice, key)); key += 1; } else { break; } - pos += 1; + self.pointer_lookahead(&mut pos); } } } @@ -202,7 +217,7 @@ pub fn handle_new_conversations( ew_sfx.send(audio::PlaySfxEvent(audio::Sfx::Ping)); let mut chat = Chat { id: chat_id, - position: 0, + position: vec![0], timer: time.elapsed_seconds_f64(), talker: event.talker.clone(), };