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) => {
if condition != "" {
if vars.evaluate(condition, &chat.talker.actor_id) {
chat.position = goto.clone();
}
}

View file

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

View file

@ -224,17 +224,15 @@ impl GameVars {
return None;
}
#[allow(dead_code)]
pub fn getb(&self, key: &str) -> bool {
if let Some(value) = self.db.get(key) {
return value != "0";
return Self::evaluate_str_as_bool(value);
}
return false;
}
#[allow(dead_code)]
pub fn set(&mut self, key: &str, value: String) {
self.db.insert(key.to_lowercase(), value);
pub fn evaluate_str_as_bool(string: &str) -> bool {
return string != "0";
}
// 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);
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;
}
}
}