lua: create LuaOptionalQuery
This commit is contained in:
parent
23a215ba46
commit
964c4ec423
|
@ -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(...)
|
||||
|
@ -54,3 +56,16 @@ end
|
|||
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
|
|
@ -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::{
|
||||
|
|
|
@ -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)),
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
|
@ -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")?;
|
||||
|
|
Loading…
Reference in New Issue