From 5df3f66ea672ae57979aa1e6bd4a1b89e795c960 Mon Sep 17 00:00:00 2001 From: hut Date: Sun, 14 Apr 2024 16:20:51 +0200 Subject: [PATCH] implement "set" token for chats --- src/chat.rs | 32 ++++++++++++++++++++++---------- src/chats/serenity.yaml | 1 + src/commands.rs | 3 ++- src/defs.txt | 1 + src/main.rs | 1 + src/var.rs | 33 +++++++++++++++++++++++++++------ 6 files changed, 54 insertions(+), 17 deletions(-) diff --git a/src/chat.rs b/src/chat.rs index f862fbf..28fc22d 100644 --- a/src/chat.rs +++ b/src/chat.rs @@ -78,7 +78,7 @@ type ChatPos = Vec; #[derive(Component)] pub struct Chat { - pub id: usize, + pub internal_id: usize, pub position: ChatPos, pub timer: f64, pub talker: Talker, @@ -94,7 +94,8 @@ pub struct Choice { #[derive(Component)] #[derive(Clone)] pub struct Talker { - pub conv_id: String, + pub chat_name: String, + pub actor_id: String, pub name: Option, pub pronoun: Option, pub talking_speed: f32, @@ -116,6 +117,7 @@ pub enum ChatEvent { SpawnChoice(String, usize, ChatPos, bool), RunScript(String), SleepSeconds(f64), + SetVariable(String), } // This is the only place where any YAML interaction should be happening. @@ -297,7 +299,7 @@ impl ChatDB { let mut popped = false; let mut seek_past_dialog_choices = false; while chat.position.len() > 0 { - match self.at(chat.id, &chat.position) { + match self.at(chat.internal_id, &chat.position) { None => { chat.position.pop(); popped = true; @@ -380,7 +382,7 @@ impl ChatDB { // This includes flow control tokens like "goto", no-op tokens like "label", // but not something with a side effect like "script", and especially not "if". fn is_skippable(&self, chat: &mut Chat) -> bool { - let current_item = self.at(chat.id, &chat.position); + let current_item = self.at(chat.internal_id, &chat.position); if current_item.is_none() { return false; } @@ -406,7 +408,7 @@ impl ChatDB { chat: &mut Chat, event: &mut EventWriter, ) -> bool { - let current_item = self.at(chat.id, &chat.position); + let current_item = self.at(chat.internal_id, &chat.position); let mut processed_a_choice = false; match current_item { Some(Value::String(message)) => { @@ -452,7 +454,9 @@ impl ChatDB { event.send(ChatEvent::SpawnMessage( message.to_string(), hud::LogLevel::Warning, sound.clone())); } - (Some(TOKEN_SET), _) => {} // TODO + (Some(TOKEN_SET), Value::String(instructions)) => { + event.send(ChatEvent::SetVariable(instructions.to_string())); + } _ => {} } } @@ -467,7 +471,7 @@ impl ChatDB { } } (Some(TOKEN_GOTO), Value::String(label)) => { - match self.search_label(chat.id, &label) { + match self.search_label(chat.internal_id, &label) { Some(pos) => { chat.position = pos; } @@ -519,7 +523,7 @@ impl ChatDB { let mut key: usize = 0; let mut reached_end_of_branch = false; while let Some((choice, _, nowait)) = - self.search_choice(self.at(chat.id, &chat.position).as_ref()) { + self.search_choice(self.at(chat.internal_id, &chat.position).as_ref()) { if reached_end_of_branch { break; } @@ -556,10 +560,10 @@ pub fn handle_new_conversations( ew_sfx.send(audio::PlaySfxEvent(audio::Sfx::Ping)); return; } - match (*chatdb).get_chat_by_id(&event.talker.conv_id) { + match (*chatdb).get_chat_by_id(&event.talker.chat_name) { Ok(chat_id) => { let mut chat = Chat { - id: chat_id, + internal_id: chat_id, position: vec![0], timer: time.elapsed_seconds_f64(), talker: event.talker.clone(), @@ -597,6 +601,7 @@ pub fn handle_chat_events( mut ew_chatscript: EventWriter, mut ew_sfx: EventWriter, mut log: ResMut, + mut vars: ResMut, q_choices: Query>, mut q_chats: Query<(Entity, &mut Chat)>, time: Res