Improve Lua ECS #30

Merged
SeanOMik merged 15 commits from feat/improve-lua-ecs-29 into main 2024-10-30 03:22:50 +00:00
5 changed files with 76 additions and 2 deletions
Showing only changes of commit 23a215ba46 - Show all commits

View File

@ -93,6 +93,13 @@ function on_update()
for _, transform in changed_res:iter() do for _, transform in changed_res:iter() do
print("Entity transform changed to: '" .. tostring(transform) .. "' on tick " .. tostring(world:get_tick())) print("Entity transform changed to: '" .. tostring(transform) .. "' on tick " .. tostring(world:get_tick()))
end end
local tick_view = View.new(TickOf(Transform))
local tick_res = world:view_query(tick_view)
---@param tick number
for _, tick in tick_res:iter() do
print("Entity transform last changed on tick " .. tostring(tick))
end
end end
--[[ function on_post_update() --[[ function on_post_update()

View File

@ -42,6 +42,15 @@ end
---@see OrQuery ---@see OrQuery
---@param ... function|table|userdata ---@param ... function|table|userdata
---@return OrQuery ---@return OrQuery
function Not(...) function Or(...)
return OrQuery.new(...) return OrQuery.new(...)
end
---Create a `TickOfQuery` for retrieving the tick of the resource or component on the entity.
---
---@see TickOfQuery
---@param ... table|userdata
---@return TickOfQuery
function TickOf(...)
return TickOfQuery.new(...)
end end

View File

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

View File

@ -0,0 +1,54 @@
use crate::{
lua::{LuaComponent, FN_NAME_INTERNAL_ECS_QUERY_RESULT},
ReflectBranch, ScriptEntity, ScriptWorldPtr,
};
use super::LuaQueryResult;
#[derive(Clone)]
pub struct LuaTickOfQuery(LuaComponent);
impl mlua::FromLua for LuaTickOfQuery {
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: "TickOfQuery".into(),
message: None,
})
.and_then(|ud| ud.borrow::<Self>())
.map(|ud| ud.clone())
}
}
impl mlua::UserData for LuaTickOfQuery {
fn add_methods<M: mlua::UserDataMethods<Self>>(methods: &mut M) {
methods.add_function("new", |_, comp: LuaComponent| Ok(Self(comp)));
methods.add_method(
FN_NAME_INTERNAL_ECS_QUERY_RESULT,
|_, this, (world, en): (ScriptWorldPtr, ScriptEntity)| {
let world = world.read();
let reflect = this.0.reflect_type()?;
match &reflect.reflect_branch {
ReflectBranch::Component(comp) => {
if let Some(tick) = comp.reflect_tick(&world, *en) {
Ok(LuaQueryResult::Some(mlua::Value::Number(*tick as _)))
} else {
Ok(LuaQueryResult::FilterDeny)
}
}
ReflectBranch::Resource(res) => {
if let Some(tick) = res.reflect_tick(&world) {
Ok(LuaQueryResult::Some(mlua::Value::Number(*tick as _)))
} else {
Ok(LuaQueryResult::FilterDeny)
}
}
}
},
);
}
}

View File

@ -1,7 +1,7 @@
use lyra_ecs::ResourceObject; use lyra_ecs::ResourceObject;
use lyra_reflect::Reflect; use lyra_reflect::Reflect;
use crate::{lua::{ecs::{query::{LuaChangedQuery, LuaHasQuery, LuaNotQuery, LuaOrQuery, LuaResQuery}, 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, 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: //fn register_lua_proxy::<T:
@ -50,6 +50,7 @@ impl ScriptApiProvider for LyraEcsApiProvider {
globals.set("HasQuery", ctx.create_proxy::<LuaHasQuery>()?)?; globals.set("HasQuery", ctx.create_proxy::<LuaHasQuery>()?)?;
globals.set("NotQuery", ctx.create_proxy::<LuaNotQuery>()?)?; globals.set("NotQuery", ctx.create_proxy::<LuaNotQuery>()?)?;
globals.set("OrQuery", ctx.create_proxy::<LuaOrQuery>()?)?; globals.set("OrQuery", ctx.create_proxy::<LuaOrQuery>()?)?;
globals.set("TickOfQuery", ctx.create_proxy::<LuaTickOfQuery>()?)?;
expose_comp_table_wrapper::<LuaCamera>(&ctx, &globals, "Camera")?; expose_comp_table_wrapper::<LuaCamera>(&ctx, &globals, "Camera")?;
expose_comp_table_wrapper::<LuaFreeFlyCamera>(&ctx, &globals, "FreeFlyCamera")?; expose_comp_table_wrapper::<LuaFreeFlyCamera>(&ctx, &globals, "FreeFlyCamera")?;