Sdk/Binding: Add component access by name

Former-commit-id: dd2e5d76254e8ce5e60db62e606c8129d784f528
This commit is contained in:
Lynix 2016-05-05 20:40:25 +02:00
parent 5e5a83f825
commit 51549f8526
3 changed files with 63 additions and 45 deletions

View File

@ -83,12 +83,6 @@ namespace Ndk
Nz::LuaClass<Nz::Node> nodeClass; Nz::LuaClass<Nz::Node> nodeClass;
// SDK // 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 AddComponentFunc = int(*)(Nz::LuaInstance&, EntityHandle&);
using GetComponentFunc = int(*)(Nz::LuaInstance&, BaseComponent&); using GetComponentFunc = int(*)(Nz::LuaInstance&, BaseComponent&);
@ -100,7 +94,16 @@ namespace Ndk
Nz::String name; 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::vector<ComponentBinding> m_componentBinding;
std::unordered_map<Nz::String, ComponentIndex> m_componentBindingByName;
#ifndef NDK_SERVER #ifndef NDK_SERVER
// Audio // Audio

View File

@ -21,6 +21,7 @@ namespace Ndk
m_componentBinding.resize(T::componentIndex + 1); m_componentBinding.resize(T::componentIndex + 1);
m_componentBinding[T::componentIndex] = std::move(binding); m_componentBinding[T::componentIndex] = std::move(binding);
m_componentBindingByName[name] = T::componentIndex;
} }
template<typename T> template<typename T>

View File

@ -59,56 +59,29 @@ namespace Ndk
entityClass.BindMethod("Kill", &Entity::Kill); entityClass.BindMethod("Kill", &Entity::Kill);
entityClass.BindMethod("IsEnabled", &Entity::IsEnabled); entityClass.BindMethod("IsEnabled", &Entity::IsEnabled);
entityClass.BindMethod("IsValid", &Entity::IsValid); entityClass.BindMethod("IsValid", &Entity::IsValid);
entityClass.BindMethod("RemoveComponent", (void(Entity::*)(ComponentIndex)) &Entity::RemoveComponent);
entityClass.BindMethod("RemoveAllComponents", &Entity::RemoveAllComponents); entityClass.BindMethod("RemoveAllComponents", &Entity::RemoveAllComponents);
entityClass.BindMethod("__tostring", &EntityHandle::ToString); 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; ComponentBinding* binding = QueryComponentIndex(instance);
ComponentIndex componentIndex = lua.Check<ComponentIndex>(&index);
if (componentIndex > m_componentBinding.size()) return binding->adder(instance, handle);
{
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);
}); });
entityClass.BindMethod("GetComponent", [this] (Nz::LuaInstance& lua, EntityHandle& handle) -> int entityClass.BindMethod("GetComponent", [this] (Nz::LuaInstance& instance, EntityHandle& handle) -> int
{ {
int index = 1; ComponentBinding* binding = QueryComponentIndex(instance);
ComponentIndex componentIndex = lua.Check<ComponentIndex>(&index);
if (!handle->HasComponent(componentIndex)) return binding->getter(instance, handle->GetComponent(binding->index));
{ });
lua.PushNil();
return 1;
}
if (componentIndex > m_componentBinding.size()) entityClass.BindMethod("RemoveComponent", [this] (Nz::LuaInstance& instance, EntityHandle& handle) -> int
{ {
lua.Error("Invalid component index"); ComponentBinding* binding = QueryComponentIndex(instance);
return 0;
}
ComponentBinding& binding = m_componentBinding[componentIndex]; handle->RemoveComponent(binding->index);
if (binding.name.IsEmpty()) return 0;
{
lua.Error("This component is not available to the LuaAPI");
return 0;
}
return binding.getter(lua, handle->GetComponent(componentIndex));
}); });
/*********************************** Ndk::NodeComponent **********************************/ /*********************************** Ndk::NodeComponent **********************************/
@ -199,4 +172,45 @@ namespace Ndk
} }
instance.SetGlobal("ComponentType"); 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;
}
} }