WIP if-branches in chats

This commit is contained in:
yuni 2024-04-14 17:29:35 +02:00
parent 5df3f66ea6
commit b4ff95c3be
2 changed files with 24 additions and 2 deletions

View file

@ -18,6 +18,7 @@ pub const TOKEN_WARN: &str = "warn";
pub const TOKEN_SLEEP: &str = "sleep"; pub const TOKEN_SLEEP: &str = "sleep";
pub const TOKEN_SET: &str = "set"; pub const TOKEN_SET: &str = "set";
pub const TOKEN_IF: &str = "if"; pub const TOKEN_IF: &str = "if";
pub const TOKEN_THEN: &str = "then";
pub const TOKEN_GOTO: &str = "goto"; pub const TOKEN_GOTO: &str = "goto";
pub const TOKEN_LABEL: &str = "label"; pub const TOKEN_LABEL: &str = "label";
pub const TOKEN_SCRIPT: &str = "script"; pub const TOKEN_SCRIPT: &str = "script";
@ -43,6 +44,7 @@ pub const NON_CHOICE_TOKENS: &[&str] = &[
TOKEN_SLEEP, TOKEN_SLEEP,
TOKEN_SET, TOKEN_SET,
TOKEN_IF, TOKEN_IF,
TOKEN_THEN,
TOKEN_GOTO, TOKEN_GOTO,
TOKEN_LABEL, TOKEN_LABEL,
TOKEN_SCRIPT, TOKEN_SCRIPT,
@ -118,6 +120,7 @@ pub enum ChatEvent {
RunScript(String), RunScript(String),
SleepSeconds(f64), SleepSeconds(f64),
SetVariable(String), SetVariable(String),
GotoIf(String, ChatPos),
} }
// This is the only place where any YAML interaction should be happening. // This is the only place where any YAML interaction should be happening.
@ -351,7 +354,11 @@ impl ChatDB {
result = Some(Value::String(value_string.into())); result = Some(Value::String(value_string.into()));
} }
Some(Value::Mapping(mapping)) => { Some(Value::Mapping(mapping)) => {
if let Some((_choicetext, subconversation, _)) = self.search_choice(value) { if let Some(branch) = mapping.get(TOKEN_THEN) {
result = Some(Value::Mapping(mapping.clone()));
next_pointer = Some(branch.clone());
}
else if let Some((_choicetext, subconversation, _)) = self.search_choice(value) {
result = Some(Value::Mapping(mapping.clone())); result = Some(Value::Mapping(mapping.clone()));
next_pointer = Some(subconversation); next_pointer = Some(subconversation);
} }
@ -430,7 +437,11 @@ impl ChatDB {
for (key, value) in &map { for (key, value) in &map {
let key = key.as_str(); let key = key.as_str();
match (key, value) { match (key, value) {
(Some(TOKEN_IF), _) => {} // TODO (Some(TOKEN_IF), Value::String(condition)) => {
let mut pos = chat.position.clone();
pos.push(0);
event.send(ChatEvent::GotoIf(condition.into(), pos));
}
(Some(TOKEN_SOUND), Value::String(sound_name)) => { (Some(TOKEN_SOUND), Value::String(sound_name)) => {
sound = sound_name.clone(); sound = sound_name.clone();
} }
@ -667,6 +678,11 @@ pub fn handle_chat_events(
vars.set_in_scope(&chat.talker.actor_id, string, "".into()); vars.set_in_scope(&chat.talker.actor_id, string, "".into());
} }
} }
ChatEvent::GotoIf(condition, goto) => {
if condition != "" {
chat.position = goto.clone();
}
}
} }
} }
} }

View file

@ -17,8 +17,14 @@
- chat: Icarus - chat: Icarus
- if:
then:
- Oh hey, you're back!
- Nice to see you again!
- goto: back
- Oh hey, you're awake! - Oh hey, you're awake!
- set: talked_before - set: talked_before
- label: back
- I found you drifting out cold, and thought, I better watch over you. - I found you drifting out cold, and thought, I better watch over you.
- Took us here behind that moonlet, to shield you from the micros. - Took us here behind that moonlet, to shield you from the micros.
- Thank you!: - Thank you!: