implement "if" statements with one boolean condition

This commit is contained in:
yuni 2024-04-14 18:56:40 +02:00
parent d6901bef00
commit d51333274b
3 changed files with 25 additions and 11 deletions

View file

@ -679,7 +679,7 @@ pub fn handle_chat_events(
} }
} }
ChatEvent::GotoIf(condition, goto) => { ChatEvent::GotoIf(condition, goto) => {
if condition != "" { if vars.evaluate(condition, &chat.talker.actor_id) {
chat.position = goto.clone(); chat.position = goto.clone();
} }
} }

View file

@ -17,14 +17,13 @@
- chat: Icarus - chat: Icarus
- if: $talked_before - if: $met
then: then:
- Oh hey, you're back! - Oh hey, you're back!
- Nice to see you again! - How are you doing?
- goto: back - goto: howru
- Oh hey, you're awake! - Oh hey, you're awake!
- set: $talked_before - set: $met
- 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!:

View file

@ -224,17 +224,15 @@ impl GameVars {
return None; return None;
} }
#[allow(dead_code)]
pub fn getb(&self, key: &str) -> bool { pub fn getb(&self, key: &str) -> bool {
if let Some(value) = self.db.get(key) { if let Some(value) = self.db.get(key) {
return value != "0"; return Self::evaluate_str_as_bool(value);
} }
return false; return false;
} }
#[allow(dead_code)] pub fn evaluate_str_as_bool(string: &str) -> bool {
pub fn set(&mut self, key: &str, value: String) { return string != "0";
self.db.insert(key.to_lowercase(), value);
} }
// This method ensures that the variable name contains a scope separator, // This method ensures that the variable name contains a scope separator,
@ -279,4 +277,21 @@ impl GameVars {
let key = Self::normalize_varname(fallback_scope, key); let key = Self::normalize_varname(fallback_scope, key);
self.db.insert(key, value); self.db.insert(key, value);
} }
pub fn evaluate(&self, condition: &str, scope: &str) -> bool {
let parts: Vec<&str> = condition.split(" ").collect();
if parts.len() == 1 {
let part = parts[0];
if part.contains(SCOPE_SEPARATOR) {
let part = Self::normalize_varname(scope, part);
let value_bool = self.getb(part.as_str());
return value_bool;
}
else {
return Self::evaluate_str_as_bool(part);
}
} else {
return false;
}
}
} }