From 8df6914dba1766ab853c854acc16cab1d5ed2221 Mon Sep 17 00:00:00 2001 From: hut Date: Sat, 13 Apr 2024 18:57:23 +0200 Subject: [PATCH] WIP choices --- src/chat.rs | 56 +++++++++++++++++++++++++++++++++++------------------ 1 file changed, 37 insertions(+), 19 deletions(-) diff --git a/src/chat.rs b/src/chat.rs index af11316..c1b8a82 100644 --- a/src/chat.rs +++ b/src/chat.rs @@ -40,10 +40,10 @@ impl Plugin for ChatPlugin { fn build(&self, app: &mut App) { app.add_systems(Startup, load_chats); app.add_systems(Update, ( + handle_reply_keys.before(handle_chat_timer), + handle_chat_timer.before(handle_chat_events), handle_new_conversations.before(handle_chat_events), handle_chat_events, - handle_reply_keys, - handle_chat_timer.before(handle_chat_events), )); app.add_event::(); app.add_event::(); @@ -51,10 +51,12 @@ impl Plugin for ChatPlugin { } } +type ChatPos = Vec; + #[derive(Component)] pub struct Chat { pub id: usize, - pub position: Vec, + pub position: ChatPos, pub timer: f64, pub talker: Talker, } @@ -63,6 +65,7 @@ pub struct Chat { pub struct Choice { pub text: String, pub key: usize, + pub goto: ChatPos, } // This is the only place where any YAML interaction should be happening. @@ -131,15 +134,23 @@ impl ChatDB { chat.position[index] += 1; while chat.position.len() > 0 { - if let None = self.at(chat.id, &chat.position) { - chat.position.pop(); + dbg!(self.at(chat.id, &chat.position)); + match self.at(chat.id, &chat.position) { + None => { + dbg!("Pop."); + chat.position.pop(); + let index = chat.position.len() - 1; + chat.position[index] += 1; + }, + Some(_) => { + break; + } } - let index = chat.position.len() - 1; - chat.position[index] += 1; } return true; } + // Note that this (intentionally) may result in a pointer that's out of bounds. fn pointer_lookahead(&self, position: &mut Vec) { let index = position.len() - 1; position[index] += 1; @@ -152,7 +163,10 @@ impl ChatDB { let mut pointer: Option<&Value> = Some(&self.0[id]); for index in position { if let Some(Value::Sequence(seq)) = self.0.get(id) { - pointer = Some(&seq[*index]); + // TODO: handle mappings + if let Some(value) = seq.get(*index) { + pointer = Some(value); + } } else { return None; } @@ -166,7 +180,6 @@ impl ChatDB { if conv.is_none() { return; } - let conv = conv.unwrap(); // Handle next entry in the chat list let mut is_skipping_through = false; @@ -178,7 +191,7 @@ impl ChatDB { else if let Some(_) = self.search_choice(self.at(chat.id, &chat.position)) { is_skipping_through = true; } - else if let Some(message) = conv[chat.position[0]].as_str() { + else if let Some(Value::String(message)) = self.at(chat.id, &chat.position) { event.send(ChatEvent::SpawnMessage(message.to_string())); } @@ -191,7 +204,9 @@ impl ChatDB { break; } if let Some(choice) = self.search_choice(self.at(chat.id, &pos)) { - event.send(ChatEvent::SpawnChoice(choice, key)); + let mut goto: Vec = pos.clone(); + goto.push(0); + event.send(ChatEvent::SpawnChoice(choice, key, goto)); key += 1; } else { @@ -221,7 +236,7 @@ pub enum ChatEvent { DespawnAllChoices, DespawnAllChats, SpawnMessage(String), - SpawnChoice(String, usize), + SpawnChoice(String, usize, ChatPos), //Script(String, String, String), } @@ -313,12 +328,13 @@ pub fn handle_chat_events( log.chat(message.into(), chat.talker.name.clone().unwrap_or(NAME_FALLBACK.to_string())); chat.timer = now + ((message.len() as f32).max(CHAT_SPEED_MIN_LEN) * TALKER_SPEED_FACTOR * chat.talker.talking_speed / settings.chat_speed) as f64; } - ChatEvent::SpawnChoice(replytext, key) => { + ChatEvent::SpawnChoice(replytext, key, goto) => { commands.spawn(( world::DespawnOnPlayerDeath, Choice { text: replytext.into(), key: *key, + goto: goto.clone(), } )); chat.timer = now + CHOICE_TIMER / settings.chat_speed as f64; @@ -331,20 +347,22 @@ fn handle_reply_keys( keyboard_input: Res>, settings: ResMut, q_choices: Query<&Choice>, + mut q_chats: Query<&mut Chat>, //mut evwriter_sendmsg: EventWriter, mut evwriter_sfx: EventWriter, + time: Res