Sdk/Binding: Add component access by name
Former-commit-id: dd2e5d76254e8ce5e60db62e606c8129d784f528
This commit is contained in:
parent
5e5a83f825
commit
51549f8526
|
|
@ -83,12 +83,6 @@ namespace Ndk
|
|||
Nz::LuaClass<Nz::Node> nodeClass;
|
||||
|
||||
// SDK
|
||||
Nz::LuaClass<Application*> application;
|
||||
Nz::LuaClass<EntityHandle> entityClass;
|
||||
Nz::LuaClass<NodeComponentHandle> nodeComponent;
|
||||
Nz::LuaClass<VelocityComponentHandle> velocityComponent;
|
||||
Nz::LuaClass<WorldHandle> worldClass;
|
||||
|
||||
using AddComponentFunc = int(*)(Nz::LuaInstance&, EntityHandle&);
|
||||
using GetComponentFunc = int(*)(Nz::LuaInstance&, BaseComponent&);
|
||||
|
||||
|
|
@ -100,7 +94,16 @@ namespace Ndk
|
|||
Nz::String name;
|
||||
};
|
||||
|
||||
ComponentBinding* QueryComponentIndex(Nz::LuaInstance& lua, int argIndex = 1);
|
||||
|
||||
Nz::LuaClass<Application*> application;
|
||||
Nz::LuaClass<EntityHandle> entityClass;
|
||||
Nz::LuaClass<NodeComponentHandle> nodeComponent;
|
||||
Nz::LuaClass<VelocityComponentHandle> velocityComponent;
|
||||
Nz::LuaClass<WorldHandle> worldClass;
|
||||
|
||||
std::vector<ComponentBinding> m_componentBinding;
|
||||
std::unordered_map<Nz::String, ComponentIndex> m_componentBindingByName;
|
||||
|
||||
#ifndef NDK_SERVER
|
||||
// Audio
|
||||
|
|
|
|||
|
|
@ -21,6 +21,7 @@ namespace Ndk
|
|||
m_componentBinding.resize(T::componentIndex + 1);
|
||||
|
||||
m_componentBinding[T::componentIndex] = std::move(binding);
|
||||
m_componentBindingByName[name] = T::componentIndex;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
|
|
|
|||
|
|
@ -59,56 +59,29 @@ namespace Ndk
|
|||
entityClass.BindMethod("Kill", &Entity::Kill);
|
||||
entityClass.BindMethod("IsEnabled", &Entity::IsEnabled);
|
||||
entityClass.BindMethod("IsValid", &Entity::IsValid);
|
||||
entityClass.BindMethod("RemoveComponent", (void(Entity::*)(ComponentIndex)) &Entity::RemoveComponent);
|
||||
entityClass.BindMethod("RemoveAllComponents", &Entity::RemoveAllComponents);
|
||||
entityClass.BindMethod("__tostring", &EntityHandle::ToString);
|
||||
|
||||
entityClass.BindMethod("AddComponent", [this] (Nz::LuaInstance& lua, EntityHandle& handle) -> int
|
||||
entityClass.BindMethod("AddComponent", [this] (Nz::LuaInstance& instance, EntityHandle& handle) -> int
|
||||
{
|
||||
int index = 1;
|
||||
ComponentIndex componentIndex = lua.Check<ComponentIndex>(&index);
|
||||
ComponentBinding* binding = QueryComponentIndex(instance);
|
||||
|
||||
if (componentIndex > m_componentBinding.size())
|
||||
{
|
||||
lua.Error("Invalid component index");
|
||||
return 0;
|
||||
}
|
||||
|
||||
ComponentBinding& binding = m_componentBinding[componentIndex];
|
||||
if (binding.name.IsEmpty())
|
||||
{
|
||||
lua.Error("This component is not available to the LuaAPI");
|
||||
return 0;
|
||||
}
|
||||
|
||||
return binding.adder(lua, handle);
|
||||
return binding->adder(instance, handle);
|
||||
});
|
||||
|
||||
entityClass.BindMethod("GetComponent", [this] (Nz::LuaInstance& lua, EntityHandle& handle) -> int
|
||||
entityClass.BindMethod("GetComponent", [this] (Nz::LuaInstance& instance, EntityHandle& handle) -> int
|
||||
{
|
||||
int index = 1;
|
||||
ComponentIndex componentIndex = lua.Check<ComponentIndex>(&index);
|
||||
ComponentBinding* binding = QueryComponentIndex(instance);
|
||||
|
||||
if (!handle->HasComponent(componentIndex))
|
||||
{
|
||||
lua.PushNil();
|
||||
return 1;
|
||||
}
|
||||
return binding->getter(instance, handle->GetComponent(binding->index));
|
||||
});
|
||||
|
||||
if (componentIndex > m_componentBinding.size())
|
||||
entityClass.BindMethod("RemoveComponent", [this] (Nz::LuaInstance& instance, EntityHandle& handle) -> int
|
||||
{
|
||||
lua.Error("Invalid component index");
|
||||
ComponentBinding* binding = QueryComponentIndex(instance);
|
||||
|
||||
handle->RemoveComponent(binding->index);
|
||||
return 0;
|
||||
}
|
||||
|
||||
ComponentBinding& binding = m_componentBinding[componentIndex];
|
||||
if (binding.name.IsEmpty())
|
||||
{
|
||||
lua.Error("This component is not available to the LuaAPI");
|
||||
return 0;
|
||||
}
|
||||
|
||||
return binding.getter(lua, handle->GetComponent(componentIndex));
|
||||
});
|
||||
|
||||
/*********************************** Ndk::NodeComponent **********************************/
|
||||
|
|
@ -199,4 +172,45 @@ namespace Ndk
|
|||
}
|
||||
instance.SetGlobal("ComponentType");
|
||||
}
|
||||
|
||||
LuaBinding::ComponentBinding* LuaBinding::QueryComponentIndex(Nz::LuaInstance& instance, int argIndex)
|
||||
{
|
||||
switch (instance.GetType(argIndex))
|
||||
{
|
||||
case Nz::LuaType_Number:
|
||||
{
|
||||
ComponentIndex componentIndex = instance.Check<ComponentIndex>(&argIndex);
|
||||
if (componentIndex > m_componentBinding.size())
|
||||
{
|
||||
instance.Error("Invalid component index");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
ComponentBinding& binding = m_componentBinding[componentIndex];
|
||||
if (binding.name.IsEmpty())
|
||||
{
|
||||
instance.Error("Invalid component index");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return &binding;
|
||||
}
|
||||
|
||||
case Nz::LuaType_String:
|
||||
{
|
||||
const char* key = instance.CheckString(argIndex);
|
||||
auto it = m_componentBindingByName.find(key);
|
||||
if (it == m_componentBindingByName.end())
|
||||
{
|
||||
instance.Error("Invalid component name");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return &m_componentBinding[it->second];
|
||||
}
|
||||
}
|
||||
|
||||
instance.Error("Invalid component index at #" + Nz::String::Number(argIndex));
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue