aboutsummaryrefslogtreecommitdiff
path: root/src/lua_api.rs
diff options
context:
space:
mode:
authorLibravatar Silas Bartha <silas@exvacuum.dev>2024-11-21 12:17:44 -0500
committerLibravatar Silas Bartha <silas@exvacuum.dev>2024-11-21 12:17:44 -0500
commit26e2eddd1efeb0a5fff8ffabebefdae48c4a65dc (patch)
tree7f0903a6c90fb4e09d2894f55cffd0acbcb51350 /src/lua_api.rs
parent99c398cc127dbc83480f98fea8c76f7c19d4dce8 (diff)
Thu Nov 21 12:17:44 PM EST 2024
Diffstat (limited to 'src/lua_api.rs')
-rw-r--r--src/lua_api.rs121
1 files changed, 115 insertions, 6 deletions
diff --git a/src/lua_api.rs b/src/lua_api.rs
index cda2486..f9a34d4 100644
--- a/src/lua_api.rs
+++ b/src/lua_api.rs
@@ -1,5 +1,13 @@
+use std::str::FromStr;
+
use bevy::prelude::*;
-use bevy_scriptum::{runtimes::lua::{BevyEntity, BevyVec3, LuaRuntime, LuaScriptData}, ScriptingRuntimeBuilder, Runtime};
+use bevy_scriptum::{
+ runtimes::lua::{BevyEntity, BevyVec3, LuaRuntime, LuaScriptData},
+ Runtime, ScriptingRuntimeBuilder,
+};
+use uuid::Uuid;
+
+use crate::{components::DirworldEntity, conditionals::Condition};
pub fn trigger_update(
mut scripted_entities: Query<(Entity, &mut LuaScriptData)>,
@@ -8,7 +16,7 @@ pub fn trigger_update(
) {
let delta = time.delta_seconds();
for (entity, mut script_data) in scripted_entities.iter_mut() {
- if let Err(e) = scripting_runtime.call_fn("on_update", &mut script_data, entity, (delta, )) {
+ if let Err(e) = scripting_runtime.call_fn("on_update", &mut script_data, entity, (delta,)) {
error!("Encountered lua scripting error: {:?}", e);
}
}
@@ -18,14 +26,28 @@ pub fn trigger_update(
macro_rules! register_fns {
($runtime:expr, $($function:expr),+) => {
- {
+ {
$runtime$(.add_function(stringify!($function).to_string(), $function))+
}
};
}
-pub fn register(runtime: ScriptingRuntimeBuilder<LuaRuntime>) -> ScriptingRuntimeBuilder<LuaRuntime> {
- register_fns!(runtime, translate, rotate)
+pub fn register(
+ runtime: ScriptingRuntimeBuilder<LuaRuntime>,
+) -> ScriptingRuntimeBuilder<LuaRuntime> {
+ register_fns!(
+ runtime,
+ translate,
+ rotate,
+ get_dirworld_id,
+ condition_true,
+ condition_ancestor_of,
+ condition_descendant_of,
+ condition_parent_of,
+ condition_child_of,
+ condition_in_room,
+ condition_object_in_room
+ )
}
fn translate(
@@ -50,4 +72,91 @@ fn rotate(
}
}
-// }}}
+fn get_dirworld_id(In((BevyEntity(entity),)): In<(BevyEntity,)>, dirworld_entity_query: Query<&DirworldEntity>) -> Option<String> {
+ dirworld_entity_query.get(entity).ok().and_then(|entity| entity.payload.as_ref().map(|payload| payload.id.to_string()))
+}
+
+// Conditionals
+fn condition_true(world: &mut World) -> bool {
+ Condition::True.evaluate(world)
+}
+
+fn condition_ancestor_of(
+ In((ancestor, descendant)): In<(String, String)>,
+ world: &mut World,
+) -> bool {
+ let Ok(ancestor) = Uuid::from_str(&ancestor) else {
+ warn!("Provided ancestor is not a valid UUID");
+ return false;
+ };
+ let Ok(descendant) = Uuid::from_str(&descendant) else {
+ warn!("Provided descendant is not a valid UUID");
+ return false;
+ };
+ Condition::AncestorOf {
+ ancestor,
+ descendant,
+ }
+ .evaluate(world)
+}
+
+fn condition_descendant_of(
+ In((descendant, ancestor)): In<(String, String)>,
+ world: &mut World,
+) -> bool {
+ let Ok(ancestor) = Uuid::from_str(&ancestor) else {
+ warn!("Provided ancestor is not a valid UUID");
+ return false;
+ };
+ let Ok(descendant) = Uuid::from_str(&descendant) else {
+ warn!("Provided descendant is not a valid UUID");
+ return false;
+ };
+ Condition::DescendantOf {
+ ancestor,
+ descendant,
+ }
+ .evaluate(world)
+}
+
+fn condition_parent_of(In((parent, child)): In<(String, String)>, world: &mut World) -> bool {
+ let Ok(parent) = Uuid::from_str(&parent) else {
+ warn!("Provided parent is not a valid UUID");
+ return false;
+ };
+ let Ok(child) = Uuid::from_str(&child) else {
+ warn!("Provided child is not a valid UUID");
+ return false;
+ };
+ Condition::ParentOf { parent, child }.evaluate(world)
+}
+
+fn condition_child_of(In((child, parent)): In<(String, String)>, world: &mut World) -> bool {
+ let Ok(parent) = Uuid::from_str(&parent) else {
+ warn!("Provided parent is not a valid UUID");
+ return false;
+ };
+ let Ok(child) = Uuid::from_str(&child) else {
+ warn!("Provided child is not a valid UUID");
+ return false;
+ };
+ Condition::ChildOf { parent, child }.evaluate(world)
+}
+
+fn condition_in_room(In((room,)): In<(String,)>, world: &mut World) -> bool {
+ let Ok(room) = Uuid::from_str(&room) else {
+ warn!("Provided room is not a valid UUID");
+ return false;
+ };
+ Condition::InRoom(room).evaluate(world)
+}
+
+fn condition_object_in_room(In((object,)): In<(String,)>, world: &mut World) -> bool {
+ let Ok(object) = Uuid::from_str(&object) else {
+ warn!("Provided object is not a valid UUID");
+ return false;
+ };
+ Condition::ObjectInRoom(object).evaluate(world)
+}
+
+// }}}