lua: create LuaOptionalQuery

This commit is contained in:
SeanOMik 2024-10-29 14:22:03 -04:00
parent 23a215ba46
commit 964c4ec423
Signed by: SeanOMik
GPG Key ID: FEC9E2FC15235964
4 changed files with 66 additions and 3 deletions

View File

@ -7,6 +7,8 @@ function Res(resource)
return ResQuery.new(resource)
end
---@alias Query function|table|userdata
---Create a `ChangedQuery` query that will return only if the resource or component has changed
---since last tick.
---
@ -30,7 +32,7 @@ end
---filter denies.
---
---@see NotQuery
---@param val function|table|userdata
---@param val Query
---@return NotQuery
function Not(val)
return NotQuery.new(val)
@ -40,7 +42,7 @@ end
---The queries are evaluated in the order they were provided.
---
---@see OrQuery
---@param ... function|table|userdata
---@param ... Query
---@return OrQuery
function Or(...)
return OrQuery.new(...)
@ -53,4 +55,17 @@ end
---@return TickOfQuery
function TickOf(...)
return TickOfQuery.new(...)
end
---Create any `OptionalQuery` that allows for a query to return nothing.
---
---If the query is a filter, its result will essentially be ignored. If the query returns `None`
---or `AlwaysNone`, this query will return `Nil`. If the query results in a value, its value
---will be the result of this query.
---
---@see OptionalQuery
---@param q Query
---@return OptionalQuery
function Optional(q)
return OptionalQuery.new(q)
end

View File

@ -16,6 +16,9 @@ pub use or::*;
mod tick_of;
pub use tick_of::*;
mod optional;
pub use optional::*;
use lyra_ecs::Entity;
use crate::{

View File

@ -0,0 +1,44 @@
use crate::{
lua::FN_NAME_INTERNAL_ECS_QUERY_RESULT, ScriptEntity, ScriptWorldPtr,
};
use super::{LuaQuery, LuaQueryResult};
#[derive(Clone)]
pub struct LuaOptionalQuery(LuaQuery);
impl mlua::FromLua for LuaOptionalQuery {
fn from_lua(value: mlua::Value, _: &mlua::Lua) -> mlua::Result<Self> {
let tyname = value.type_name();
value
.as_userdata()
.ok_or(mlua::Error::FromLuaConversionError {
from: tyname,
to: "OptionalQuery".into(),
message: None,
})
.and_then(|ud| ud.borrow::<Self>())
.map(|ud| ud.clone())
}
}
impl mlua::UserData for LuaOptionalQuery {
fn add_methods<M: mlua::UserDataMethods<Self>>(methods: &mut M) {
methods.add_function("new", |_, q: LuaQuery| Ok(Self(q)));
methods.add_method(
FN_NAME_INTERNAL_ECS_QUERY_RESULT,
|_, this, (world, en): (ScriptWorldPtr, ScriptEntity)| {
let res = this.0.get_query_result(world, en.0)?;
match res {
LuaQueryResult::None => Ok(LuaQueryResult::Some(mlua::Value::Nil)),
LuaQueryResult::AlwaysNone => Ok(LuaQueryResult::Some(mlua::Value::Nil)),
LuaQueryResult::FilterPass => Ok(LuaQueryResult::FilterPass),
LuaQueryResult::FilterDeny => Ok(LuaQueryResult::FilterPass),
LuaQueryResult::Some(v) => Ok(LuaQueryResult::Some(v)),
}
},
);
}
}

View File

@ -1,7 +1,7 @@
use lyra_ecs::ResourceObject;
use lyra_reflect::Reflect;
use crate::{lua::{ecs::{query::{LuaChangedQuery, LuaHasQuery, LuaNotQuery, LuaOrQuery, LuaResQuery, LuaTickOfQuery}, View}, wrappers::*, LuaContext, LuaWrapper, RegisterLuaType, FN_NAME_INTERNAL_REFLECT, FN_NAME_INTERNAL_REFLECT_TYPE}, ScriptApiProvider, ScriptBorrow, ScriptData, ScriptDynamicBundle, ScriptWorldPtr};
use crate::{lua::{ecs::{query::{LuaChangedQuery, LuaHasQuery, LuaNotQuery, LuaOptionalQuery, LuaOrQuery, LuaResQuery, LuaTickOfQuery}, View}, wrappers::*, LuaContext, LuaWrapper, RegisterLuaType, FN_NAME_INTERNAL_REFLECT, FN_NAME_INTERNAL_REFLECT_TYPE}, ScriptApiProvider, ScriptBorrow, ScriptData, ScriptDynamicBundle, ScriptWorldPtr};
//fn register_lua_proxy::<T:
@ -51,6 +51,7 @@ impl ScriptApiProvider for LyraEcsApiProvider {
globals.set("NotQuery", ctx.create_proxy::<LuaNotQuery>()?)?;
globals.set("OrQuery", ctx.create_proxy::<LuaOrQuery>()?)?;
globals.set("TickOfQuery", ctx.create_proxy::<LuaTickOfQuery>()?)?;
globals.set("OptionalQuery", ctx.create_proxy::<LuaOptionalQuery>()?)?;
expose_comp_table_wrapper::<LuaCamera>(&ctx, &globals, "Camera")?;
expose_comp_table_wrapper::<LuaFreeFlyCamera>(&ctx, &globals, "FreeFlyCamera")?;