WIP stack-based conversation position

This commit is contained in:
yuni 2024-04-13 16:03:15 +02:00
parent ef9d9c8fcc
commit 57f0e94bc6

View file

@ -54,7 +54,7 @@ impl Plugin for ChatPlugin {
#[derive(Component)] #[derive(Component)]
pub struct Chat { pub struct Chat {
pub id: usize, pub id: usize,
pub position: usize, pub position: Vec<usize>,
pub timer: f64, pub timer: f64,
pub talker: Talker, pub talker: Talker,
} }
@ -110,6 +110,21 @@ impl ChatDB {
return None; 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<usize>) {
let index = position.len() - 1;
position[index] += 1;
}
fn at(&self, id: usize, position: Vec<usize>) -> Option<Yaml> {
return None;
}
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].as_vec(); let conv = &self.0[chat.id].as_vec();
@ -120,34 +135,34 @@ impl ChatDB {
// Handle next entry in the chat list // Handle next entry in the chat list
let mut is_skipping_through = false; let mut is_skipping_through = false;
chat.position += 1; if !self.advance_pointer(chat) {
if chat.position >= conv.len() {
event.send(ChatEvent::SpawnMessage("Disconnected.".to_string())); event.send(ChatEvent::SpawnMessage("Disconnected.".to_string()));
event.send(ChatEvent::DespawnAllChats); event.send(ChatEvent::DespawnAllChats);
return; 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; 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())); event.send(ChatEvent::SpawnMessage(message.to_string()));
} }
// Check if the immediately following entries are choices // 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; let mut key: usize = 0;
loop { loop {
if is_skipping_through || pos >= conv.len() { if is_skipping_through || pos[0] >= conv.len() {
break; 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)); event.send(ChatEvent::SpawnChoice(choice, key));
key += 1; key += 1;
} }
else { else {
break; 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)); ew_sfx.send(audio::PlaySfxEvent(audio::Sfx::Ping));
let mut chat = Chat { let mut chat = Chat {
id: chat_id, id: chat_id,
position: 0, position: vec![0],
timer: time.elapsed_seconds_f64(), timer: time.elapsed_seconds_f64(),
talker: event.talker.clone(), talker: event.talker.clone(),
}; };